hdu 4704 Sum (隔板法,指数循环节,费马小定理)
题意:定义s(k)为将n分成k个正整数的划分数,给出n,问s(1) + s(2) + ... + s(n-1) + s(n)是多少,结果9+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)9+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))
然后快速幂搞之。
/* ***********************************************
Author :111qqz
Created Time :Wed 26 Oct 2016 06:22:39 PM CST
File Name :code/hdu/4704.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E5+7;
7const LL mod = 1E9+7;
8char st[N];
9int len;
10LL n;
11LL ksm(LL a,LL b)
12{
13 LL res = 1LL;
14 while (b>0)
15 {
16 if (b&1) res = (res * a) % mod;
17 b = b >> 1LL;
18 a = ( a*a ) % mod;
19 }
20 return res;
21}
22int main()
23{
24 #ifndef ONLINE_JUDGE
25 freopen("code/in.txt","r",stdin);
26 #endif
1 while (~scanf("%s",st))
2 {
3 len = strlen(st);
4 n = 0 ;
5 for ( int i = 0 ; i < len ; i++)
6 {
7 LL val = st[i]-'0';
8 n = ((n * 10LL)%(mod-1) + val)% (mod-1);
9 }
10 n = (n-1+mod-1)%(mod-1);
11 LL ans = ksm( 2,n );
12 printf("%lld\n",ans);
13 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}