Find a multiple
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6651   Accepted: 2910   Special Judge

Description

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 分析:当不存在从下标0开始的某一段数字对n取余等于0的时候,需要找一个yu[i]和yu[j]相等,采用类似哈希的方式。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 10000+100 using namespace std; int a[N];
int sum[N];
int yu[N]; struct node
{
bool k;
int pos; }q[N]; int main()
{
int n;
int i, j;
int left, right; while(~scanf("%d", &n))
{
bool flag1=false;
bool flag2=false; for(i=0; i<n; i++){
scanf("%d", &a[i] );
if(i==0) sum[i]=a[i];
else sum[i]=sum[i-1]+a[i];
}
left=0;
memset(q, 0, sizeof(q)); for(i=0; i<n; i++){
yu[i]=sum[i]%n;
if(yu[i]==0){
flag1=true; right=i; break;
}
else{
if(q[yu[i]].k ){
flag2=true;
left=q[yu[i]].pos; right=i; break;
}else{
q[yu[i]].k=true; q[yu[i]].pos=i;
}
}
}
if(flag1){
printf("%d\n", right+1 );
for(i=0; i<=right; i++){
printf("%d\n", a[i] );
}
}
else if(flag2){
printf("%d\n", right-left );
for(i=left+1; i<=right; i++){
printf("%d\n", a[i] );
}
}
}
return 0;
}

最新文章

  1. spring 源码
  2. Ionic – 强大的 HTML5 Mobile App 开发框架
  3. vim支持lua
  4. Solr分页与高亮(使用SolrNet实现)
  5. 强连通+二分匹配(hdu4685 Prince and Princess)
  6. ZOJ 3872--解题报告
  7. 【NOIP2010】引水入城
  8. 使用post()方法以POST方式从服务器发送数据
  9. idl生成.h .c文件
  10. Android-为何以及如何保存Fragment实例
  11. 第十八篇 js高级知识---作用域链
  12. 程序员的基本功之Java集合的实现细节
  13. 【Win 10 应用开发】UI Composition 札记(六):动画
  14. gcov 统计 inline 函数
  15. 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)
  16. git 查看远程分支最后一次提交时间
  17. pdo 操作
  18. 最小费用最大流模板(POJ 2135-Farm Tour)
  19. android 启动socket 失败:socket(af_inet sock_stream 0) 返回-1
  20. JSONP学习笔记

热门文章

  1. 用HTML5canvas绘制一个圆环形的进度表示
  2. layer 不居中的坑爹问题
  3. 高度平衡树 -- AVL 树
  4. strpos 判断字符串是否存在
  5. windows上mysql安装
  6. 转载 iOS全局检测网络变化的实时状态
  7. DICOM:C-GET服务
  8. 【v2.x OGE教程 11】 动画编辑器帮助文档
  9. K.Bro Sorting(思维题)
  10. vue项目创建流程和使用