| | |
number of ways to..ehh, i hate counting ways
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 86
Reputation:
Solved Threads: 5
okay, this is the problem (variation of the knapsack coins problem): you've got 4 coin types:
1 cent, 2 cents, 5 cents and a quarter...infinite amount of each. I'm supposed to find the number of ways in which the coins can be arranged to form the sum of some integer n...
well i... had an idea but it proved pretty wrong.
my idea was to check all the possible ways of forming n with two other numbers, and save the solution as number of needed for the first times number of needed for the second, the problem is it is not always the case, as you can get large numbers cause of repetitions, for example 6:
2 + 4
two ways to make 2 ( 2, 1+1 )
three ways to make 4( 2+2, 1+1+1+1, 2+1 )
multiplied... 6 ways? nope...5
1+1+1+1+1+1
2+1+1+1+1
2+2+1+1
2+2+2
5+1... damn...
1 cent, 2 cents, 5 cents and a quarter...infinite amount of each. I'm supposed to find the number of ways in which the coins can be arranged to form the sum of some integer n...
well i... had an idea but it proved pretty wrong.
my idea was to check all the possible ways of forming n with two other numbers, and save the solution as number of needed for the first times number of needed for the second, the problem is it is not always the case, as you can get large numbers cause of repetitions, for example 6:
2 + 4
two ways to make 2 ( 2, 1+1 )
three ways to make 4( 2+2, 1+1+1+1, 2+1 )
multiplied... 6 ways? nope...5
1+1+1+1+1+1
2+1+1+1+1
2+2+1+1
2+2+2
5+1... damn...
Nope. You have troubles with integer arithmetics.
For two cent coin: possible 0 or 1.
1st case: 3 - 0 = 3, for 1 cent coins 1 solution (1+1+1); 1 solution subtotal
2nd case: 3 - 2 = 1, for 1 cent coin 1 solution (1); 1 solution subtotal
1 + 1 = 2 - 2 solutions total, that's OK.
It is understandable that you hate counting
...
For two cent coin: possible 0 or 1.
1st case: 3 - 0 = 3, for 1 cent coins 1 solution (1+1+1); 1 solution subtotal
2nd case: 3 - 2 = 1, for 1 cent coin 1 solution (1); 1 solution subtotal
1 + 1 = 2 - 2 solutions total, that's OK.
It is understandable that you hate counting
... Last edited by ArkM; Oct 8th, 2008 at 6:37 pm.
•
•
Join Date: Jun 2008
Posts: 86
Reputation:
Solved Threads: 5
hmm, well now i've implemented it, but it's so damn slow... it should work fast for 100k atleast... here's my implementation:
i guess my implementation is stupid in some way... or can it run no faster?
c++ Syntax (Toggle Plain Text)
#include <cstdio> #include <cstring> #include <algorithm> #define MOD 100000 using namespace std; const int coins[] = {1, 2, 5, 25}; int n, dp[100000][5]; int rec(int n, int m) { int ret = 0; if(dp[n][m]) return dp[n][m]; if(m == 0 || n == 0) return (dp[n][m] = 1); if(n < coins[m]) return (rec(n, m-1)); for(int i = 0; i*coins[m] <= n; ++i) { ret = ((ret%MOD) + (rec(n-i*coins[m], m-1)%MOD))%MOD; } return (dp[n][m] = ret); } int main( void ) { scanf("%d", &n); memset(dp, 0, sizeof dp); printf("%d\n", rec(n, 3)%MOD); return 0; }
Last edited by gregorynoob; Oct 9th, 2008 at 3:54 pm.
Sorry, I have no time to inspect your implementation carefully, but no need in arrays in this algorithm (except coins which must be a parameter of a recursive function). No need in <algorithm> header or MOD macros too.
It must be a very fast (and short) code...
It must be a very fast (and short) code...
Last edited by ArkM; Oct 9th, 2008 at 8:55 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need some guidance/pointers/tips/etc regarding codes.
- Next Thread: How to use typedef for the template structure? What its syntax?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






