Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
package OnLuyen;

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

public class SumOfIt {
	static int n, t, cnt, check;
	static int[] arr = new int[15];
	static int[][] mt_check;
	static int[] set = new int[15];

	static void input(Scanner sc) {
		t = sc.nextInt();
		n = sc.nextInt();
		int sum = 0;
		check = 0;
		mt_check = new int[n*n][n*n];
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
			sum += arr[i];
		}
		if (sum < t) {
			check = 1;
		}
	}

	static void backTrack(int sum, int dem, int i_before) {
		if (sum >= t) {
			if (sum == t) {
//				System.out.println("set");
//				for (int i = 0; i < n; i++) {
//					System.out.print(set[i] + " ");
//				}
//				System.out.println("\nmt_check " + cnt);
//				for (int i = 0; i < cnt; i++) {
//					for (int j = 0; j < n; j++) {
//						System.out.print(mt_check[i][j] + " ");
//					}
//					System.out.println();
//				}
//				System.out.println();
				if (cnt == 0) {
					for (int i = 0; i < dem; i++) {
						mt_check[cnt][i] = set[i];
					}
					cnt++;
				} else {
					int flag = 0;
					for (int i = 0; i < cnt; i++) {
						flag = 0;
						for (int j = 0; j < dem; j++) {
							if (mt_check[i][j] == set[j]) {
								flag++;
							}
						}
						if (flag == dem) {
							flag = -1;
							break;
						}
					}
					if (flag != -1) {
						for (int i = 0; i < dem; i++) {
							mt_check[cnt][i] = set[i];
						}
						cnt++;
					}
				}
			}
			return;
		}
		for (int i = 0; i < n; i++) {
			if (i > i_before) {
				set[dem] = arr[i];
				backTrack(sum + arr[i], dem + 1, i);
			}
		}
	}

	static void solve(int tc) {
		if (check == 1) {
			System.out.println(-1);
		} else {
			cnt = 0;
			backTrack(0, 0, -1);
			if (cnt == 0) {
				System.out.println(-1);
			} else {

				System.out.println(cnt);
			}

		}

	}

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("SumOfIt.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int i = 0; i < T; i++) {
			input(sc);
			solve(i + 1);
		}
	}
}