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

时间:2023-03-08 23:47:42
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
hdu  4704(费马小定理+快速幂取模)
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
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<stack>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const int N=+;
const ll mod=1e9+;
char str[N];
ll ksm(ll a,ll b){
ll ans=;
while(b){
if(b&){
ans=ans*a%mod;
}
b=b/;
a=a*a%mod;
}
return ans;
}
int main(){
while(gets(str)){
ll m=mod-;
ll ans=;
int len=strlen(str);
for(int i=;i<len;i++){
ans=ans*+str[i]-'';
if(ans>=m)ans=ans%m;
}
ans=(ans-+m)%m;
ll t;
t=ksm(,ans);
printf("%lld\n",t);
}
}