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 ...
随机推荐
- [转]android 让一个控件按钮居于底部的几种方法
本文转自:http://www.cnblogs.com/zdz8207/archive/2012/12/13/2816906.html android 让一个控件按钮居于底部的几种方法 1.采用lin ...
- 元素属性的添加删除(原生js)
添加属性 odiv.setAttribute("title","hello div!"); odiv.setAttribute("class" ...
- HTML 表单 存为EXCEL文件时 中文显示乱码
在做宣传品发放系统时,需求要把数据库查询的记录生成表单并转存excel文件. 在转存的EXCEL文件中文显示乱码,表格和其他字符正常,检查后发现是创建EXCEL文件打开模式不对 之前: myfile ...
- React Native组件间通信
React Native组件间通信 React Native组件的关系有:父子关系.无直接关系.组件间通信主要针对这两类来讨论. 一.父组件和子组件之间通信 父组件向子组件传递消息.数据通过对子组件的 ...
- Less——less基本安装
1.下载node.js 我们需要NodeJ运行LESS示例. 要下载NodeJ,请打开链接https://nodejs.org/en/ 2.node.js安装是否正确 在cmd中输入lessc -v, ...
- jQuery——类的添加与删除
添加类:addClass 删除类:removeClass 判断类是否存在:hasClass <!DOCTYPE html> <html lang="en"> ...
- JS——三个运用正则的方法
trim().replace().search() <script> //trim();去除前后的空格 var str = " 你好 我很好! "; console.l ...
- JSP学习笔记 - 内置对象 Request
1.主要掌握以下5个内置对象及其所属类,必须学会在java docs里根据类名查找相应的方法 request javax.servlet.http.HttpServletRequest res ...
- CAD把一个命令当着一个函数调用,不执行(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SendStringToExecuteFun 把一个命令当着一个函数调用,不执行,详细说明如下: 参数 说明 CString s ...
- 聊聊JS动画库:Velocity.js
前言 又到了炎热的7月,很久没有更新技术文章了,原因是上月月底实习结束,从公司离职.然后最近在弄自己的项目和考驾照,为了下次公司的应聘做准备,送别了女朋友到外地,哩哩啦啦半个月把一切事情都办妥后,还是 ...