欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

[HDU-1398] 生成函数基础题

程序员文章站 2022-04-01 13:05:05
还是经典生成函数:(1+x^i)#include#include#include#includeusing namespace std;typedef long long ll;#define ls (o<<1)#define rs (o<<1|1)#define pb push_backconst int M = 300+7;/...

还是经典生成函数:

(1+x^i)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back

const int M = 300+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int f[2][M];
int w[21];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	for(int i=1;i<=17;i++)w[i]=i*i;
  	while(1)
	{
		int n;cin>>n;
		if(n<=0)break;
		memset(f,0,sizeof(f));
	  	f[0][0]=1;
	  	for(int i=1;i<=17;i++)
	  	{
	  		for(int j=0;j<=n;j++)f[i&1][j]=0;
	  		for(int k=0;k*w[i]<=n;k++)//当前种类硬币选几个 
	  			for(int x=0;x+k*w[i]<=n;x++)
	  				f[i&1][x+k*w[i]]+=f[(i-1)&1][x];
					  //cout<<i<<" "<<x<<" "<<k<<" "<<x+k*w[i]<<"  - -   "<<f[i&1][x+k*w[i]]<<"   - > "<<f[(i-1)&1][x]<<endl;;
		}
		cout<<f[17&1][n]<<endl;
	}
	return 0;
}

 

本文地址:https://blog.csdn.net/bjfu170203101/article/details/107593420