Input
2
Output
2
Hint
1. For N = 2, S(1) = S(2) = 1.
2. The input file consists of multiple test cases.
题意就是 x1+x2+x3+... ... +xk=n有多少种可能,
利用“隔板法”,可以知道结果就是C(n-1)^0+C(n-1)^1+C(n-1)^2+···+C(n-1)^r+···+C(n-1)^(n-1).这是二项式定理的展开式。
根据二项式定理可以得出,结果就是2^(n-1)。
所以,这道题就转换为求解2^(n-1)mod(1e9+7)
可以用费马小定理+快速幂来解题,首先用费马小定理将指数进行降幂,然后用快速幂取余求得结果。
费马小定理的内容是:a为整数,p为质数,a与p互质,则恒有a^(p-1)%p==1;
所以n中含有多个p-1,所以可以通过k=(n-1)%(p-1)进行降幂,最终求的是a^k%p;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const long long mod = 1e9+7;
const int maxn=1e6+5;
typedef long long ll;
using namespace std;
char s[maxn];
long long Mood(long long a,long long b){
long long ans=1;
while(b){
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b=b>>1;
}
return ans;
}
long long feima(char *s){
long long ans=0;
int len=strlen(s);
for(int i=0;i<len;i++){
ans=(ans*10+s[i]-'0') %(mod-1);
}
return ans;
}
int main(){
while(~scanf("%s",s)){
long long sum=feima(s)-1;
printf("%lld\n",Mood(2,sum));
}
return 0;
}