Poj2356Find a multiple鸽巢原理

  一定存在连续的k个数,使得他们的和能被n整除。设a[i]为前缀和

a[1]%n  ,a[2]%n,...,a[n]%n的值的范围<n,所以有n个数小与n,肯定会出现两个一样的数,表明了,第二个数比第一个数多出来的一部分一定能被n整除。

要注意处理 前缀和中出现0的情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
typedef long long LL;

int main()
{
    int n;
    int a[22222];
    int b[22222];
    int vis[22222];
    while (cin >> n){
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++)
            cin >> a[i], b[i] = a[i];
        for (int i = 1; i <= n; i++){
            b[i] += b[i - 1]; b[i] %= n;
        }
        int gg = 0;
        for (int i = 1; i <= n;i++)
        if (b[i] == 0){
            cout << i << endl;
            for (int j = 1; j <= i; j++)
                cout << a[j] << endl;
            gg = 1; break;
        }
        if (gg) continue;
        int flag = 0;
        for (int i = 1; i <= n; i++){
            if (flag) continue;
            if (!vis[b[i]]){
                vis[b[i]] = i;
            }
            else{
                cout << i - vis[b[i]] << endl;
                for (int j = vis[b[i]]+1; j <= i; j++)
                    cout << a[j] << endl;
                break;
            }
        }
    }
    return 0;
}
时间: 2024-08-23 14:18:38

Poj2356Find a multiple鸽巢原理的相关文章

poj 2356 Find a multiple 鸽巢原理的简单应用

题目要求任选几个自然数,使得他们的和是n的倍数. 由鸽巢原理如果我们只选连续的数,一定能得到解. 首先预处理前缀和模n下的sum,如果发现sum[i]==sum[j] 那么(sum[j]-sum[i])%n一定为0,直接输出i+1~j就够了. 为什么一定会有解,因为sum从1~n有n个数,而模n下的数只有0~n-1,把n个数放入0~n-1个数里,怎么也会有重复,所以这种构造方法一定没问题. 其实可以O(n)实现,嫌麻烦,就二重循环无脑了. #include <iostream> #includ

POJ 2356 Find a multiple 鸽巢原理

题目来源:POJ 2356 Find a multiple 题意:n个数 选出任意个数 使得这些数的和是n的倍数 思路:肯定有解 并且解是连续的一段数 证明: 假设有m个数 a1,a2,a3...am    s1 s2 s3...sm为前缀和 s1 = a1 s2 = a1+a2 s3 = a1+a2+a3... sm = a1+a2+a3+...+am 1.如果某个前缀和si%m == 0 那么得到解 2.设x1=s1%m x2 = s2%m x3 = s3%m xm = sm%m 因为1不成

POJ 2356 find multiple 鸽巢原理

我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>jquery test</title> <script src="jquery-1.11.1.min.js">&

[POJ2356] Find a multiple 鸽巢原理

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000

poj 2356 Find a multiple (鸽巢原理妙用)

题目链接:http://poj.org/problem?id=2356 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

poj2356Find a multiple——鸽巢定理运用

题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,a[10005]; long long mo

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

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000

poj 2356 Find a multiple(鸽巢原理)

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 i

poj2356 Find a multiple(抽屉原理|鸽巢原理)

/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据抽屉原理: 因为有n个数,对n个数取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同, 如果余数出现0,自然就是n的倍数.也就是说,n个数中一定存在一些数的和是n的倍数. 本题的思路是从第一个数开始一次求得前 i(i <= N)项的和关于N的余数sum,并依次记录相应余数的存在状态,