Topic Link:
Http://codeforces.com/problemset/problem/577/B
The main effect of the topic:
Give the number of N and ask if some of them are divisible by M.
Ideas:
When N>m, according to the pigeon Nest principle, can judge must be yes. Because for these numbers, after the modulus of the range is (0,m-1), then when the first m+1, at least after the number of modulo and the previous one has a repeat, at this time there must be two add can be divisible by M.
When n<=m, it is possible to consider using DP,DP[I][J] to represent the first I number modulo to obtain J.
Code:
#include <stdio.h>
#include <string.h>
int dp[1005][1005],a[1000005];
int main ()
{
int n,m,i,j,k;
while (scanf ("%d%d", &n,&m)!=eof)
{
memset (DP));
for (i=1;i<=n;i++)
{
scanf ("%d", &a[i]);
a[i]=a[i]%m;
}
if (n>m)
{
printf ("yes\n");
Continue;
}
Dp[1][a[1]]=1;
for (i=2;i<=n;i++)
{
dp[i][a[i]]=1;
for (j=0;j<m;j++)
{
if (Dp[i-1][j]) {
dp[i][j]=1;
dp[i][(J+a[i])%m]=1
}}
if (Dp[n][0]) printf ("yes\n");
else printf ("no\n");
}
You can also use set to add the resulting and all to the set to see if the smallest one is 0.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <
Set> using namespace std;
int a[1000005],sum[100005];
int main () {int n,m,i,j,k,flag;
set<int>se;
while (scanf ("%d%d", &n,&m)!=eof) {flag=0;
memset (sum,0,sizeof (sum));
Se.clear ();
for (i=1;i<=n;i++) {scanf ("%d", &a[i]);
a[i]=a[i]%m;
} if (n>m) {printf ("yes\n");
Continue
} Set<int>::iterator it;
Se.insert (a[1]);
for (i=2;i<=n;i++) {it=se.begin ();
int x=*it;
if (x==0) {flag=1;
Break
int l=0;
For (It=se.begin (); It!=se.end (); ++it) {int x=*it;
Sum[l++]= (X+a[i])%m;
for (j=0;j<l;j++) Se.insert (Sum[j]); Se.insert (a[i]);
} it=se.begin ();
if (*it==0) flag=1;
if (flag) printf ("yes\n");
else printf ("no\n");
return 0;
}