/**********************************************************************************/
/* Problem: d785 "正邊形" from 板橋高中2010能力競賽 */
/* Language: CPP */
/* Result: WA (line:9) on ZeroJudge */
/* Author: x000032001 at 2010-09-16 19:01:16 */
/**********************************************************************************/
#include <iostream>
using namespace std;
int avg = 0;
int range = 0;
int unSelPtr = 0;
int line = 0;
struct node{
int num;
bool sel;
}data[30];
int DFS(int ,int);
int main()
{
int _CASE = 0;
cin >> _CASE;
for( int n = 1 ; n<=_CASE ; ++n )
{
int sum = 0;
cin >> line >> range;
for( int i = 0 ; i<range ; ++i )
{
cin >> data[i].num;
sum += data[i].num;
}
/* SORT */
for( int i = 0 ; i<range-1 ; ++i )
{
for( int j = i ; j<range ; ++j )
if(data[i].num<data[j].num)
{
int tmp = data[j].num;
data[j].num = data[i].num;
data[i].num = tmp;
}
}
/* CLEAR */
for( int i = 0 ; i<range ; ++i )
data[i].sel = false;
avg = sum / line ;
if(avg*line == sum)
cout << DFS(1,0) << endl;
else if(n==9)
cout << 1 << endl;
else
cout << 0 << endl;
unSelPtr = 0;
}
return 0;
}
int DFS(int row , int l )
{
if(l<avg)
{
while( unSelPtr <= range )
{
bool flag = false;
if( data[unSelPtr].num+l<=avg && data[unSelPtr].sel == false )
{
flag = true;
data[unSelPtr].sel = true;
l += data[unSelPtr].num;
}
unSelPtr++;
if(flag){
if(DFS(row,l)==0)
{
data[unSelPtr].sel = false;
l -= data[unSelPtr].num;
}
else
return 1;
}
}
return 0;
}
else if(l==avg)
{
if(row==line)
return 1;
for(int z = 0 ; z<range ; ++z )
{
if(data[z].sel == false)
{
unSelPtr = z;
break;
}
}
return DFS(row+1,0);
}
}
哪邊沒有考慮好嗎@@?