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))
然后快速幂搞之。
1/* ***********************************************
2Author :111qqz
3Created Time :Wed 26 Oct 2016 06:22:39 PM CST
4File Name :code/hdu/4704.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34const LL mod = 1E9+7;
35char st[N];
36int len;
37LL n;
38LL ksm(LL a,LL b)
39{
40 LL res = 1LL;
41 while (b>0)
42 {
43 if (b&1) res = (res * a) % mod;
44 b = b >> 1LL;
45 a = ( a*a ) % mod;
46 }
47 return res;
48}
49int main()
50{
51 #ifndef ONLINE_JUDGE
52 freopen("code/in.txt","r",stdin);
53 #endif
54
55 while (~scanf("%s",st))
56 {
57 len = strlen(st);
58 n = 0 ;
59 for ( int i = 0 ; i < len ; i++)
60 {
61 LL val = st[i]-'0';
62 n = ((n * 10LL)%(mod-1) + val)% (mod-1);
63 }
64 n = (n-1+mod-1)%(mod-1);
65 LL ans = ksm( 2,n );
66 printf("%lld\n",ans);
67 }
68
69 #ifndef ONLINE_JUDGE
70 fclose(stdin);
71 #endif
72 return 0;
73}