hdu 4704 Sum (組合+尤拉定理)
阿新 • • 發佈:2019-01-08
http://acm.hdu.edu.cn/showproblem.php?pid=4704
大意:給定N,設S(k)是由k個數字相加得到N的方案數,求解
分析:本題中兩個數字的排列,如1、2 應該有2種——1、2; 2、1(一開始以為它們相同,算作一種,怎麼也算不出來)
如此以來,容易分析,將數字n看作1+1+1……+1(n個)
那麼 (組合思維,隔板看待)
於是,答案就是
原問題的答案就是
我們發現,mod是一個素數,所以,藉助尤拉定理 (或者費馬小定理),得到
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long LL; const int mod=1e9+7,N=1e5+10; char str[N]; LL power(LL a,LL p){ LL ans=1; while(p){ if(p&1) ans=ans*a%mod; a=a*a%mod; p>>=1; } return ans; } int main(){ while(~scanf("%s",str)){ int len=strlen(str); LL ans=0; for(int i=0;i<len;i++){ ans=(ans*10+str[i]-'0')%(mod-1); } ans=(ans-1+mod-1)%(mod-1); printf("%lld\n",power(2LL,ans)); } return 0; }