Question: Can I add a plus or minus sign between each adjacent two items of an integer sequence so that the final result can be divisible by a given integer k <= 100.
DP [I] [J] indicates that the number of I is taken as the Boolean value where the remainder K is J.
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 20000 5 using namespace std; 6 7 int t; 8 int n,k; 9 int a[maxn];10 bool flag;11 bool dp[maxn][110];12 13 int main()14 {15 scanf("%d",&t);16 while(t--)17 {18 memset(dp,false,sizeof(dp));19 scanf("%d%d",&n,&k);20 for(int i=0; i<n; i++)21 {22 scanf("%d",&a[i]);23 }24 dp[0][abs(a[0])%k]=true;25 for(int i=1; i<n; i++)26 {27 for(int j=0; j<k; j++)28 {29 if(dp[i-1][j])30 {31 dp[i][(j+k+abs(a[i]))%k]=true;32 dp[i][((j+k-abs(a[i]))%k+k)%k]=true;33 }34 }35 }36 if(dp[n-1][0]) printf("Divisible\n");37 else printf("Not divisible\n");38 }39 return 0;40 }View code
Ultraviolet A 10036 Problem C: divisibility