• POJ 3761 Bubble Sort(乘方取模)


     点我看题目

    题意 : 冒泡排序的原理众所周知,需要扫描很多遍。而现在是求1到n的各种排列中,需要扫描k遍就变为有序的数列的个数,结果模20100713,当然了,只要数列有序就扫描结束,不需要像真正的冒泡排序要扫描n-1遍。

    思路 : 这个题的结果是K!((K + 1) ^ (N - K) - K ^ (N - K))。需要用到逆序数,此题具体推导

     1 //POJ 3761
     2 #include <iostream>
     3 #include <stdio.h>
     4 #include <string.h>
     5 
     6 using namespace std;
     7 const __int64 mod = 20100713LL ;
     8 __int64 factorial[1010000] ;
     9 
    10 void chart()
    11 {
    12     factorial[0] = factorial[1] = 1;
    13     for(int i = 2 ; i <= 1000000 ; i++)
    14         factorial[i] = factorial[i-1]*i % mod ;
    15 }
    16 
    17 __int64 multimod(__int64 x,__int64 n )
    18 {
    19     __int64 tmp = x ,res = 1LL ;
    20     while(n)
    21     {
    22         if(n & 1LL)
    23         {
    24             res *= tmp ;
    25             res %= mod ;
    26         }
    27         tmp *= tmp ;
    28         tmp %= mod ;
    29         n >>= 1LL ;
    30     }
    31     return res ;
    32 }
    33 int main()
    34 {
    35     __int64 t,n,k ;
    36     __int64 ans1,ans2,ans ;
    37     chart() ;
    38     scanf("%I64d",&t) ;
    39     while(t--)
    40     {
    41         scanf("%I64d %I64d",&n,&k) ;
    42         if(k == 0) {
    43             printf("1
    ") ;
    44             continue ;
    45         }
    46         ans1 = ans2 = factorial[k] ;
    47         ans1 *= multimod(k+1,n-k) ;
    48         ans1 %= mod ;
    49         ans1 += mod ;
    50         ans2 *= multimod(k,n-k) ;
    51         ans2 %= mod ;
    52         ans = (ans1-ans2)%mod ;
    53         printf("%I64d
    ",ans) ;
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    java当前时间
    @Repository、@Service、@Controller 和 @Component
    spring mvc controller间跳转 重定向 传参
    MVC思想
    AJAX
    MySQL优化大总结
    MySQL数据库优化
    java JDBM2 的几个简单实例
    集群环境下如何防止定时任务重复执行?
    Java应用集群下的定时任务处理方案(mysql)
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3645981.html
Copyright © 2020-2023  润新知