Codeforces 577B modulo Sum (dp| | Set) __codeforces

Source: Internet
Author: User

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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.