Poj 2356 pigeon cage principles

Source: Internet
Author: User

(It should be noted that the code I wrote outputs the result when there is an answer, but OJ also reads the result from the file, so at first glance, it seems that there is a result when I have not entered it, but I don't know about OJ. In fact, I directly use the poj3370 code AC, 32 MS, and O (?_ =) O) to directly paste the code.

#include<cstdio>  #include<cstring>  using namespace std;  #define N 100002    int sum[N],pos[N],a[N];    int main()  {        int n,i,r,t,j;        while(~scanf("%d",&n))      {          memset(pos,-1,sizeof(pos));          bool flag=false;            scanf("%d",&sum[0]);          a[0]=sum[0];          sum[0]%=n;          pos[sum[0]]=0;          if(sum[0]==0)          {              printf("1\n%d\n",sum[0]);              flag=1;          }            for(i=1;i<n;i++)          {              scanf("%d",&sum[i]);              if(flag)continue;              a[i]=sum[i];              sum[i]%=n;              sum[i]+=sum[i-1];              sum[i]%=n;              if(sum[i]==0)              {                  printf("%d\n",i+1);                  for(j=0;j<=i;j++)                      printf("%d\n",a[j]);                  flag=1;                  continue;              }              if(pos[sum[i]]==-1)pos[sum[i]]=i;              else              {                  printf("%d\n",i-pos[sum[i]]);                  for(j=pos[sum[i]]+1;j<=i;j++)                      printf("%d\n",a[j]);                  flag=1;              }          }        }        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.