题意:定义s(k)为将n分成k个正整数的划分数,给出n,问s(1) + s(2) + … + s(n-1) + s(n)是多少,结果%1E9+7,其中n<=10^100000。
思路:首先化简要求的式子。
根据隔板法_维基百科
现在有10个球,要放进3个盒子里
- ●●●●●●●●●●
隔2个板子,把10个球被隔开成3个部分
- ●|●|●●●●●●●●、●|●●|●●●●●●●、●|●●●|●●●●●●、●|●●●●|●●●●●、●|●●●●●|●●●●、●|●●●●●●|●●●、……
如此类推,10个球放进3个盒子的方法总数为{
n个球放进k个盒子的方法总数为{
问题等价于求{
的可行解数,其中
为正整数。
于是问题转化成:
n个木棍,n-1个缝,
分成1份则是C(n-1,0);
分成2份则是C(n-1,1);
分成3份则是C(n-1,2);
…
分成n份则是C(n-1,n-1);
ans = sum( C(n-1,i) ) (0<=i<=n-1)
=2^(n-1);
这是我能理解的得到2^(n-1)的方式。。。
看到有好多人说这个结论是显然的。。。求指教(说这是个结论记住就好的就算了23333)
接下来,就是求A=2^(n-1)%1E9+7的问题了。。。
根据指数循环节公式A=2^((n-1)%(mod-1))*2^(mod-1)%mod (其中mod=1E9+7)
由于gcd(2,1E9+7)=1,根据费马小定理2^(mod-1)%mod=1,因此A=2^((n-1)%(mod-1))
然后快速幂搞之。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
/* *********************************************** Author :111qqz Created Time :Wed 26 Oct 2016 06:22:39 PM CST File Name :code/hdu/4704.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=1E5+7; const LL mod = 1E9+7; char st[N]; int len; LL n; LL ksm(LL a,LL b) { LL res = 1LL; while (b>0) { if (b&1) res = (res * a) % mod; b = b >> 1LL; a = ( a*a ) % mod; } return res; } int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif while (~scanf("%s",st)) { len = strlen(st); n = 0 ; for ( int i = 0 ; i < len ; i++) { LL val = st[i]-'0'; n = ((n * 10LL)%(mod-1) + val)% (mod-1); } n = (n-1+mod-1)%(mod-1); LL ans = ksm( 2,n ); printf("%lld\n",ans); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!