CF 577B Modulo Sum

Source: Internet
Author: User

Test instructions: Gives a sequence of positive integers of length n, asking if a discontinuous subsequence can be found and divisible by M.

Solution: Drawer principle +DP. First when the m<n must have the answer, because according to the drawer principle, when the sequence of n prefixes and%m, must appear two the same number, the two prefixes and subtract the resulting sequence and must be divisible by M. When N<=m, the DP is ready, similar to the 01 backpack.

In fact, you can directly DP, as long as the scroll array + in the answer when the break can be, the same because the drawer principle, when enumerated to the M+1 item when it will certainly get the solution, so the final complexity O (m^2).

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL    Long longusing namespace Std;int a[1005];bool dp[1005][1005];int main () {int n, m;        while (~SCANF ("%d%d", &n, &m)) {memset (DP, 0, sizeof DP);            if (n > m) {int x;            for (int i = 0; i < n; i++)//Do not read join re yo ~ Don't ask me how to Know _ (: З"∠) _ scanf ("%d ", &x);            Puts ("YES");        Continue        } for (int i = 1; I <= n; i++) scanf ("%d", &a[i]);        bool ans = 0;            for (int i = 1; I <= n &&!ans; i++) {a[i]%= m; Dp[i][a[i]] = 1;//is different from the 01 backpack, the backpack must have items, so can not be transferred directly from the 0 state for (int j = 0; J < m &&!ans;                DP[I][J] |= dp[i-1][j];//not select Current Item dp[i][(j + a[i])% m] |= dp[i-1][j];//Select Current item            if (dp[i][0]) ans = 1;        }} if (ans) puts ("YES");    Else puts ("NO"); } return 0;}

It was all 01 backpacks in my mind when I wrote it. However, there are some differences ... The result is a mess of writing ...

CF 577B Modulo Sum

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.