codeforces 577B. Modulo Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/577/B
题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中选出一些数(不能为空),使得这些数的总和能整除 m 。
实不相瞒,完全没想法。。。看题解,有个地方看都看不懂: n > m的情况。求助乌冬子,连带被批英语水皮 >___<。还是谢谢他啦,一步一步引导我。

貌似挺多人也有这个疑惑的。他说那个是特例优化,原谅我懒,直接摘抄吧~
首先要知道一些参数。
前缀和 Sl = a1 + a2 + ... + al-1 + al
Sr = a1 + a2 + ... + al-1 + al + al+1 + ... + ar-1 + ar
Sr - Sl = al+1 + al+2 + ...ar-1 + ar
那个翻译就是,如果有两个前缀和 mod m 相等(Sr = Sl(mod m) ),那么就有以下的前缀和向区间和的转化了。
==> Sr - Sl = 0 (mod m)
==> a[l+1] + a[l+2] .. + a[r] = 0 (mod m)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
using namespace std; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, m;
while (scanf("%d%d", &n, &m) != EOF) { int a, r;
set<int> mods;
set<int> mods_tmp; mods.insert();
for (int i = ; i < n; i++) {
scanf("%d", &a);
for (set<int>::iterator it = mods.begin(); it != mods.end(); ++it) {
int r = (*it + a) % m;
if (r == ) {
printf("YES\n");
return ;
}
mods_tmp.insert(r);
mods_tmp.insert(a);
}
mods.insert(mods_tmp.begin(), mods_tmp.end());
mods_tmp.clear();
}
printf("NO\n");
}
return ;
}
(2)好理解点,中规中矩(218ms)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
using namespace std; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, m;
while (scanf("%d%d", &n, &m) != EOF) { int a, r;
set<int> mods;
set<int> mods_tmp; mods.insert();
for (int i = ; i < n; i++) {
scanf("%d", &a);
for (set<int>::iterator it = mods.begin(); it != mods.end(); ++it) {
int r = (*it + a) % m;
if (r == ) {
printf("YES\n");
return ;
}
mods_tmp.insert(r);
mods_tmp.insert(a);
}
mods.insert(mods_tmp.begin(), mods_tmp.end());
mods_tmp.clear();
}
printf("NO\n");
}
return ;
}
codeforces 577B. Modulo Sum 解题报告的更多相关文章
- Codeforces 577B Modulo Sum
http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...
- Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...
- codeforces D. Toy Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/405/D 题目意思:从 1 - 1000000 中选择 n 个数:x1,x2,...,xn,对 x1-1, ...
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
随机推荐
- PHP实现各种经典算法
<? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) function bin_s ...
- Excel 使用CHIINV函数和GAMMA.DIST函数绘制卡方分布
1.使用CHIINV(概率,自由度),在Excel中绘制卡方分布. 若n个独立的随机变量均服从标准正态分布,则这n个随机变量的平方和构成一新的随机变量,其分布规律称为服从自由度为ν 的χ2分布. 2. ...
- noip2012 疫情控制
[问题描述] H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境城市(叶子 ...
- R语言练习(一)
b = seq(from=0, to=1, by=0.001) #一次方 l1 = function(b){ b^1 } y1 = l1(b) #二次方 l2 = function(b){ b^2 } ...
- eclipse svn快捷键
一.打开eclipse插件安装市场,搜索svn,选择Subclipse安装 二.设置 svn ,设置快捷键, 1.windows-preference,在打开对话框输入keys过滤出keys选择 2. ...
- 修改Mysql默认编码
show variables like 'character%';+--------------------------+----------------------------+| Variable ...
- JQuery simpleModal插件的使用-遁地龙卷风
(0)写在前面 jquery.simpleModal.浏览器这三者的兼容性,不仅显示在报错上,还体现在所呈现的效果不是预期上. 说一下我的环境 jquery-1.8.3.js jquery.simpl ...
- Effective Java 读书笔记之六 方法
一.检查参数的有效性 1.考虑参数有哪些限制,把限制写到文档中,在方法的开头处通过显式地检查来实施这些限制. 二.必要时进行保护性拷贝 1.如果类具有从客户端得到或者返回的可变组件,类就必须考虑保护性 ...
- hiho #1325 : 平衡树·Treap
#1325 : 平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? ...
- php curl 实例+详解
直接上实例 <?php //创建一个新cURL资源 $ch = curl_init(); //用于中文等特殊字符的url转码 $aurl = urlencode($address); $url= ...