find a multiple
Description
Input
Output
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
5
1
2
3
4
1
Sample Output
2
2
3
题目大意是: 输入n个数,从中找出数字,它们加起来之和是n的倍数,并将它们输出。 分析:之和是n的倍数,就是对n求余是0。 通常大家会想把这些数随机组合,得到的和存放在一个数组里,但遇到很多数,这就不好办了。 由于问题只要找出一组即可,所以我们就是要找到一组。 所以我建立个抽屉模型,
为什么可以用抽屉模型?有n个数,这些数的连续和分别对n取余得到的余数范围是0~n-1, 不相同的余数个数不超过n,当个数为n时, 那其中肯定有一组数的和满足条件,若小于n,这当中至少有两组数的余数相同,举个
例子,1,3,5,9,2 ,5个数, 我把它们连续相加变成, 1,4, 9, 18, 20, 仍然是5个数。 使他们一一对5取余,得到 1, 4, 4, 3, 0。 我把余数当作抽屉,和作为物体, 这里有5个物体,4个抽屉,
第一组数是1,3,第二组数是1,3,5。 用后者减前者便是答案。
#include <stdio.h>
#define M 10001
int a[M], mod[M];
int main()
{
int n, i, sum, begin, end;
while(scanf("%d", &n) != EOF){ sum = ;
for(i = ; i <= n; i++){ scanf("%d", &a[i]); }
for(i = ; i <= n; i++){ sum = (sum + a[i]) % n;
if(!sum)
{
begin = ;
end = i;
break;
}
else if(mod[sum] > )
{
begin = mod[sum] + ;
end = i;
break;
}
mod[sum] = i; }
printf("%d\n", end - begin + );
for(i = begin; i <= end; i++){ printf("%d\n", a[i]); }
putchar('\n'); }
return ;
}
find a multiple的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- scala - multiple overloaded alternatives of method bar define default arguments
同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
- OPEN CASCADE Multiple Variable Function
OPEN CASCADE Multiple Variable Function eryar@163.com Abstract. Multiple variable function with grad ...
随机推荐
- hdu3448 01背包+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...
- hdu 5072 Coprime 容斥原理
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- Type.GetField 修改类中私有字段。
上一篇Popup Bug中修改了SystemParameters类中静态只读属性MenuDropAlignment. var t = typeof(SystemParameters); var fie ...
- CentOS7安装docker出错(Transaction check error)
1. 出错内容: Transaction check error: :-.el7_2..x86_64 conflicts with :-.el7.x86_64 :-.el7_2..x86_64 con ...
- Hive 实现HBase 数据批量插入
HBase 数据的插入可以使用Java API 来写Java 程序逐条倒入,但是不是很方便.利用Hive自带的一个Jar包,可以建立Hive和HBase的映射关系 利用Hive 的insert可以将批 ...
- C# 词法分析器(二)输入缓冲和代码定位
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 一.输入缓冲 在介绍如何进行词法分析之前,先来说说一 ...
- AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
- jenkins 的 ProcessTreeKiller----无法启动子进程的解决办法
参考: http://alanland.iteye.com/blog/2047244 http://scmbob.org/start-process-in-jenkins.html java -Dhu ...
- hdu 1097 A hard puzzle
Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...
- 【BZOJ】1856: [Scoi2010]字符串
http://www.lydsy.com/JudgeOnline/problem.php?id=1856 题意:把n个1和m个0组成字符串,要求在组成的字符串中,任意的前k个字符1的个数不能少于0的个 ...