URAL1032--抽屉原理


/*
ID: wangzha4
LANG: C++
TASK: URAL1032
*/

// 根据抽屉原理一定有解
// remain[i]记录前i项和对inn的模
// if( 存在remain[i]==0 ) --  直接输出1~i
// else remain[i]的取值是[1~n-1]
// 而remain[i]共有n个值 -- 由抽屉原理 -- 定存在i,j满足remain[i]==remain[j]

#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< string .h >
#include 
< ctype.h >
#define  llong unsigned long long 
#define  unint unsigned int
#define  printcrlf printf( "\n" )

const   int  inf  =   1000000  ;
const   int  size  =   10010  ;

int  inn ;
int  data[size] ;
int  remain[size] ;
int  flag[size] ;

int  main()
{

#ifndef ONLINE_JUDGE
    freopen( 
" in.txt " " r " , stdin ) ;
    freopen( 
" out.txt " , " w " ,stdout ) ;
#endif

    
while ( scanf(  " %d " , & inn )  !=  EOF )
    {
        memset( flag, 
0 sizeof (flag) ) ;
        remain[
0 =   0  ;
        
for int  i = 1 ; i <= inn; i ++  ){
            scanf( 
" %d " , & data[i] ) ;
            remain[i] 
=  (remain[i - 1 ] + data[i])  %  inn ;
        }

        
for int  i = 1 ; i <= inn; i ++  ){
            
if 0   ==  remain[i] ){
                printf( 
" %d\n " ,i ) ;
                
for int  j = 1 ; j <= i; j ++  )    printf(  " %d\n " ,data[j] ) ;
                
break  ;
            }
            
else {
                
if ( flag[remain[i]] ){
                    printf( 
" %d\n " ,i - flag[remain[i]] ) ;
                    
for int  j = flag[remain[i]] + 1 ; j <= i; j ++  )    printf(  " %d\n " ,data[j] ) ;
                    
break  ;
                }
                
else {
                    flag[remain[i]] 
=  i ;
                }
            }
        }
    }
// while
     return   0  ;
}

你可能感兴趣的:(URAL1032--抽屉原理)