洛谷P1450 HAOI2008 硬幣購物

2022-04-01 13:18:19 字數 1232 閱讀 4106

硬幣購物一共有4種硬幣。面值分別為c1,c2,c3,c4。某人去商店買東西,去了tot次。每次帶di枚ci硬幣,買si的價值的東西。請問每次有多少種付款方法。

輸入格式:

第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s

輸出格式:

每次的方法數

輸入樣例#1:

1 2 5 10 2

3 2 3 1 10

1000 2 2 2 900

輸出樣例#1:

4

27

di,s<=100000

tot<=1000

[haoi2008]

#include#include

#include

#include

using

namespace

std;

int c[5],t,cnt[5

],s;

int sum[1000000

],num,ans,now;

intmain()

for(int i=0;i<=cnt[3];i++)

for(int j=0;j<=cnt[4];j++)

printf(

"%d\n

",ans);}}

10分 meet in the middle

/*

不要忘了開longlong

*/#include

#include

#include

#ifdef win32

#define pll "%i64d"

#else

#define pll "%lld"

#endif

using

namespace

std;

int c[5],d[5

],t;

long

long f[100010

],ans;

void dfs(int now,int cnt,int

sum)

dfs(now+1

,cnt,sum);

if(sum-(d[now]+1)*c[now]>=0

) dfs(now+1,cnt+1,sum-(d[now]+1)*c[now]);

}int

main()

}

100分 完全揹包+容斥原理

洛谷P1450 HAOI2008 硬幣購物

硬幣購物一共有4種硬幣。面值分別為c1,c2,c3,c4。某人去商店買東西,去了tot次。每次帶di枚ci硬幣,買si的價值的東西。請問每次有多少種付款方法。輸入格式 第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s 輸出格式 每次的方法數 輸入樣例 1 複製1 2 ...

P1450 HAOI2008 硬幣購物

完全揹包和容斥原理的結合 可以看乙個區間相減的 其實是錯的,但是好理解 求 2,3 2,3 2,3 轉換為求 2,3,2,3 begin 2,infty 3,infty rightarrow 2,3 end 2,3 2,3 f s f s f s 表示買s ss的東西有多少種方案 在每種錢都有無限的...

P1450 HAOI2008 硬幣購物

p1450 haoi2008 硬幣購物 完全揹包 容斥 真是秒呀 方案數統計。如果無法直接計算出來,可以嘗試使用容斥原理進行拼湊。你看,這個題中的對答案有影響的元素只有4個。2 n 次方的容斥完全可以做 我們可以使用所有的方案數,減去乙個硬幣不合法的方案數,加上兩個硬幣不合法的方案數,然後如此搞一搞...