import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m="";
int cases=Integer.parseInt(br.readLine());
for (int i = 0; i < cases; i++) {
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int num=Integer.parseInt(st.nextToken());
int rem=0;
st=new StringTokenizer(br.readLine());
int temp[]=new int[n];
short[][]arr=new short[n+1][101];
for(int j=0;j<n;j++){
temp[j]=Math.abs(Integer.parseInt(st.nextToken()));
}
for(int j=0;j<n;j++){
for(int l=0;l<101;l++){
arr[j][l]=2;
}
}
if(sum(temp, 0, num, 0,arr)==0){
sb.append("Divisible\n");
}else{
sb.append("Not divisible\n");
}
}
System.out.print(sb);
}
static short sum(int[]nums,int i,int k,int sum,short[][] arr){
if(i==nums.length){
if(sum%k==0){
return 0;
}
return 1;
}
int mod=sum%k;
if(mod<0)
mod+=k;
if(arr[i][mod]!=2){
return arr[i][mod%k];
}
arr[i][mod]=(short) Math.min(sum(nums,i+1,k,(sum+nums[i]),arr),sum(nums,i+1,k,(sum-nums[i]),arr));
return arr[i][mod];
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m="";
int cases=Integer.parseInt(br.readLine());
for (int i = 0; i < cases; i++) {
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int num=Integer.parseInt(st.nextToken());
int rem=0;
st=new StringTokenizer(br.readLine());
int temp[]=new int[n];
short[][]arr=new short[n+1][101];
for(int j=0;j<n;j++){
temp[j]=Math.abs(Integer.parseInt(st.nextToken()));
}
for(int j=0;j<n;j++){
for(int l=0;l<101;l++){
arr[j][l]=2;
}
}
if(sum(temp, 0, num, 0,arr)==0){
sb.append("Divisible\n");
}else{
sb.append("Not divisible\n");
}
}
System.out.print(sb);
}
static short sum(int[]nums,int i,int k,int sum,short[][] arr){
if(i==nums.length){
if(sum%k==0){
return 0;
}
return 1;
}
int mod=sum%k;
if(mod<0)
mod+=k;
if(arr[i][mod]!=2){
return arr[i][mod%k];
}
arr[i][mod]=(short) Math.min(sum(nums,i+1,k,(sum+nums[i]),arr),sum(nums,i+1,k,(sum-nums[i]),arr));
return arr[i][mod];
}
}
No comments:
Post a Comment