Description
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+···+xn=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)
可以用费马小定理+快速幂来解题。
首先用费马小定理将指数进行降幂,然后用快速幂取余求得结果。
代码:
<span style="font-family:Arial;font-size:14px;">#include <iostream>
#include <stdio.h>
using namespace std;
char N[100005];
const int mod=1e9+7;
//费马小定理
long long PhiMa(char *N,int mod)
{
long long ans=0;
for(int i=0;N[i]!='\0';i++)
ans=(ans*10+N[i]-'0')%mod;
return ans;
}
//快速幂取余运算
long long FastPow(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
while(scanf("%s",N)!=EOF)
{
long long n=PhiMa(N,mod-1)-1;
printf("%d\n",FastPow(2,n));
}
return 0;
}
</span>
0