File size: 963 Bytes
c4b0eef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/*
>>~~ UVa Online Judge ACM Problem Solution ~~<<
ID: 10036
Name: Divisibility
Problem: https://onlinejudge.org/external/100/10036.pdf
Language: C++
Author: Arash Shakery
Email: [email protected]
*/
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
int d[10001],k,n;
int saved[10001][101];
bool smod(int ind,int m){
if(m<0)m+=k;
if(ind==n-1)
return d[ind]%k==m;
if(saved[ind][m]!=-1)return saved[ind][m];
return saved[ind][m]=( smod(ind+1,(m+d[ind]%k)%k)||
smod(ind+1,m-d[ind]%k));
}
int main(){
int T,i,j;
cin>>T;
while(T--){
cin>>n>>k;
for(i=0;i<n;i++){
cin>>d[i];
if(d[i]<0)d[i]=-d[i];
}
for(i=0;i<=n;i++)
for(j=0;j<=k;j++)
saved[i][j]=-1;
if(smod(0,0))
puts("Divisible");
else
puts("Not divisible");
}
} |