Codeforces 577B. Modulo Sum (数学 + DP)

Description
You are given a sequence of numbers a 1, a 2, …, a n, and a number m.

Check if it is possible to choose a non-empty subsequence a i j such that the sum of numbers in this subsequence is divisible by m.

Input
The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.

The second line contains n integers a 1, a 2, …, a n (0 ≤ a i ≤ 109).

Output
In the single line print either “YES” (without the quotes) if there exists the sought subsequence, or “NO” (without the quotes), if such subsequence doesn’t exist.

Examples
Input
3 5
1 2 3
Output
YES

Input
1 6
5
Output
NO

Input
4 6
3 1 1 3
Output
YES

Input
6 6
5 5 5 5 5 5
Output
YES

Solution

Code

int a[maxn];
bool dp[maxn][2];
int main() {
int n,m;scanf("%d%d",&n,&m);
for(int i = 1;i <= n;++i){
scanf("%d",&a[i]); a[i]%=m;
}
if(n > m) {printf("YES\n");return 0;}
int op = 0;
for(int i = 1;i <= n;++i){
op ^= 1;
for(int j = 0;j < m;++j) dp[j][op] = dp[j][op^1];
dp[a[i]][op] = true;
for(int j = 0;j < m;++j){
if(dp[j][op^1]) dp[(j+a[i])%m][op] = true;
}
}
if(dp[0][op]) printf("YES\n"); else printf("NO\n");
return 0;
}

09-22

09-21 1839
01-11 256
09-21 367
05-22 42
01-29 120