您的位置:首页 > 其它

Sum HDU - 4704(隔板法+欧拉降幂+快速幂)

2017-09-07 21:30 357 查看
Problem Description



Sample Input

2

Sample Output

2

Hint

For N = 2, S(1) = S(2) = 1.

The input file consists of multiple test cases.

大致题意:给你一个数n,s(k)表示将n拆成i个数的方法,让你求s(1)+s(2)+……+s(n)对1e9+7模的值。

思路:将n拆成i个数,我们可以当做有n个1,i-1个隔板,然后放置,那么方案数即C(n-1,i-1)。总的方案数为C(n-1,0)+C(n-1,1)+……+C(n-1,n-1)=2^(n-1).因为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)+mod-1;

printf("%lld\n",kpow(2,sum));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: