POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats
和POJ2356差点儿相同。
事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列。并且有这种数列也必然能够找到。
#include <cstdio>
#include <cstdlib>
#include <xutility> int main()
{
int c, n;
while (scanf("%d %d", &c, &n) && c)
{
int *neighbours = (int *) malloc(sizeof(int) * n);
int *sumMod = (int *) malloc(sizeof(int) * (n+1));
int *iiMap = (int *) malloc(sizeof(int) * c);
std::fill(iiMap, iiMap+c, -1); sumMod[0] = 0;
int L = -1, R = -1; for (int i = 0; i < n; i++) scanf("%d", &neighbours[i]); for (int i = 0; i < n; i++)
{
sumMod[i+1] = (sumMod[i] + neighbours[i]) % c; if (sumMod[i+1] == 0)
{
L = 1, R = ++i;//下标从1起
break;
} if (iiMap[sumMod[i+1]] != -1)
{
L = iiMap[sumMod[i+1]] + 2, R = ++i; //下标从1起
break;
} iiMap[sumMod[i+1]] = i;
} if (R != -1)
{
for (int i = L; i < R; i++)
{
printf("%d ", i);
}
printf("%d\n", R);
}
else puts("no sweets"); free(neighbours), free(iiMap), free(sumMod);
}
return 0;
}
POJ 3370 Halloween treats 鸽巢原理 解题的更多相关文章
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- [POJ 3370] Halloween treats
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7143 Accepted: 2641 ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...
- 鸽巢原理应用-分糖果 POJ 3370 Halloween treats
基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...
- POJ3370&HDU1808 Halloween treats【鸽巢原理】
题目链接: id=3370">http://poj.org/problem?id=3370 http://acm.hdu.edu.cn/showproblem.php?pid=1808 ...
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
随机推荐
- PKUSC2017 游记 密码:blog密码
退役之前,写点破事乐呵乐呵 省选滚大粗 报了PKU和THU的SC 果然THU直接审核不通过... 于是就来到了PKU 滚粗狗就又续命几天. Day1 上午考数学 喜闻乐见啥都不会 出来一对题 ...
- 12 C#中的方法
还记得我们的第一个程序吗?忘记了?那你要努力了.我们的第一个程序是就是往dos窗口输出一些字符串.在哪个程序中只有一个方法,Main方法.Main方法是一个特殊的方法,但是它也是一个方法.为什么说Ma ...
- fcc jQuery 练习
在页面顶端增加一行script元素,然后写上结束符, 浏览器会运行script 里所有的Javascript,包括jQuery <script>$(document).ready(func ...
- Java NIO 聊天室实例
最近写了个Java NIO聊天室聊天的程序,NIO学习起来比较困难的,我的代码能给大家起到一个抛砖引玉的作用! 服务端: package test.javanio; /** * @author * @ ...
- [Windows Server 2008] 安装网站伪静态
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS伪静 ...
- Docker是什么?可以用Docker做什么?
作者:刘允鹏 链接:https://www.zhihu.com/question/28300645/answer/67707287 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- win10系统安装postgresql后无法连接
win10系统安装postgresql后在系统服务列表中找不到,连接不上数据库. 可以尝试关闭系统防火墙后重启电脑或者重装程序.
- 使用GetMirror一次镜像多个实体
public static void GetMirror(this ObjectIdCollection ids, Point3d p1, Point3d p2, bool s, params Ent ...
- JAVA通用工具类
1.异常验证框架 框架1:com.google.common.base.Preconditions 框架2:org.apache.commons.lang.Validate 框架3:org.apach ...
- swift 关键字willSet 和 didSet
// 下面是苹果给出的解释,就是在给属性设置新值的时候,可以在设置前和设置后做一些处理,这两个关键字就好像对该属性变化的监控 If you don’t need to compute the prop ...