题目连接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=977
利用一个二维数组,,以为表示数字的个数,,二维表示当前对k取余能到达的状态
最后判断一下n个数能不能到达0的状态就好啦
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1e10+7
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
vi a;
int T;
int n,k;
int dp[10005][105];
int t;
int main()
{
ios::sync_with_stdio(false);
cin >>T;
while(T--)
{
memset(dp,0,sizeof(dp));
a.clear();
a.push_back(0);
cin >> n>>k;
rep(i,1,n)
{
cin >> t;
t%=k;
a.push_back(t);
}
int tt = a[1]%k;
tt += k;
tt%=k;
dp[1][tt]=1;
for(int i = 2;i <=n;i++)
{
for(int j=0;j<k;j++)
{
if(dp[i-1][j])
{
tt = (j+a[i])%k;
tt += k;
tt%=k;
if(!dp[i][tt])
{
dp[i][tt]=1;
}
tt = (j-a[i])%k;
tt += k;
tt%=k;
if(!dp[i][tt])
{
dp[i][tt]=1;
}
}
}
}
/*for(int i=1;i<=n;i++)
{
for(int j=0;j<k;j++)
cout << dp[i][j]<<" ";
cout <<endl;
}
*/
if(dp[n][0]==1)
{
cout <<"Divisible"<<endl;
}
else
{
cout <<"Not divisible"<<endl;
}
}
return 0;
}