Project Euler #78: Coin partitions

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    C++

    #include <bits/stdc++.h>
    using namespace std;
    const long long MOD = 1000000007;
    
    int MAX = 60000;
    
    int main() 
    {
        int t;
        cin >> t;
    
        vector<long long> pent;
        unordered_set<long long> nums;
        
        for(int i = 1; i < 10000; i++)
        {
            int p = i * (3*i-1) / 2;
            
            if(!nums.count(p)) 
            {
                pent.push_back(p);
                nums.insert(p);
            }                
            p = -i * (3*(-i)-1)/2;
            
            if(!nums.count(p))
            {
                pent.push_back(p);
                nums.insert(p);
            }        
        }
        sort(pent.begin(), pent.end());
    
        long long DP[60010] = {0};
        DP[0] = 1;
        
        for(int i = 1; i <= MAX; i++)
        {              
            bool add = 0;
            long long num = 0;
            long long p = 0;
    
            for(int j = 0; p <= i; j++)
            {                
                if(add) DP[i] = (DP[i] + num) % MOD;
                else DP[i] = (DP[i]) - num;
    
                if(DP[i] < 0) DP[i] += MOD;
    
                p = pent[j];
                num = DP[i - p] % MOD;
    
                if(j % 2 == 0) add = !add;
            }
            DP[i] = DP[i] % MOD;        
        }
        while(t--)
        {
            int n;
            cin >> n;
            
            cout << DP[n] % 1000000007 << endl;
        }
        return 0;
    }
    
  • + 0 comments

    Try Coin change algo in node js

  • + 0 comments

    If you get stuck, don't forget to check out this video. The best explanation you need to know before solving this problem.

    https://youtu.be/iJ8pnCO0nTY

  • + 0 comments

    Can't seem to figure out why I'm getting the wrong answer for 6 and 7. I'm not timing out. I'm using the recursive p[n] formula using the pentagonal numbers. Can anyone tell me is this the solution for the following test case (assume line breaks rather than spaces, it just doesn't post that way when I load the comment) 3 1000 10000 60000

    709496666 17783467 895843280

  • + 0 comments

    Keyword for this problem is integer partition algorithm.

    There are many unreliable codes in the internet, so you should choose the one that has time = O(n^1.5)