题目:给n(1< n <10^10000),求(s1+s2+s3+…+sn)mod(1e9+7)。其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3。
Input
2
Output
2
Hint
For N = 2, S(1) = S(2) = 1.
The input file consists of multiple test cases.
思路:求n的分解方案总数之和,可以把n看成n个1,然后相邻的每个1之间可以选择放或者不放隔板,那么一共有n-1个可选的隔板,那么结果即为2^(n-1)%m(m=1e9+7)。n的数据范围很大,如果直接搞肯定会爆掉.这里我们用欧拉降幂+费马小定理来做。
欧拉降幂:
费马小定理:
推导过程如下:
然后用快速幂求解即可
代码如下
#include <iostream>
#include <cstring>
using namespace std;
#define LL long long
const int mod=1e9+7;
char s[100005];
LL kpow(LL x,LL n) // x^n%MAX
{
LL res=1;
while(n>0)
{
if(n & 1)
res=(res*x)%mod;
x=(x*x)%mod;
n >>= 1;
}
return res;
}
int main()
{
while(~scanf("%s",s))
{
int len=strlen(s);
LL sum=0;
for(int i=0;i<len;i++)
sum=(sum*10+s[i]-'0')%(mod-1);
sum=(sum-1+mod-1)%(mod-1);
printf("%lld\n",kpow(2,sum));
}
return 0;
}