You are given a sequence of numbers a1, a2, ..., an, and a number m.
Check if it is possible to choose a non-empty subsequence aij 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 a1, a2, ..., an (0 ≤ ai ≤ 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.
Sample test(s)
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
Note
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.
In the third sample test you need to choose two numbers 3 on the ends.
Check if it is possible to choose a non-empty subsequence aij 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 a1, a2, ..., an (0 ≤ ai ≤ 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.
Sample test(s)
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
Note
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.
In the third sample test you need to choose two numbers 3 on the ends.
In the fourth sample test you can take the whole subsequence.
更新----我是傻逼,我是傻逼,我是傻逼,看一个知识点总是粗略的看一下,而后的原理自己都搞的很少。
鸽巢就是告诉了我们n>=m时是一定有的,而我呢?更新看代码吧,,,看过别人的代码
---------完结
也许是自己菜吧,反正觉得这是一道好题。
首先当n>=m时我们直接套用抽屉原理判断,对于n<m的现象自己最先没有想到,看了别人的题解才明白了需要用dp。
dp[i][j]记录的是i个数里,和为j的数字有没有出现过,最后判断dp[n][c]是否等于1
这是自己直接套用的抽屉原理模板,应该可以改简单的,明天再看一下。
/* ***********************************************
Author :Mosu
Created Time :2015/10/10 18:30:45
File Name :\Users\Mosu\Desktop\project\BC.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff
int neigb[1000009];
int dp[1005][1005];
long long S;
struct Remnant{
int h,r;
}R[1000009];
bool cmp(const Remnant &a,const Remnant &b){
if(a.r==b.r)
return a.h<b.h;
return a.r<b.r;
}
int main(){
int c,n,k,h;
while(scanf("%d%d",&n,&c)!=EOF){
for(int i=0;i<n;i++)
scanf("%d",&neigb[i]);
if(n>c){
k=-1;
S=0;
for(int i=0;i<n;i++){
//scanf("%d",&neigb[i]);
S+=neigb[i];
R[i].r=S%c;
R[i].h=i+1;
if(k==-1&&R[i].r==0)
k=i;
}
if(k==-1){
sort(R,R+n,cmp);
for(int i=0;i<n-1;i++){
if(k==-1&&R[i].r==R[i+1].r){
k=R[i].h;
h=R[i+1].h;
}
}
if(k==-1)
printf("NO\n");
else{
printf("YES\n");
}
}
else{
printf("YES\n");
}
}
else{
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=c;j++)
dp[i][j]=(dp[i-1][(j+neigb[i-1])%c]||dp[i-1][j]);
if(dp[n][c])
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}
更新:
/* ***********************************************
Author :Mosu
Created Time :2015/10/10 18:30:45
File Name :\Users\Mosu\Desktop\project\BC.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff
int dp[1009][1009];
int s[1000009];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2){
int k=1;
for(int i=0;i<n;i++){
scanf("%d",&s[i]);
if(s[i]%m==0)
k=0;
}
if(n>m||k==0){
printf("YES\n");
continue;
}
sizeof(dp,0,sizeof(dp));
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
dp[i][j]=(dp[i-1][(j+s[i-1])%m]||dp[i-1][j]);
if(dp[n][m])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}