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很大,所以我们可以采用欧拉降幂,然后再用快速幂解决得到答案。
欧拉降幂公式:
代码如下
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; }
相关文章推荐
- HDU - 4704 Sum——费马小定理
- HDU 4704 Sum Fermat定律
- hdu 4704 Sum【组合数学/费马小定理/大数取模】By cellur925
- hdu 4704 Sum (费马小定理+快速幂)
- HDU 4704 Sum
- hdu 4704 Sum 多校第十场
- HDU 4704 Sum(快速幂+费马小定理)
- hdu 4704 Sum-----2^高精度%p
- hdu 4704 Sum(费马小定理)解题报告
- HDU 4704 Sum
- HDU 4704 Sum (费马定理+快速幂)
- HDU 4704 SUM 整数快速幂+费马小定理
- [HDU 4704] Sum · 费马小定理 & 快速幂
- HDU 4704Sum(费马小定理+快速幂)
- hdu 4704 sum 大整数取模+费马小定理+数快速幂
- HDU 4704 Sum (隔板原理 + 费马小定理)
- HDU 4704 Sum
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
- Hdu 4704 Sum -- 快速幂+高精度处理
- [数论]HDU 4704 Sum 费马小定理