Poj2356find a multiple Pigeon nest Principle

Source: Internet
Author: User

There must be a continuous K number, so that their abilities and abilities are divisible by N. Set a [I] As the prefix and

A [1] % N, a [2] % N ,..., the range of a [n] % N value is <n, so there are n numbers smaller than N. Two identical numbers will certainly appear, indicating, A part of the second number that is more than the first number must be divisible by N.

Pay attention to the handling of prefix and occurrence of 0 in the case.

#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;}

 

Poj2356find a multiple Pigeon nest Principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.