B-modulo SumTime
limit:2000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatus
Description
You are given a sequence of numbers a1, a2, ..., an, and a Number m.
Check if it is possible to choose a non-empty subsequence aiJ such that sum of Numbers in this subsequence are divisible by M.
Input
The first line contains numbers, n and m (1≤ n ≤106, 2≤ m ≤10 3)-the size of the original sequence and the number such that sum should is divisible by it.
The second line contains n integers a1, a2, ..., an ( C10>0≤ ai ≤109).
Output
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or 'NO ' (with Out the quotes), if such subsequence doesn ' t exist.
Sample Input
Input
3 5
1 2 3
Output
YES
Input
1 6
5
Output
NO
Input
4 6
3 1 1 3
Output
YES
Input
6 6
5 5 5 5 5 5
Output
YES
Hint
In the first sample test you can choose numbers 2 and 3, the sum of which are divisible by 5.
In the second sample test, the single non-empty subsequence of numbers are a single number 5. Number 5 is not divisible by 6, which is, the sought subsequence doesn ' t exist.
In the third, sample test, need to choose, numbers 3 on the ends.
In the fourth, sample test you can take the whole subsequence.
The backpack is still not well understood.
Because every time it is in that one-dimensional form.
The practice of this problem is similar to the 01 backpack.
In addition there can be an optimization ...
When the n>m ... The principle of eradicating drawers. Must be yes.
The complexity can be from O (nm) excellent to O (m^2)
1 /*************************************************************************2 > File name:code/#319/bb.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 September 15 Tuesday 21:31 12 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + #defineY1 HUST111QQZ A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - #defineLR DYING111QQZ - using namespacestd; - #definefor (i, n) for (int i=0;i<int (n); ++i) -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to Const intn=1e3+7; + intn,m; - intA[n]; the intdp[n][5]; * intMain () $ {Panax Notoginseng #ifndef Online_judge - the #endif +Cin>>n>>m; A if(n>m) the { +Puts"YES"); - return 0; $ } $ for(inti =1; I <= N; i++) - { - intx; thescanf"%d",&x); -A[i] = x%m;Wuyi } the intx =0 ; - for(inti =1; I <= N; i++) Wu { - Aboutdp[a[i]][1-X] =1; $ for(intj =1; J < M; J + +) - { - if(Dp[j][x]) - { A +dp[(J+a[i])%m][1-X] =1; thedp[j][1-X] =1; - } $ } thex =1-x; the if(dp[0][x]) the { thePuts"YES"); - return 0; in } the the } AboutPuts"NO"); the return 0; the the + - the Bayi #ifndef Online_judge the fclose (stdin); the #endif - return 0; -}
View Code
Codeforces #319 B-modulo Sum (drawer principle, DP)