Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
6
Indexable
using System;

class Program
{
    static void Main(string[] args)
    {
        int n = 2;
        int[] A = {3, 2};
        
        int m = 4;
        int[] P = {3, 8, 17, 11};
        // Create array B
        long[] B = new long[n];
        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i <= j; i++)
            {
                B[j] += (long)(A[i] * Math.Pow(2, j - i));
            }
        }
        
        foreach (var val in B)
        {
            Console.WriteLine(val);
        }
        
        long[] BB = new long[5]{3, 2, 6, 8, 7};
        
        target = 17
        
        0 1 2 3 4 5 6 7 8 9 10 11
        1 0 1 1 1 1 1 1 1 0 0  0
        1 0 1 1 0 1 1 1 1 1 0  1 1
        
        
        foreach (int num in BB) {
            if (IsPossible(B, num))
                Console.Write("Yes ");
            else
                Console.Write("No ");
        }
    }
    
    static bool IsPossible(long[] B, int target) {
        bool[] dp = new bool[target + 1];
        dp[0] = true;
        Console.WriteLine("Target: " + target);
        Console.WriteLine("DP INITITAL: " + string.Join(" ", dp));
        foreach (long b in B) {
            Console.WriteLine("b: " + b);
            for (int i = target; i >= b; i--) {
                Console.WriteLine("i: " + i);
                Console.WriteLine("dp[i - b]" + "["+(i-b)+"] = " + dp[i - b]);
                if (dp[i - b]) {
                    dp[i] = true;
                }
            }
        }
        Console.WriteLine("DP AFTER: " + string.Join(" ", dp));
        return dp[target];
    }
}
Editor is loading...
Leave a Comment