hi how to solve the problem change from the spoj (http://www.spoj.com/problems/TPC07/)
i read concrete mathematics chapter 7 about coin change. but i can't able to understand can anybody help me to get idea about solving this problem ?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
hi how to solve the problem change from the spoj (http://www.spoj.com/problems/TPC07/)
i read concrete mathematics chapter 7 about coin change. but i can't able to understand can anybody help me to get idea about solving this problem ?
Name |
---|
This is a dynamic programming problem. First learn something about it, then approach this problem
The upper limit of n is quite large, where 1 ≤ n ≤ 1000000000. How to cater for this with DP?
Probably there is a more general formula?
Well the idea is for dp, but you need to use matrices to run it fast, in time (50 is the maximal value of a coin).
I can explain more if you want.
It can be solved in O(1).
We want to count the number of solutions of x1 + 5x2 + 10x3 + 25x4 + 50x5 = N.
x1 must be congruent with n%5, so is equivalent to count the number of solutions of x1 + x2 + 2x3 + 5x4 + 10x5 = ⌊ N / 5⌋ = M
Let's suppose (i.e xi = 5ai + ri), for i ≤ 3.
We can brute force all possibilities of ri, now we have to count the solutions of
a1 + a2 + 2a3 + x4 + 2x5 = ⌊ (M - r1 - r2 - 2r3) / 5⌋ = P
Grouping, (a1 + a2 + x4) + 2(a3 + x5) = P which can be solved by