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\n") ;

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\n",ans) ;

54     }

55     return 0;

56 }
View Code

 

你可能感兴趣的:(Bubble)