Sort by

recency

|

23 Discussions

|

  • + 1 comment

    5 line Python, 100%

    Works for arbitrary number of coins with arbitrary values. Standard dp, use memoization or @cache python builtin.

    def solve(n, coins, v=[1, 2, 5, 10]):
        @cache
        def dp(s, p):
            if s==0 or p==0:
                return 1 if s<=coins[p] else 0
            return sum(dp(s-i*v[p], p-1) for i in range(min(coins[p], s//v[p])+1))
        return dp(n, len(coins))
    
  • + 0 comments

    Python small solution.

    def f(n,x,i,z,dp):
        if n==0:return 1
        if i>3 :return 0
        if (n,i) in dp:return dp[n,i]
        ans=0
        for j in range(x[i]+1):
            if n-j*z[i]<0:break
            ans+=f(n-j*z[i],x,i+1,z,dp)
        dp[n,i]=ans
        return ans
    def solve(n, x):
        z=(1,2,5,10)
        return f(n,x,0,z,{})
    
  • + 4 comments

    SHORTEST CODE TO THIS QUESTION

    int min(int a, int b) {int c=a; if(a>b) c=b; return c; }

    int main() { int t,n,a,b,c,d,i,j,k,l; scanf("%d",&t);

    while(t--)
    {int x=0;
        scanf("%d %d %d %d %d",&n,&a,&b,&c,&d);
    
        for(i=min(d,n/10);i>=0;i--)
        {
            for(j=min((n-10*i)/5,c);j>=0;j--)
            {
                for(k=min(b,(n-10*i-5*j)/2);k>=0;k--)
                {
                    for(l=min(a,(n-10*i-5*j-2*k));l>=0;l--)
                    {
                        if(10*i+5*j+2*k+l==n)
                        {
                            x++;
                            break;
                        }
                    }
                }
            }
        }
        printf("%d\n",x);
    }
    return 0;
    

    }

  • + 0 comments

    Solution for C++

    #include<bits/stdc++.h>
    typedef long long ll;
    using namespace std;
    const int modd=1e9;
    int T,n,a,b,c,d;
    ll ans;
    int main() {
    	ios::sync_with_stdio(false);
    	cin>>T;
    	while(T--) {
    		cin>>n,ans=0;
    		cin>>a>>b>>c>>d;
    		for(int i=0; i<=min(d,n/10); i++)
    			for(int j=0; j<=min(c,n/5); j++)
    				for(int k=0; k<=min(b,n/2); k++)
    					if(n-10*i-5*j-2*k<=a&&n-10*i-5*j-2*k>=0) ans++;
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • + 1 comment

    The editorial optimization 3 is wrong and bad way . The optimization 4 is good approach . Suppose u have 0 1 coins , 3 2 coins ,2 5 coins , 1 10 coins. Then U can't make N if(13) But as rule of optimization of 3

    (2*i+5*j+10*k>=(N-A)) count++;

    many times 2*i+5*j+10*k this part will be greater than (13-0) . So what rubbish solution that is !!!

    include

    using namespace std;

    int main() { int T,N,A,B,C,D; cin>>T; while(T--) { cin>>N; cin>>A>>B>>C>>D; long long ways[N+1]={0}; for(int i=0;i<=A;i++) { for(int j=0;j<=B&&(i+2*j)<=N;j++) { ways[(i+2*j)]++; } } long long ans=0; for(int i=0;i<=C&&(i*5)<=N;i++) { for(int j=0;j<=D&&((i*5)+(10*j))<=N;j++) { ans+=ways[N-((i*5)+(10*j))]; } } cout<