codeforces 577B B. Modulo Sum(水题)
2016-09-06 11:10
127 查看
题目链接:
B. Modulo Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 the sum of numbers in this subsequence is divisible by m.
Input
The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
Examples
input
output
input
output
input
output
input
output
B. Modulo Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 the sum of numbers in this subsequence is divisible by m.
Input
The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
Examples
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 题意: 能否找到一个子序列的和能被m整除; 思路: 有一道hdu的水题就是这样的,以前傻傻的居然不会做;就是把余数拿出来搞搞就好了; AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const int mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e6+20; const int maxn=1e3+110; const double eps=1e-12; int n,m,a ,vis[maxn],temp[maxn]; int main() { read(n);read(m); int flag=0; For(i,1,n)read(a[i]); vis[0]=1; For(i,1,n) { a[i]%=m; int t=(m-a[i])%m; if(vis[t]){flag=1;break;} mst(temp,0); for(int j=0;j<m;j++) { if(vis[j])temp[(j+a[i])%m]=1; } for(int j=0;j<m;j++) if(temp[j])vis[j]=1; } if(flag)cout<<"YES\n"; else cout<<"NO\n"; return 0; }
相关文章推荐
- codeforces 577B B. Modulo Sum(背包+dp)
- CodeForces 577 B.Modulo Sum(组合数学+dp)
- codeforces 577B B. Modulo Sum(背包+dp)
- Codeforces 445A DZY Loves Chessboard(水题)
- codeforces 701A A. Cards(水题)
- codeforces 702A A. Maximum Increase(水题)
- Sereja ans Anagrams - CodeForces 367B 水题
- Yaroslav and Sequence - CodeForces 301A 水题
- CodeForces 732A Buy a Shovel (水题)
- codeforces 652B B. z-sort(水题)
- Codeforces 463A Caisa and Sugar(水题)
- CodeForces 705A(训练水题)
- Codeforces 735B Urbanization【水题】
- Codeforces 659C Tanya and Toys 【水题】
- CodeForces 631A Interview(水题)
- CodeForces - 789A Anastasia and pebbles (水题)
- Codeforces--616B--Dinner with Emma(模拟水题)
- 【Codeforces 707A】Brain's Photos 水题
- Codeforces 554B:Ohana Cleans Up(思维+水题)
- Codeforces 766C. Mahmoud and a Message 水题详解+错点记录