1. 程式人生 > >hdu 1028 Ignatius and the Princess III —— 整數劃分(生成函數)

hdu 1028 Ignatius and the Princess III —— 整數劃分(生成函數)

int const 整數劃分 pan 題目 algo while php cst

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1028

整數劃分,每個數可以用無限次;

所以構造 f(x) = (1+x+x2+x3+...)(1+x2+x4+...)(1+x3+x6+...)...(1+xn)

乘起來後的 xn 的系數就是方案數;

用兩個數組做即可,從第一個括號開始一個一個乘,f[i] 表示此時 xi 項的系數,後面每乘過來一個括號,相當於多了一種轉移,所以加上。

代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using
namespace std; int const xn=125; int n,f[xn],t[xn]; int main() { while(scanf("%d",&n)==1) { for(int i=0;i<=n;i++)f[i]=1,t[i]=0; for(int i=2;i<=n;i++) { for(int j=0;j<=n;j++) for(int k=0;k<=n;k+=i)t[j+k]+=f[j]; for(int j=0;j<=n;j++)f[j]=t[j],t[j]=0
; } printf("%d\n",f[n]); } return 0; }

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1398

同上。

代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=305;
int n,f[xn],t[xn];
int main()
{
  n=300;
  for(int i=0
;i<=n;i++)f[i]=1,t[i]=0; for(int i=2;i<=17;i++) { int s=i*i; for(int j=0;j<=n;j++) for(int k=0;j+k<=n;k+=s)t[j+k]+=f[j]; for(int j=0;j<=n;j++)f[j]=t[j],t[j]=0; } while(1) { scanf("%d",&n); if(!n)return 0; printf("%d\n",f[n]); } }

hdu 1028 Ignatius and the Princess III —— 整數劃分(生成函數)