题意:求一个正整数N拆分成1到N个正整数的和,有多少种拆法(3 = 2 + 1和3 = 1 + 2算2种不同的拆法)(1 <= N < 10^100000)。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704
——>>数学不太行呀,还好队友给力~
题意转换为求2 ^ (N - 1),不过N好大~
由费马小定理,得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 <cstdio>
#include <cstring>
using namespace std;
const int mod = 1000000000 + 7;
const int mom = 1000000000 + 6;
const int maxn = 100000 + 10;
char N[maxn];
int pow_mod(int a, int n){
if(n == 1) return a % mod;
else if(!n) return 1;
long long x = pow_mod(a, n/2);
x = x * x % mod;
if(n&1) x = x * a % mod;
return (int)x;
}
int main()
{
while(scanf("%s", N) == 1){
int len = strlen(N);
long long n = 0;
for(int i = 0; i < len; i++) n = (n * 10 + N[i] - '0') % mom;
n = (n - 1 + mom) % mom;
printf("%d\n", pow_mod(2, n));
}
return 0;
}