Problem
Test instructions
Give you the number of n (1≤ n ≤106) A1. An (0≤ ai ≤109), give you M ( 2≤ m ≤103) If the number of n is a subset of the and can be divisible by M, The output is yes, otherwise no.
Analysis
In two cases:
When N>m, S[i] said before I money total money, s[i]%m value of 0 to m-1, by the principle of the drawer, s[i] must have repeated, if repeat is s[l] and s[r], then s[r]-s[l] that is l+1 to r these money Plus is the multiple of M. So the answer is yes.
When n≤m, we use dp[i][j]==1 to denote that the former I money can get the price of M remainder to J, Dp[i][a%m]=1;dp[i][j]=max (dp[i-1][j],dp[i-1][(m+j-a[i]%m)%m]) (J!=a%m, 0≤J<M), that is, if the Zhangxian can get the price of M-remainder is J. Once you get dp[i][0]==1, the answer is yes and you don't have to. It was such a waste of space at the time, and could not open that large array, I maximum of 106. Just save the last time and this once is enough.
The other is a direct push, based on the remainder that can be obtained before the remainder of this round is introduced.
Code DP Code
#include <cstdio> #include <algorithm>using namespace Std;int n,m,a;int dp[2][1005];int main () { scanf ( "%d%d", &n,&m); if (n>m) dp[0][0]=1; else for (int i=1, i<=n; i++) {for (int j=0; j<m; j + +) dp[1][j]=0; scanf ("%d", &a); Dp[1][a%m]=1; for (int j=0; j<m; j + +) { if (dp[1][0]) break; if (j!=a%m) Dp[1][j]=max (dp[0][j],dp[0][(m+j-a%m)%m]); } for (int j=0; j<m; j + +) dp[0][j]=dp[1][j]; } if (Dp[0][0]) printf ("YES"); else printf ("NO"); return 0;}
Another type of code
#include <cstdio> #include <cstring>int n,m;int a;int mark[1005],t[1005];int Main () { scanf ("%d%d", &N,&M); if (n>m) t[0]=1; else while (n--) { scanf ("%d", &a); memset (Mark,0,sizeof (Mark)); Mark[a%m]=1; for (int i=0, i<m; i++) { if (t[0]) break; if (T[i]) mark[(a%m+i)%m]=1; If you can get%m=i price before, you can now get (a%m+i)%m price} for (int i=0; i<m; i++) { if (t[i]==0) t[i]=mark[i]; Save this round. cannot be merged together with write } if (t[0]) printf ("yes\n"); else printf ("no\n"); return 0;}
"Codeforces 577B" Modulo Sum