题目链接: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 (mod m)
==> Sr - Sl = 0 (mod m)
==> a[l+1] + a[l+2] .. + a[r] = 0 (mod m) 
  而到了 n <= m的情况, 之后就是01背包窝~~写不出来,好惨啊= =
  还是用set浅显易懂。。。。
  轮到我用的方法了(都是参考人的,神奇啦)
  *****************************************
  大体思路:保存选的数的各种组合,求出各种和的不同结果,保存在set容器中的 mods 中。当中要用到另一个set :mods_tmp。用来保存当前处理的数a与mods中的各个和,组合出的但在原mods中没有出现的和。一轮迭代之后,将这个mods_tmp添加到mods中。这样不用下次再算嘛~~~~注意,mod m 最多只会出现 m 种结果:0, 1, 2, ..., m-1。所以不用担心超时。
  顺便说说代码中为什么一开始要插入 0,因为可以只选当前输入的那个 a 嘛~~~哈哈哈·~~~
  (1)这个会比(2)快(124ms)

 #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 解题报告的更多相关文章

  1. Codeforces 577B Modulo Sum

    http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...

  2. Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

    题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...

  3. codeforces D. Toy Sum 解题报告

    题目链接:http://codeforces.com/problemset/problem/405/D 题目意思:从 1 - 1000000 中选择 n 个数:x1,x2,...,xn,对 x1-1, ...

  4. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  5. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  6. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  9. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

随机推荐

  1. smarty string_format用法 取小数点后2位

    <{if $d.ul_pv}> <{$d.sum/$d.ul_pv|string_format:'%.2f'}> <{else}> 0.00 <{/if}&g ...

  2. openssh for windows安装

     openssh for windows安装 2009-11-22 22:43:58 分类: WINDOWS 本文转自:http://blog.chinaunix.net/uid-7541208-id ...

  3. autopep8

    修正python pep8的警告挺无趣的, 用了 autopep8 感觉比较爽. 记录如下. ----------------pep8检查----------------平时我用pydev做pep8检 ...

  4. 在ashx中使用Server对象

    Server.MapPath() System.Web.HttpContext.Current.Server.MapPath()

  5. Perl 正则表达式

    匹配:m/<regexp>;/ (还可以简写为 /<regexp>;/ ,略去 m)替换:s/<pattern>;/<replacement>;/转化: ...

  6. List对象排序通用方法

    import java.util.Collections; import java.util.Comparator; import java.util.List; import java.lang.r ...

  7. 微信电脑版真的要来了 微信Windows版客户端1.0 Alpha推出

    微信电脑版的搜索量一直很大,但只有网页版,之前也写了微信网页版APP - 网页微信客户端电脑版体验,在键盘上打字的感觉就是快.现在微信Windows版客户端1.0 Alpha推出了,来一睹芳容吧(20 ...

  8. 【C语言入门教程】3.3 条件控制语句

    在程序的 3 种基本结构中,第二种是选择结构,选择结构是根据程序运行时获得的条件,决定程序执行情况.条件控制语句可用来实现这种结构,C 语言提供了 if 语句和 switch 语句两种条件控制语句,i ...

  9. SSH-keygen参数说明

    以防网址丢失发,复制备份.复制来源ssh-keygen参数说明 ssh-keygen - 生成.管理和转换认证密钥     ssh-keygen [-q] [-b bits] -t type [-N  ...

  10. 浮动层-JS兼容IE6

    //引用jquery 包/*orderBycat 与 orderBycatHead 双层浮动*/ $(window).scroll(function () { var top = $(window). ...