HDU4704+费马小定理

费马小定理
题意:求s1+s2+s3+...+sn;si表示n划分i个数的n的划分的个数,如n=4,则s1=1,s2=3
    利用隔板定理可知,就是求(2^n-1)%mod-----Y
    现在已知 (2^mod-1)%mod = 1,所以  Y = 2^( (n%(mod-1) -1 +mod)%mod )%mod

证明( 定理:a^(p-1)==1%p,gcd(a,p)==1 ):
    (http://www.cnitblog.com/luckydmz/archive/2008/06/03/39458.html)
    构造模p的完全剩余系P = {0,1, 2, … ,p-1},
    因为gcd(a, p) = 1,所以A= {0*a, 1*a, 2*a, … ,(p-1)*a}也是模p的一个完全剩余系。
    就是说P和A在模p意义下是相等的。
    因为0 ≡ 0a (mod p),所以 P-{0} 与 A-{0*a}在模p意义下是相等的。
    记P'=P-{0},A'=A-{0*a}
    令W = πP' = 1 * 2 * 3 * 4 … * (p-1),Y = πA' = a * 2a *3a * 4a * …(p-1)a = W*a^(p-1)  //π表示连乘积
    有,W ≡ Y (mod p)
    即,W ≡ W*a^(p-1) (mod p)
    又因为,(W, p) = 1
    则有,1 ≡ a^(p-1) (mod p)

 1 /*
 2 费马小定理
 3 题意:求s1+s2+s3+...+sn;si表示n划分i个数的n的划分的个数,如n=4,则s1=1,s2=3
 4     利用隔板定理可知,就是求(2^n-1)%mod-----Y
 5 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 #include<stdlib.h>
 9 #include<algorithm>
10 #include<iostream>
11 #include<queue>
12 #include<map>
13 #include<stack>
14 #include<set>
15 #include<math.h>
16 using namespace std;
17 typedef long long int64;
18 //typedef __int64 int64;
19 typedef pair<int64,int64> PII;
20 #define MP(a,b) make_pair((a),(b)) 
21 const int maxn = 100015;
22 const int inf = 0x7fffffff;
23 const double pi=acos(-1.0);
24 const double eps = 1e-8;
25 const int64 mod = 1000000000+7;
26 
27 int64 Fast_Pow( int64 a,int64 n,int64 mod ){
28     int64 res = 1;
29     while( n>=1 ){
30         if( n&1 ){
31             res = res*a%mod;
32         }
33         a = a*a%mod;
34         n >>= 1;
35     }
36     return res%mod;
37 }
38 
39 int64 GetNum( char str[],int64 mod ){
40     int64 res = 0;
41     int len = strlen( str );
42     for( int i=0;i<len;i++ ){
43         res = (res*10+str[i]-'0')%mod;
44     }
45     return res;
46 }
47 
48 int main(){
49     char str[ maxn ];
50     while( scanf("%s",str)!=EOF ){
51         int64 n = GetNum( str,mod-1 );
52         printf("%I64d
",Fast_Pow( 2,(n-1+mod)%mod,mod ));
53     }
54     return 0;
55 }
View Code
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/3307021.html