↓ 跳过正文
  1. Posts/

hdu 2065 "红色病毒"问题 (指数型母函数,泰勒级数展开)

·739 字·2 分钟

http://acm.hdu.edu.cn/showproblem.php?pid=2065 题意:a,b,c,d四种元素,a,c只能出现偶数次(包括0次),b,d没有限制,问n个(2^64)个元素有多少种不同的组合。 思路:指数型母函数。。。n大的没办法用之前的办法做。

先来看下我们要求的式子:A=(1+x/1!+x^2/2!+x^3/3!……)^2*(1+x^2/2!+x^4/4!+x^6/6!……)^2.

其实一共四个式子相乘,但是a和c的情况相同,b和d的式子相同。

我们要求的是x^n的系数。。。n太大了。。直接搞肯定不行。

想到微积分学的泰勒展开。

e^x=1+x/1!+x^2/2!+x^3/3!+… (|x|<oo)

其实这里x的范围没有意义,因为母函数关注的是系数,不会代入x的值,所以可以不用考虑收敛性。

那么第一项(1+x/1!+x^2/2!+x^3/3!……)^2就可以换成(e^x)^2

第二项没有奇数项,很容易想到可以写成((e^x+e^(-x))/2)^2

继续化简:4A=(e^x)^2((e^x+e^(-x))/2)^2

4A = (e^(4x)+2e^(2x)+1)

我们要的是x^n的系数,再正向泰勒展开,得到x^n的系数应该是 (4^n+2*2^n)/4,也就是4^(n-1)+2^(n-1)

因为只要后两位的结果,其实就是结果0.快速幂搞之。

以及,2^64 long long 存不下,应该用unsigned long long ,类型说明符是 %llu

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月28日 星期日 14时01分58秒
 4File Name :code/hdu/2065.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 unsigned 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 LL MOD = 100;
34LL n;
35LL ksm(LL a,LL b)
36{
37    LL res = 1LL;
38    while (b)
39    {
40	if (b&1) res = (res*a)%MOD;
41	b = b>>1;
42	a =(a*a)%MOD;
43    }
44    return res;
45}
46int main()
47{
48	#ifndef  ONLINE_JUDGE
49	freopen("code/in.txt","r",stdin);
50  #endif
51	int T;
52
53	while (~scanf("%d",&T))
54	{
55	    if (T==0) break;
56	    int cas = 0 ;
57	    while (T--)
58	    {
59		printf("Case %d: ",++cas);
60		scanf("%llu",&n);
61		LL ans = ksm(4,n-1)+ksm(2,n-1);
62		ans %=MOD;
63		printf("%llu\n",ans);
64	    }
65	    printf("\n");
66	}
67
68
69  #ifndef ONLINE_JUDGE
70  fclose(stdin);
71  #endif
72    return 0;
73}

相关文章

bzoj 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 (筛法)

·389 字·1 分钟
http://www.lydsy.com/JudgeOnline/problem.php?id=1607 题意:n个数,求对于每个数来说,其他n-1个数中是它约数的数的个数。 思路:类似筛法,从小到大处理,数i对其所有倍数的数的答案有cnt[i]的贡献 。最后记得把自己是自己的约数的情况减掉。

hdu 1521 排列组合 (指数型母函数模板题)

·505 字·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=1521 题意:有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB",“BA"两种。

普通型母函数总结

·1401 字·3 分钟
从这里母函数(Generating function)详解学习了普通型母函数 1#include <iostream> 2using namespace std; 3// Author: Tanky Woo 4// www.wutianqi.com 5const int _max = 10001; 6// c1是保存各项质量砝码可以组合的数目 7// c2是中间量,保存没一次的情况 8int c1[_max], c2[_max]; 9int main() 10{ //int n,i,j,k; 11 int nNum; // 12 int i, j, k; 13 14 while(cin >> nNum) 15 { 16 for(i=0; i<=nNum; ++i) // ---- ① 17 { 18 c1[i] = 1; 19 c2[i] = 0; 20 } 21 for(i=2; i<=nNum; ++i) // ----- ② 22 { 23 24 for(j=0; j<=nNum; ++j) // ----- ③ 25 for(k=0; k+j<=nNum; k+=i) // ---- ④ 26 { 27 c2[j+k] += c1[j]; 28 } 29 for(j=0; j<=nNum; ++j) // ---- ⑤ 30 { 31 c1[j] = c2[j]; 32 c2[j] = 0; 33 } 34 } 35 cout << c1[nNum] << endl; 36 } 37 return 0; 38} ① 、首先对c1初始化,由第一个表达式(1+x+x^2+..x^n)初始化,把质量从0到n的所有砝码都初始化为1.

hdu 1284 铅笔兑换问题(母函数)

·401 字·1 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意:有1分,2分,3分的钱若干,问组成n(n<=32767)分钱的方案数。 思路:母函数. 需要注意的是多组数据。每次都搞会TLE,可以先预处理出来存到数组里,每次直接调用。如果预处理时间也还是慢的话,可以先跑出来,然后打表。这算一个小tip吧2333

hdu 2082 找单词 (母函数)

·406 字·1 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=2082 题意:26个字母,第i个字母有x[i]个,价值为i.问能组成多少个价值不超过50的单词(注意这里的单词只考虑字母的组成,不考虑字母之间的顺序) 思路:母函数。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月27日 星期六 15时56分31秒 4File Name :code/hdu/2082.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=1E4; 34LL a[N],tmp[N]; 35int x[N]; 36int n; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 43 44 ios::sync_with_stdio(false); 45 int T; 46 cin>>T; 47 while (T--) 48 { 49 for ( int i = 1; i <= 26 ; i ++) cin>>x[i]; 50 51 ms(tmp,0); 52 ms(a,0); 53 for ( int i = 0 ; i <= x[1]; i++) 54 { 55 a[i] = 1; 56 } 57 // cout<<"aaa"<<endl; 58 int cur = x[1]*1; 59 for ( int i = 2 ; i <= 26 ; i++) 60 { 61 for ( int j = 0 ; j<= cur ; j++) 62 { 63 for ( int k = 0 ; k <= x[i] ; k++) 64 { 65 tmp[j+i*k]+=a[j]; 66 } 67 } 68 69 cur += x[i]*i; 70 71 // cout<<"bbb:"<<endl; 72 73 for ( int j = 0 ; j <= cur ; j++) 74 { 75 a[j] = tmp[j]; 76 tmp[j] = 0 ; 77 } 78 } 79 // cout<<"asdsad"<<endl; 80 LL ans = 0LL ; 81 for ( int j = 1 ; j <= 50 ; j++) 82 { 83 ans += a[j]; 84 } 85 cout<<ans<<endl; 86 } 87 88 #ifndef ONLINE_JUDGE 89 fclose(stdin); 90 #endif 91 return 0; 92}