找组合

  题目大意:给你N个自然数,请你求出若干个数的组合的和为N的整数倍的数

  经典鸽巢原理题目,鸽巢原理的意思是,有N个物品,放在N-1个集合中,则一定存在一个集合有2个元素或以上。

  这一题是说有找出和为N的整数倍的组合,则说明目标是找到sum[i]%N==0,而sum[i]%N恰好有N-1种非0的情况,而sum有N个,那么根据鸽巢原理,一定存在i,j,使sum[i]%N==sum[j]%N,且(sum[i]-sum[j])%N==0,同时j-i就是组合的个数,而且在这里,组合必定连续(因为sum是连续递增的)。

  

 #include <iostream>
#include <algorithm>
#define MAX_N 10001 using namespace std; static int hash_sum[MAX_N], num[MAX_N], sum[MAX_N]; int main(void)
{
int sum_n;
scanf("%d", &sum_n); fill(hash_sum, hash_sum + sum_n, -);
hash_sum[] = ; for (int i = ; i <= sum_n; i++)
{
scanf("%d", &num[i]);
sum[i] = (sum[i - ] + num[i]) % sum_n;
if (hash_sum[sum[i]] == -)
hash_sum[sum[i]] = i;
else
{
printf("%d\n", i - hash_sum[sum[i]]);
for (int j = hash_sum[sum[i]] + ; j <= i; j++)
printf("%d\n", num[j]);
break;
}
}
return ;
}

  

  另外这一题又卡cin了,用了std::ios::sync_with_stdio(false)都不行,不知道为什么。

  参考:http://www.cnblogs.com/BlackStorm/p/5243156.html

Mathematics:Find a multiple(POJ 2356)的更多相关文章

  1. Find a multiple POJ - 2356 【鸽巢原理应用】

    Problem DescriptionThe input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). E ...

  2. Find a multiple POJ - 2356

    The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers ...

  3. Find a multiple POJ - 2356 (抽屉原理)

    抽屉原理: 形式一:设把n+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2. ...

  4. Find a multiple POJ - 2356 容斥原理(鸠巢原理)

    1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 ...

  5. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  6. POJ 2356 Find a multiple 抽屉原理

    从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...

  7. POJ 2356 && POJ 3370 鸽巢原理

    POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...

  8. [POJ 2356] Find a multiple

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6535   Accepted: 2849   ...

  9. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

随机推荐

  1. R 语言程序设计

    Data The zip file containing the data can be downloaded here: specdata.zip [2.4MB] The zip file cont ...

  2. .NET中的工作目录一览!

    定义:       当前工作目录——进行某项操作的目的目录,会随着OpenFileDialog.SaveFileDialog等对象所确定的目录而改变. 当前执行目录——该进程从中启动的目录,即文件自身 ...

  3. OC第一节 —— 类和对象

    一.类和对象的概念 1.1类 自己的定义: 具有相同或相似性质对象的抽象. 1.2 对象 自己的定义: 对象是人们要进行研究的任何物体,从最简单的整数到复杂的飞机 等均可以看做是对象. 举例说明: 类 ...

  4. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  5. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...

  6. bug--java访问hdfs (Server IPC version 9 cannot communicate with client version 4 错误)

    1. 今天想做一个hdfs的java工具类,但是在连接hdfs的时候,报如下错误: Exception in thread "main" org.apache.hadoop.ipc ...

  7. 使用PHP的五个小技巧

    PHP的一些小技巧,比较基础,总结一下,老鸟换个姿势飘过去就是. 1. str_replace str_replace是非常常常常常用的php函数,用于字符串替换,经常看到某些php新人为了替换一批字 ...

  8. 使用APPCAN开发移动应用APP心得

    要想使用APPCAN开发移动应用,首先要弄明白什么是APPCAN,APPCAN都具有哪些功能. 1.什么是APPCAN? APPCAN是正益无线公司开发的一套Hybrid混合应用开发平台(AppCan ...

  9. UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence

    混淆了 python2 里边的 str 和 unicode 数据类型. 1. 对需要 str->unicode 的代码,可以在前边写上 import sys reload(sys) sys.se ...

  10. ASP.NET发送邮件(QQ发送)

    public void SetEmail()        {            //电子邮件对象            MailMessage mailMessage = new MailMes ...