HDU - 2079 生成函数基础题
程序员文章站
2022-12-02 15:43:49
依旧是这个生成函数。。怎么这么多水题啊。。#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 = 40+7;...
依旧是这个生成函数。。怎么这么多水题啊。。
#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 = 40+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[11],nm[11];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,k;cin>>n>>k;
memset(f,0,sizeof(f));
for(int i=1;i<=k;i++)
cin>>w[i]>>nm[i];
f[0][0]=1;
for(int i=1;i<=k;i++)
{
for(int j=0;j<=n;j++)f[i&1][j]=0;
for(int k=0;k<=nm[i];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[k&1][n]<<endl;
}
return 0;
}
本文地址:https://blog.csdn.net/bjfu170203101/article/details/107593495