Find a multiple
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 4988  Accepted: 2159  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

Source

题目大意: 给出n个数,选出连续的若干m个数,使得和为n的倍数。输出m,以及任意顺序的m个数。
#include <stdio.h>
#define MAX 10100
int s1[MAX];
int s2[MAX];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,sum,begin,end;
        sum=begin=end=0;
        memset(s2,0,sizeof(s2));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&s1[i]);
        }
     
        for(i=1;i<=n;i++)
        {
            sum=(s1[i]+sum)%n;
            if(sum==0) {begin=1;end=i;break;}
            else if(!s2[sum])
            {s2[sum]=i;}
            else
            {
                begin=s2[sum]+1;
                end=i;
                break;
            }
        }
        //printf("%d\n",begin);
        //printf("%d\n",end);
        //if(i>=n)
        //printf("0\n");
        //else
        //{
        printf("%d\n",end+1-begin);
        for(i=begin;i<=end;i++)
        printf("%d\n",s1[i]);
        //}
    }
    return 0;   
}
//参考代码如下所示:
/*
#include<stdio.h>
#define MAX 10100
int a[MAX];
int locker[MAX];
int main()
{
 int n,i;
 while(scanf("%d",&n)!=EOF)
 {
  int sum=0,begin=0,end=0,num;
  for(i=1; i<=n; i++)
   scanf("%d",&a[i]);
  memset(locker,0,sizeof(locker)); 
  for(i=1; i<=n; i++)
  {
   sum=(a[i]+sum)%n;
   if(sum==0)
   {
    begin=1;
    end=i; break;
   }
   else if(!locker[sum])  //抽屉为空
   {
    locker[sum]=i;  //抽屉保存的是该元素在数组中的地址
   }
   else //抽屉不为空
   {
    begin=locker[sum]+1;
    end=i; break;
   }

}
  num=end+1-begin;
  printf("%d\n",num);
  for(i=begin; i<=end; i++)
   printf("%d\n",a[i]);
 }
 return 0;
}
*/

组合数学之抽屉原理

•第一原理:
•1:把多于n个的物体放到n个抽屉里,则至少有一个抽屉里的东西不少于两件。
 
•2:把多于mn(m乘以n)个的物体放到n个抽屉里,则至少有一个抽屉里有不少于m+1的物体。
 
•3:把无穷多件物体放入n个抽屉,则至少有一个抽屉里 有无穷个物体。
 

【ACM】poj_2356_Find a multiple_201308061947的更多相关文章

  1. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  2. 【ACM】HDU1008 Elevator 新手题前后不同的代码版本

    [前言] 很久没有纯粹的写写小代码,偶然想起要回炉再来,就去HDU随便选了个最基础的题,也不记得曾经AC过:最后吃惊的发现,思路完全不一样了,代码风格啥的也有不小的变化.希望是成长了一点点吧.后面定期 ...

  3. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  4. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  5. 【ACM】不要62 (数位DP)

    题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...

  6. 【Acm】八皇后问题

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 其解决办法和我以前发过的[算法之美—Fire Net:www.cnblogs.com/lcw/p/3159414.html]类似 题目:在8 ...

  7. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  8. 【acm】杀人游戏(hdu2211)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others)    M ...

  9. 【ACM】How many prime numbers

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=2 #inclu ...

随机推荐

  1. WebSocket在Asp.Net中的例子

    环境 以下代码环境要求:win8或win10, .net4.5+IIS8 部署到IIS8上面 转到 Windows程序和功能 -打开Windows功能里面 IIS选项启动4.5 和WebSocket支 ...

  2. codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

    E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...

  3. 查看服务器wwn是否在交换机侧

    判断port_state是否为Online状态,是的话,读取出port_name,即为wwn. #!/usr/bin/env python3 # -*- coding: UTF-8 -*- impor ...

  4. [Apple开发者帐户帮助]三、创建证书(4)创建Safari签名证书

    您的Safari扩展程序必须由Apple颁发的证书签名,您可以在开发者帐户中创建和下载该证书. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择“Safari扩展”. 在“证书”下,选择“ ...

  5. 有关于dict(字典)的特性与操作方法

    有关于dict(字典)的特性与操作方法 1.字典的特性 语法: dic = {key1 : value1,key2 : value2,key3 : value3............} 注:字典中k ...

  6. Vue 函数

    1.转换为大写字符 .toUpperCase() 2.字符串反转  this.message = this.message.split('').reverse().join('') 3.从index开 ...

  7. 苹果html上传后图片旋转问题

    最近做移动web项目但是遇到在苹果设备上html上传图片后,图片传到后台是旋转的 旋转角度不一,因此再次 读取照片时,无法正常显示,目前已经找到解决方法,至于原因看不太懂 翻译过来也是完全按照单词翻译 ...

  8. Spring Boot (19) servlet、filter、listener

    servlet.filter.listener,在spring boot中配置方式有两种:一种是以servlet3开始提供的注解方式,另一种是spring的注入方式. servlet注解方式 serv ...

  9. 【java基础】(6)内部类

    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...

  10. Md2All版本更新记录

    Md2All版本更新记录 版本号:V2.8.2更新日期:2018-06-281:结合云图床,解决了Latex公式复制到公众号时有可能报“图片粘贴失败的问题”;2:结合云图床,解决了Latex公式复制到 ...