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 鸽巢原理 解题的更多相关文章

  1. POJ 3370 Halloween treats(抽屉原理)

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6631   Accepted: 2448 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 3370 Halloween treats( 鸽巢原理简单题 )

    链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...

  4. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

  5. POJ 3370 Halloween treats(抽屉原理)

    Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...

  6. poj 3370 Halloween treats(鸽巢原理)

    Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...

  7. 鸽巢原理应用-分糖果 POJ 3370 Halloween treats

    基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...

  8. POJ3370&amp;HDU1808 Halloween treats【鸽巢原理】

    题目链接: id=3370">http://poj.org/problem?id=3370 http://acm.hdu.edu.cn/showproblem.php?pid=1808 ...

  9. [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)

    [POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...

随机推荐

  1. hihocode 编程练习赛17

    1. f1 score 首先了解f1 score的计算方法, 我记得是学信息检索知道的, 然后简单处理就行. 由于我写的比较麻烦, 中间处理过程引入了一些除数为0的情况,导致错了很多次.其实是很简单的 ...

  2. day03_12/13/2016_bean的管理之依赖注入

  3. define与typedef的区别

    define: 发生在预处理阶段,也就是编译之前,仅仅文本替换,不做任何的类型检查 没有作用域的限制 typedef: 多用于简化复杂的类型声明,比如函数指针声明:typedef bool (*fun ...

  4. [转]Linux finger命令

    转自:http://os.51cto.com/art/201003/186354.htm Linux finger命令是系统管理员的必备命令之一,他可以清楚的告诉管理员有多少用户在同时使用他所管理的L ...

  5. SublimeText学习(一)-安装

    1.下载安装包:http://www.sublimetext.com/2 2.开始安装,一直下一步 3.开始汉化 汉化包下载:http://files.cnblogs.com/files/2star/ ...

  6. 关于java.util.properties的随笔

    public class Propertiesextends Hashtable<Object,Object> Properties 类表示了一个持久的属性集.Properties 可保存 ...

  7. 【Java基础】多态

    首先先来个总结: 什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消 ...

  8. [Android]异常5-throwable:java.lang.OutOfMemoryError: pthread_create

    背景:线程初始化耗时任务 异常原因: 可能一>多个new Thread()嵌套 解决办法有: 解决一>使用Handler分离new Thread()嵌套 注: 06-30 09:12:26 ...

  9. win32绘图基础

     获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...

  10. Android studio USB连接失败

    Android studio USB连接失败,可能是因为adb的端口被占了,此时在其自带的cmd中输入netstat -aon|findstr "5037",并且启动任务管理器关掉 ...