Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
4
Indexable
package SumItUp;

import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Solution {
    static int n,s,m,ans;
    static int[] a,d,len;
    static int[][] ds;
    public static void main(String[] args) throws Exception {
        System.setIn(new FileInputStream("input.txt"));
        System.setOut(new PrintStream("output.txt"));
        // final long startTime = System.currentTimeMillis();
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        for (int t = 1; t <= tc; t++) {
            s = sc.nextInt();
            n = sc.nextInt();
            ds = new int[1000][n];
            d = new int[n];
            len = new int[1000];
            a = new int[n];
            for(int i=0;i<n;i++){
                a[i] = sc.nextInt();
            }
            m = 0;
            BT(0,0);
            ans = m;
            for(int i=0;i<m;i++){
                for(int j=i+1;j<m;j++){
                    if(len[i]==len[j]&&same(ds[i], ds[j], len[i])){
                        ans--;
                        break;
                    }
                }
            }
            if(ans==0){
                System.out.println("#"+t+" -1");
            }
            else{
                System.out.println("#"+t+" "+ans);
            }
        }
    }
    static void BT(int k,int current_sum){
        if(k==n){
            if(current_sum==s){
                for(int i=0;i<n;i++){
                    if(d[i]==1){
                        ds[m][len[m]++] = a[i];
                    }
                }
                m++;
            }
            return;
        }
        if(current_sum>s) return;
        BT(k+1, current_sum);
        d[k] = 1;
        BT(k+1,current_sum+a[k]);
        d[k] = 0;
    }
    static boolean same(int[] a1,int[] a2,int length){
        for(int i=0;i<length;i++){
            if(a1[i]!=a2[i]) return false;
        }
        return true;
    }
}