DP: Coin Change

  • + 0 comments
    /*
     * 2024 ^_^
     *ThinhNguyen97
     * 
     */
    
    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Set;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Queue;
    import java.util.Scanner;
    import java.util.Stack;
    
    
    
    
    
    
    public class Solve_problems 
    {
        static int n;//so luong dong xu
        static int[] coins=new int[1001];//luu menh gia cac dong xu
        static long[] dp=new long[1000001];
        
        static long solve(int S)
        {
            dp[0]=1;//so tien la 0 tuc la co 1 cach lua chon
            
            for(int j=0;j<n;j++)//duyet het mang
                for(int i=coins[j];i<=S;i++)
                {
                    dp[i]+=dp[i-coins[j]];
                    //dp[i]%=1000000007;
                }
                    
            
            return dp[S];
        }
    
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            ////////
            int S=sc.nextInt();//so tien can tao ra
            n=sc.nextInt();
            for(int i=0;i<n;i++)
                coins[i]=sc.nextInt();
            System.out.println(solve(S));
            
            // Close the Scanner instance
            sc.close();
        }
    }