Find a multiple

The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).

Input

The first line of the input contains the single number N. Each of next N lines contains one number from the given set.

Output

In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.

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的倍数。

第二种sum[1]=a[1],sum[2]=a[1]+a[2],…

其中有n的倍数,输出,就好了。

第三种 有sum[]中有n个数,sum%n的范围是0~n-1,这里就用到了抽屉原理,有n个数字,范围在1~n-1, 必定存在 sum[i]%n-sum[j]%n==0;

就可以知道有i+1~j

代码有点乱,,,,,

#include<stdio.h>
int a[10010],sum[10010];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sum1=0;
int s;
int flag=0;
int k;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]%n==0)
{
k=a[i];
flag=1;
}
sum1+=a[i];
sum[i]=sum1;
}
if(flag==1)
{
return printf("1\n%d\n",k); }
else
{
int d=0;
for(int i=1;i<=n;i++)
{
if(sum[i]%n==0)
{
printf("%d\n",i);
for(int j=1;j<=i;j++)
printf("%d\n",a[j]);
d=1;
}
}
if(d==0)
{
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if((sum[j]%n-sum[i]%n)==0)
{
printf("%d\n",j-i);
for(int k=i+1;k<=j;k++)
printf("%d\n",a[k]);
break; }
}
}
} } }
return 0;
}

poj 23565-Find a multiple的更多相关文章

  1. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  2. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  3. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  4. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  5. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

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

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

  7. POJ 1426 Find The Multiple(数论——中国同余定理)

    题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...

  8. POJ 1426 - Find The Multiple - [DP][BFS]

    题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...

  9. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  10. [深度优先搜索] POJ 1426 Find The Multiple

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28550   Accepted: 118 ...

随机推荐

  1. Unity C# 运用 GetSaveFileName() 导出Excel文件

    本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html 唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根 ...

  2. Elasticsearch如何安装中文分词插件ik

    elasticsearch-analysis-ik 是一款中文的分词插件,支持自定义词库. 安装步骤: 1.到github网站下载源代码,网站地址为:https://github.com/medcl/ ...

  3. js 数组对象去重

    let hash = {}; let config = [ { name: 2, state: true, output: 'Y'}, { name: 3, state: true, output: ...

  4. Android RecycleView实现混合Item布局

    首先来看看效果吧: 效果预览.png 本实例来自于慕课网的视屏http://www.imooc.com/video/13046,实现步骤可以自己去观看视屏,这里只记录了下实现的代码. 添加依赖: (1 ...

  5. Myeclipse 突然打不开的问题

    用的好好的Myeclipse今天突然打不开了,打开myeclipse提示 :an error has occurred  see the log file 然后我打开日志文件,看到如下的报错信息: ! ...

  6. JS调试debug

    1. debugger; 我以前也说过,你可以在JavaScript代码中加入一句debugger;来手工造成一个断点效果.需要带有条件的断点吗?你只需要用if语句包围它: if (something ...

  7. 【BZOJ1857】传送带(分治经典:三分套三分)

    点此看题面 大致题意: 一个二维平面上有两条传送带\(AB\)和\(CD\),\(AB\)传送带的移动速度为\(P\),\(CD\)传送带的移动速度为\(Q\),步行速度为\(R\),问你从\(A\) ...

  8. python 数据库操作 SQLite、MySQL 摘录

    转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...

  9. RobotFramework:钉钉扫码登录UI自动化

    背景: 遇到一个项目,使用的是钉钉扫码登录,一时间不知道该怎么下手了,还是先F12抓包看下都有什么数据传输吧. 分析: 先熟悉下钉钉扫码登录的逻辑,参考官文:https://open-doc.ding ...

  10. unsigned __int64 打印方法

    原文出处 long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647. 而unsigned范围是[0,2^32),即0~4294967295.也就是说,常规的 ...