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