HDU 4704 Sum (费马小定理)
题意
给定一个N,设S(K)表示x1 + x2 + …… + xk = N的解的个数(xi ∈ Z+),求SUM( S(i), 0 <= i <= N ) mod 109+7.
思路
隔板问题,S(k) = C(n-1, k-1).所以SUM( S(i) ) = 2^(N-1). 而由费马小定理可得:如果(a, p)=1,X^N%p = X^(N%(p-1))%p.
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
long long exp_mod(long long a, long long b, long long m){
long long res = 1 % m, tmp = a % m;
while(b){
if (b & 1){
res = (res * tmp) % m;
}
tmp = tmp * tmp % m;
b >>= 1;
}
return res;
}
char num[100005];
int mod = 1e9 + 7;
int main(){
long long ans;
while(scanf("%s", num) != EOF){
ans = 0;
int len = strlen(num);
for(int i = 0; i < len; i ++){
ans = (ans * 10 + num[i] - 48) % (mod-1);
}
if (ans == 0) ans = mod-2;
else ans --;
printf("%I64d
", exp_mod(2, ans, mod));
}
return 0;
}
[/cpp]