Poj 2356 find a multiple/3370 Halloween treats Pigeon nest Principle

Source: Internet
Author: User

Given n positive integers, select any number from them so that their sum can be divisible by N.

Question: Combined mathematics P17

#include<cstdio>#include<cstring>using namespace std;const int MAXN = 20000;int sum[MAXN], num[MAXN], flag[MAXN];void print ( int s, int t ){    printf("%d\n", t - s + 1);    for ( int i = s; i <= t; i++ )        printf("%d\n",num[i]);}int main(){    int n, i;    scanf("%d",&n);    for ( sum[0] = 0, i = 1; i <= n; i++ )    {        scanf("%d",&num[i]);        sum[i] = sum[i-1] + num[i];    }    memset(flag,0,sizeof(flag));    for ( i = 1; i <= n; i++ )    {        int t = sum[i] % n;        if ( t == 0 ) { print ( 1, i ); break; }        if ( flag[t] ) { print ( flag[t] + 1, i ); break; }        flag[t] = i;    }    return 0;}

Below is 3370

#include<cstdio>#include<cstring>using namespace std;#define lint __int64const int MAXN = 110000;lint sum[MAXN];int num[MAXN], flag[MAXN];void print ( int s, int t ){    for ( int i = s; i < t; i++ )        printf("%d ",i);    printf("%d\n",t);}int main(){    int c, n, i;    while ( scanf("%d%d",&c,&n) )    {        if ( !c && !n ) break;        for ( sum[0] = 0, i = 1; i <= n; i++ )        {            scanf("%d",&num[i]);            sum[i] = sum[i-1] + num[i];        }        int start, end, t, f = 0;        memset(flag,0,sizeof(flag));        for ( i = 1; i <= n && !f; i++ )        {            t = sum[i] % c;            if ( t == 0 && sum[i] >= c )            {                start = 1, end = i; f = 1; break;            }            if ( flag[t] && sum[i] - sum[flag[t]] >= c )            {                start = flag[t] + 1, end = i, f = 1; break;            }            flag[t] = i;        }        if ( f ) print (start, end);        else printf("no sweets\n");    }    return 0;}

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.