传送门:Sum
Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1028 Accepted Submission(s): 462
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/2fb92eb08283067dc96500ff89a997ed.jpeg)
Sample Input
2
Sample Output
2Hint1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases.
Source
解题报告:
题意:求一个正整数N拆分成1到N个正整数的和,有多少种拆法(3 = 2 + 1和3 = 1 + 2算2种不同的拆法)(1 <= N < 10^100000)。
题意转换为求2 ^ (n - 1)
由费马小定理,得2 ^ (10 ^ 9 + 6) % (10 ^ 9 + 7) = 1 = 2 ^ 0,
设MOD = 10^9 + 7,则2 ^ (n-1) % MOD = 2 ^ ((n - 1) % (10 ^ 9 + 6)) % MOD,
而(n- 1) % (10 ^ 9 + 6) = ((n % (10 ^ 9 + 6) - 1) + (10 ^ 9 + 6)) % (10 ^ 9 + 6),
最后用快速幂求解。
代码如下:#include<iostream>
#include<cstdio>
#include<cstring>
#define MOD 1000000007
using namespace std;
char s[1000010];
long long pow_mod(long long a,long long b,long long n){
long long res=1;
while(b){
if(b&1) res=res*a%n;
a=a*a%n;
b>>=1;
}
return res;
}
int main(){
while(scanf("%s",s)!=EOF){
int len=strlen(s);
long long n=0;
for(int i=0;i<len;i++)
n=(n*10+(s[i]-'0'))%(MOD-1);
n=(n-1+(MOD-1))%(MOD-1);
printf("%lld\n",pow_mod(2,n,MOD));
}
return 0;
}