Coin shopping, incorrect printing of coins on nails
2016.1.27
Question description |
There are currently four types of coins with different nominal values: ci (I = 1, 2, 4 ). Someone goes to the store to buy things, and goes to tot. Each time they bring di ci coins, they buy goods worth si. How many payment methods are there each time. |
Input |
The first row contains five numbers, namely c1, c2, c3, c4, and tot. Next there are five numbers in each row, the fifth number in the I + 1 row is the number of four coins taken by the I shopping and the value of the purchased goods (d1, d2, d3, d4, s ). Each row is separated by a space. |
Output |
One row, including the number of tots, is the number of methods for each payment in turn. Separate the two numbers with a space. |
Input example |
1 2 5 10 2 3 2 3 1 10 1000 2 2 900 |
Output example |
4 27 |
Other Instructions |
Data range: 0 <di, s <= 100000,0 <tot <= 1000, and the data nominal value cannot exceed 20. The data is guaranteed to have at least one payment method each time. |
Hzwer's principle Solution
First, the method to reach the value of s is preprocessed, regardless of whether the coin exceeds the limit. This is just a simple backpack.
The answer is to deduct at least one coin from it that exceeds the limit.
There are: at least one coin exceeded = first coin exceeded + second coin exceeded + third coin exceeded + fourth coin exceeded-first and second exceeded-first and third exceeded- both the first and fourth types exceed the upper limit-both the second and third types exceed the upper limit-both the second and fourth types exceed the upper limit + ...... -The quota of all four types is exceeded.
Then, it is like a pressure rejection.
If the first coin is exceeded, f [s-(d [1] + 1) * c [1] is the number of schemes, because the first coin is used once. And so on
Pay attention to the situation where sum exceeds s
AC code:
# Include <iostream> using namespace std; int c [5], tot, d [5], s, ct, sum, flag; long f [100005], ans; int main () {for (int I = 1; I <= 4; I ++) scanf ("% d", & c [I]); scanf ("% d", & tot); f [0] = 1; for (int I = 1; I <= 4; I ++) {for (int j = c [I]; j <= 100000; j ++) {f [j] + = f [j-c [I];} while (tot --) {for (int I = 1; I <= 4; I ++) scanf ("% d", & d [I]); scanf ("% d", & s); ans = 0; for (int I = (1 <4)-1; I> = 0; I --) {ct = 0; sum = 0; for (int j = 3; j> = 0; j --) {if (1 <j & I) ct ++, sum + = (d [j + 1] + 1) * c [j + 1];} if (s <sum) continue; if (ct & 1) ans-= f [s-sum]; else ans + = f [s-sum];} if (flag) cout <""; flag = 1; cout <ans ;}}View Code