hdu 4704(费马小定理+快速幂取模)

                                               Sum

                                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                                                               Total Submission(s): 2633    Accepted Submission(s): 1104

Problem Description
 
Sample Input
2
Sample Output
2
Hint
1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases.
 
Source
题意 :给定一个数n 将其分解,Si 表示将n拆成i个数的方案数
用隔板法可以很容易算出结果2^(n-1),mod=1e9+7;
gcd(2,mod)=1; 费马小定理  2^(mod-1)%mod=1;
结果求2^(n-1)%mod ==>2^[(n-1)%(mod-1)]%mod
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<stack>
12 #include<string>
13 #define eps 0.000000001
14 typedef long long ll;
15 typedef unsigned long long LL;
16 using namespace std;
17 const int INF=0x3f3f3f3f;
18 const int N=100000+10;
19 const ll mod=1e9+7;
20 char str[N];
21 ll ksm(ll a,ll b){
22     ll ans=1;
23     while(b){
24         if(b&1){
25             ans=ans*a%mod;
26         }
27         b=b/2;
28         a=a*a%mod;
29     }
30     return ans;
31 }
32 int main(){
33     while(gets(str)){
34         ll m=mod-1;
35         ll ans=0;
36         int len=strlen(str);
37         for(int i=0;i<len;i++){
38             ans=ans*10+str[i]-'0';
39             if(ans>=m)ans=ans%m;
40         }
41         ans=(ans-1+m)%m;
42         ll t;
43         t=ksm(2,ans);
44         printf("%lld\n",t);
45     }
46 }

 

 

posted on 2017-03-27 20:55  见字如面  阅读(528)  评论(0编辑  收藏  举报

导航