zoukankan      html  css  js  c++  java
  • poj 2356 Find a multiple (剩余类,抽屉原理)

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

    和上一道题类似。

    由抽屉原理可知,一定不存在无解的情况。

    然后这道题。。。wa了多次。。

    原因是读错题。。

    输出的是原始的a[i],不是i。。。

    样例恰好a[2]=2,a[3] =3 

    真是sad....

     1 /*************************************************************************
     2     > File Name: code/poj/2356.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年08月21日 星期五 13时43分41秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #define y0 abc111qqz
    21 #define y1 hust111qqz
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define tm crazy111qqz
    25 #define lr dying111qqz
    26 using namespace std;
    27 #define REP(i, n) for (int i=0;i<int(n);++i)  
    28 typedef long long LL;
    29 typedef unsigned long long ULL;
    30 const int inf = 0x3f3f3f3f;
    31 const int N=2E4+7;
    32 int a[N];
    33 int sum[N];
    34 int n;
    35 int p[N];
    36 int main()
    37 {
    38     scanf("%d",&n);
    39     sum[0]= 0;
    40     for ( int i = 1  ; i <= n ; i++){
    41     scanf("%d",&a[i]);
    42     a[i] = a[i];
    43     sum[i] = (sum[i-1] + a[i])%n;
    44     }
    45     memset(p,0,sizeof(p));
    46     for ( int i = 1 ; i <= n ; i++){
    47     if (sum[i]==0){
    48         printf("%d
    ",i);
    49         for ( int j = 1 ; j <= i ; j++){
    50         printf("%d
    ",a[j]);
    51         }
    52         break;
    53     }
    54     if (p[sum[i]]){
    55        // cout<<"111qqz"<<endl;
    56         printf("%d
    ",i-p[sum[i]]);
    57         for ( int j = p[sum[i]]+1 ; j <= i ; j++){
    58         printf("%d
    ",a[j]);
    59         }
    60         break;
    61     }
    62         p[sum[i]] =  i;
    63     
    64     }
    65   
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    spring项目(springmvc)(多模块/单模块)maven打包引入第三方jar方式,使用scope:system配置systemPath编译,不用添加到本地仓库!
    Mysql 执行效率 性能综合贴
    前端Js框架 UI框架汇总 特性 适用范围 选择
    通用 正则表达式 C# (.NET)Regex 总结
    VSCode Node cannot launch program setting the 'outFiles' attribute might help
    CSS常见问题,定位技巧总结
    java 欢迎页 主页 设置为servlet的方法
    MSSQL Server 及 MSSQL Express版本 自动备份
    SQL Server 2008 R2 安装 下载
    mysql 日期计算集合
  • 原文地址:https://www.cnblogs.com/111qqz/p/4747739.html
Copyright © 2011-2022 走看看