Codeforces 577B Modulo Sum 【多重背包】
来源:互联网 发布:淘宝天猫交易额 编辑:程序博客网 时间:2024/06/05 17:36
题目链接:Codeforces 577B Modulo Sum
B. Modulo Sum
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn’t exist.
In the third sample test you need to choose two numbers 3 on the ends.
In the fourth sample test you can take the whole subsequence.
题意:给你n个数,问你能否找出若干个数,使得它们之和可以被m整除。
思路:找出余数,最坏情况下,余数最多有10^3个,每个最多有10^3个,这样就变成用这些余数去装m的背包,特判掉余数为0的情况。然后用多重背包优化下,时间复杂度最坏O(2*10^7)。
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <cmath>#include <stack>#include <map>#include <vector>#define fi first#define se second#define ll o<<1#define rr o<<1|1#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MOD = 1e9 + 7;const int MAXN = 1e6 + 10;const int INF = 1e9 + 10;void add(LL &x, LL y) { x += y; x %= MOD; }int a[MAXN];int num[1001];int val[25*1001];int dp[25*1001][1001];int main(){ int n, m; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 0; i < m; i++) { num[i] = 0; } bool flag = false; for(int i = 0; i < n; i++) { scanf("%d", &a[i]); if(a[i] % m == 0) { flag = true; } a[i] %= m; num[a[i]]++; } if(flag) { printf("YES\n"); continue; } sort(a, a+n); int N = unique(a, a+n) - a; int k = 1; for(int i = 0; i < N; i++) { for(int j = 1; j <= num[a[i]]; j <<= 1) { if((j * a[i]) % m == 0) { flag = true; } val[k++] = (j * a[i]) % m; num[a[i]] -= j; } if(num[a[i]] > 0) { if((a[i] * num[a[i]]) % m == 0) { flag = true; } val[k++] = (num[a[i]] * a[i]) % m; } } if(flag) { printf("YES\n"); continue; } CLR(dp, 0); dp[1][0] = dp[1][val[1]] = 1; for(int i = 1; i < k-1; i++) { for(int j = 0; j < m; j++) { if(dp[i][j]) { dp[i+1][j] = 1; if((j + val[i+1]) % m == 0) { flag = true; break; } dp[i+1][(j+val[i+1])%m] = 1; } } if(flag) break; } if(flag) { printf("YES\n"); } else { printf("NO\n"); } } return 0;}
- Codeforces 577B Modulo Sum 【多重背包】
- codeforces 577B B. Modulo Sum(背包+dp)
- codeforces 577B B. Modulo Sum(背包+dp)
- codeforces 577B Modulo Sum
- codeforces 577B Modulo Sum
- [CodeForces 577B]Modulo Sum[实现][数学]
- codeforces #319 DIV2 577B Modulo Sum
- codeforces 577B Modulo Sum(DP||set)
- [杂题] Codeforces #577B. Modulo Sum
- Codeforces Round #319 (Div. 2) B Modulo Sum (背包)
- Codeforces Round #319 (Div. 2) 577B Modulo Sum(dp)
- Codeforces 577B Modulo Sum 鸽巢原理
- Codeforces 577B Modulo Sum 动态规划基础
- Codeforces 577 B Modulo Sum 简单数学+dp
- cf B. Modulo Sum (01背包)
- codeforces 377B Modulo Sum(数学,dp)
- Codeforces Round #319 (Div. 2)——B. Modulo Sum(01背包)
- codeforce 577 B. Modulo Sum
- 了解数据库
- 关于百度,google map的url api的调用
- ubuntu安装pip+python27+ipython+scrapy+zlib-及遇到的各种问题解决
- java二分法查找
- 220. Contains Duplicate III set的应用
- Codeforces 577B Modulo Sum 【多重背包】
- 分布式服务通讯
- 深度学习之caffe
- VMware虚拟linux系统时与主机共享文件夹
- 计算机常识
- PHP+Mysql+jQuery实现文件下载次数统计
- ubuntu-pip+python27+ipython-安装-及遇到的各种问题解决
- 态度
- 黑马程序员 —— 注释的介绍及其使用