Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
1
Indexable
#include <iostream>
using namespace std;

int arr[12];
int M, N;
int ctt[12];
int visited[12];
int Ar[12][1001][12];

void reset1()
{
	for (int i = 0; i < 12; i++)
	{
		arr[i] = 0;
		ctt[i] = 0;
		visited[i] = 0;
	}
}

void reset2()
{
	for (int i = 0; i < 12; i++)
	{
		for (int j = 0; j < 1001; j++)
		{
			for (int t = 0; t < 12; t++)
			{
				Ar[i][j][t] = 0;
			}
		}
	}
}

void addArr(int dem)
{
	// day gia tri vao mang tong luu de so sanh
	int k = 0;
	for (int i = 0; i < N; i++)
	{
		if (arr[i] == 0)
		{
			Ar[dem][ctt[dem]][k] = visited[i];
			k++;
		}
	}
}

bool check(int dem)
{
	// check xem co trung nhau khong
	int ars[12] = { 0 };
	int k = 0;
	for (int i = 0; i < N; i++)
	{
		if (arr[i] == 0)
		{
			ars[k] = visited[i];
			k++;
		}
	}
	for (int i = 0; i < ctt[dem]; i++)
	{
		int dk = 0;
		for (int j = 0; j < k; j++)
		{
			if (Ar[dem][i][j] != ars[j])
			{
				dk = 1;
				break;
			}
		}
		if (dk == 0)
		{
			return false;
		}
	}
	return true;
}

void sum_M(int k, int& cnt, int dem, int sum)
{
	if (k == N + 1)
	{
		return;
	}
	if (sum >= M)
	{
		if (sum == M)
		{
			if (ctt[dem] == 0)
			{
				cnt++;
				addArr(dem);
				ctt[dem]++;
			}
			else
			{
				// checjk xem xo trung nhau khong
				if (check(dem))
				{
					cnt++;
					addArr(dem);
					ctt[dem]++;
				}
			}
		}
		return;
	}
	for (int i = 0; i <= 1; i++)
	{
		if (i == 0)
		{
			sum_M(k + 1, cnt, dem , sum);
		}
		else
		{
			int t = arr[k];
			arr[k] = 0;
			sum_M(k + 1, cnt, dem + 1, sum + t);
			arr[k] = t;
		}
	}
}

int main()
{
	int testcase;
	cin >> testcase;
	for (int tc = 1; tc <= testcase; tc++)
	{
		cin >> M >> N;
		reset1();
		reset2();
		for (int i = 0; i < N; i++)
		{
			cin >> arr[i];
			visited[i] = arr[i];
		}
		int cnt = 0;
		sum_M(0, cnt, 0, 0);
		for (int i = 0; i < N - 1; i++)
		{
			for (int j = 0; j < N; j++)
			{
				if (arr[i] > arr[j])
					swap(arr[i], arr[j]);
			}
		}
		if (cnt != 0)
		{
			cout << "#" << tc << " " << cnt << endl;
		}
		else
			cout << "#" << tc << " " << -1 << endl;
	}
}