[Codeforces 577B] modulo sum[implementation [Mathematics]__codeforces

Source: Internet
Author: User

Topic Link: [Codeforces 577b]modulo sum[Implementation] [mathematics]

The analysis:

Give the number of N, and then give a number m. Ask the given number of n, any combination, whether there is a combination and can be divisible by M.

Ideas for solving problems:

Further, the question is: in the combination of all these numbers, is there and makes it divisible by M. Obviously, the combination of these and after the remainder is not greater than M, there is a lot of duplication. So we can use an array to mark this and whether it appears, and if not, put the vector array, and then update the vector array each time.

The entire complexity of 1e9. But will not arrive, the reason is n>m, the answer must exist, so the total complexity should be m^2.

The specific reasons are as follows:

As mentioned above, after the number of the rest is not greater than M, when there are n>m number, consider starting from the first number of continuous and, constantly go with M to take the rest, there must be a time after the result of the rest of the show before. Because: assuming that each new number after the successive and after the number is new, then at most m will appear before the successive and, set the first occurrence of the continuous interval for [0,first] The second occurrence of the interval is [0,second]. Then there must be: (SUM (0,first) + sum (first,second))%m = SUM (0,first)% m + sum (one, second)%m = SUM (0, second).

Because sum (0,first) = = SUM (0, second), SUM (first,second)% m = 0. All right, here's the proof.

Personal experience:

The weak slag was kneeling yesterday. Feeling ability is still insufficient. Math is the pain that can breathe.

The specific code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <queue> using namespace std;

const int MAXN = 1E3 + 111;
int A, n, M, SUM[MAXN];

BOOL flag = 0, VIS[MAXN];
    int main () {scanf ("%d%d", &n, &m);
    Vector<int> sum;
        for (int i = 0; i < n; ++i) {scanf ("%d", &a);
        A%= m;
        int len = Sum.size ();
            for (int j = 0; j < Len; ++j)//update VECOTR array {int newsum = (Sum[j] + a)% m;
                if (newsum = = 0) {flag = 1;
            Goto Ed;
                } if (!vis[newsum]) {sum.push_back (newsum);
            Vis[newsum] = 1; } if (!vis[a])//Don't forget to put the number in itself {if (a = = 0) {flag = 1
                ;
            Break
            } sum.push_back (a);
        Vis[a] = 1;
 } ed: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.