母函数题目
hdu 1028 Ignatius and the Princess III 母函數模板題
http://acm.hdu.edu.cn/showproblem.php?pid=1028
題意:
整數的拆分。
母函數學習http://www.wutianqi.com/?p=596
View Code #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string>#define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a , b) ((a) < (b) ? (a) : (b)) #define Max(a , b) ((a) > (b) ? (a) : (b))#define ll __int64 #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 127 using namespace std; //freopen("din.txt","r",stdin);int c1[N],c2[N];void init(int n){int i,j,k;for (i = 0; i <= n; ++i){c1[i] = 1;//乘完后的系數c2[i] = 0;//乘的過程中的系數 }for (i = 2; i <= n; ++i){//總共有n個括號,從第2個起每一個括號都要和前面那一個括號相乘//所以可以忽略第一個括號 for (j = 0; j <= n; ++j){//j代表最前面這個大括號的項數 for (k = 0; k + j <= n; k += i){//在大括號后面,x都是以i方遞增的 c2[k + j] += c1[j];//這里就是大括號后面的括號與前面相乘的計算 }}for (j = 0; j <= n; ++j){c1[j] = c2[j];c2[j] = 0;}} } int main(){int n;init(120);while (~scanf("%d",&n)){printf("%d\n",c1[n]);}return 0; }?
hdu 1398?Square Coins
http://acm.hdu.edu.cn/showproblem.php?pid=1398
題意:
整數拆分的題目,只不過后邊的括號里面的差值變成了i^2吧了。。
View Code #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string>#define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a , b) ((a) < (b) ? (a) : (b)) #define Max(a , b) ((a) > (b) ? (a) : (b))#define ll __int64 #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 327 using namespace std; //freopen("din.txt","r",stdin);int c1[N],c2[N]; int pow2[320];void init(int n){int i,j,k;for (i = 1; i <= 300; ++i){pow2[i] = i*i;}for (i = 0; i <= n; ++i){c1[i] = 1;c2[i] = 0;}for (i = 2; i <= n; ++i){for (j = 0; j <= n; ++j){for (k = 0; k + j <= n; k += pow2[i]){c2[k + j] += c1[j];}}for (j = 0; j <= n; ++j){c1[j] = c2[j];c2[j] = 0;}} } int main(){int n;init(300);while (~scanf("%d",&n)){if (!n) break;printf("%d\n",c1[n]);}return 0; }?
hdu 1085?Holding Bin-Laden Captive!
http://acm.hdu.edu.cn/showproblem.php?pid=1085
題意:
給出三種硬幣,價值分別為1,2,5并給出他們的數量num1,num2,num5問他們不能夠組合出來的的最小價值;
思路:
前邊我們介紹的都是要么是每種數量為1個或者無限個,而這里給出了確定的數量。而且詢問的是不能組合出來額最小值。所以有點繞,但思路還是沒有改變。
我們只要在進行乘法運算時動態的增加括號里的項數即可。最后從小到大枚舉一下就好。。
View Code #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string>#define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a , b) ((a) < (b) ? (a) : (b)) #define Max(a , b) ((a) > (b) ? (a) : (b))#define ll __int64 #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 8007 using namespace std; //freopen("din.txt","r",stdin);int c1[N],c2[N];int main(){int i,j,k;int num1,num2,num5;while (~scanf("%d%d%d",&num1,&num2,&num5)){if (!num1 && !num2 && !num5) break;int MAX = num1 + num2*2 + num5*5;//本身所能表示的最大數for (i = 0; i <= MAX; ++i){c1[i] = 0;c2[i] = 0;}for (i = 0; i <= num1; ++i) c1[i] = 1;//初始化第一個括號里的項//成第二個括號里的項for (j = 0; j <= num1; ++j){for (k = 0; k <= num2*2; k += 2){c2[k + j] += c1[j];}}for (j = 0; j <= num1 + num2*2; ++j){//動態的添加了c1[j] = c2[j];c2[j] = 0;}for (j = 0; j <= num1 + num2*2; ++j){for (k = 0; k <= num5*5; k += 5){c2[k + j] += c1[j];}}for (j = 0; j <= MAX; ++j){c1[j] = c2[j];c2[j] = 0;}for (i = 1; i <= MAX; ++i){if (!c1[i]) break;}printf("%d\n",i);}return 0; }?hdu?Big Event in HDU
http://acm.hdu.edu.cn/showproblem.php?pid=1171
題意:
給出N中不同的設備,分別給出他們的價值以及數量,將這些設備分為兩組,盡量使這兩組的價值相等。輸出各自的價值,若不相等大的在前邊;
思路:
和上邊一樣。。。不多講;
View Code #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string>#define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a , b) ((a) < (b) ? (a) : (b)) #define Max(a , b) ((a) > (b) ? (a) : (b))#define ll __int64 #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 250007 using namespace std; //freopen("din.txt","r",stdin);int c1[N],c2[N];int num[55],val[55];int main(){// freopen("din.txt","r",stdin);int n,i,j,k;while (~scanf("%d",&n)){if (n < 0) break;int MAX = 0;for (i = 0; i < n; ++i){scanf("%d%d",&val[i],&num[i]);MAX += (val[i]*num[i]);}for (i = 0; i <= MAX; ++i){c1[i] = c2[i] = 0;}int len = val[0]*num[0];for (i = 0; i <= len; i += val[0]){c1[i] = 1;}for (i = 1; i < n; ++i){for (j = 0; j <= len; ++j){for (k = 0; k <= val[i]*num[i]; k += val[i]){c2[k + j] += c1[j];}}len += val[i]*num[i];for (j = 0; j <= len; ++j){c1[j] = c2[j];c2[j] = 0;}}int ave = MAX/2;for (i = ave; i >= 0; --i){if (c1[i]) break;}int A = i;int B = MAX - A;printf("%d %d\n",max(A,B),min(A,B));}return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 挂“洋头”卖奶粉,澳优还要欺骗好久
- 下一篇: 关于.net的垃圾回收和大对象处理_标记