zju 2042 Divisibility

consider (1 <= N <= 10000, 2 <= K <= 100), that is the last modular will not exceed 100

otherwise,  when interger ( x % k = a )  and  ( a < 0 ) , change a = k - a ;

so, negtive value should not take consider  

/////////////////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <string.h>

const int M = 105 ;
bool flag[M], temp[M] ;

int main()
{
 bool isFirst = true ;
 int cases ;
 scanf ( "%d", &cases ) ;
 while ( cases-- )
 {
  memset ( flag, 0, sizeof(flag) ) ;
  
  int i, j, n, k, d, a, b ;
  scanf ( "%d %d %d", &n, &k, &d ) ;
  a = d % k ;
  a = a > 0 ? a : ( k + a ) ;
  b = k - a ;
  flag[a] = flag[b] = true ;
  
  for ( i = 1; i < n; i++ )
  {
   scanf ( "%d", &d ) ;
   
   memset ( temp, 0, sizeof(temp) ) ;
   for ( j = 0; j < k; j++ )
   {
    if ( flag[j] )
    {
     a = ( j + d ) % k ;
     if ( a < 0 )
      a = k + a ;
     b = ( j - d ) % k ;
     if ( b < 0 )
      b = k + b ;
     temp[a] = temp[b] = true ;
    }
   }
   memcpy ( flag, temp, sizeof(flag) ) ;
   
  }

  if ( isFirst )
   isFirst = false ;
  else
   printf ( "/n" ) ;

  if ( flag[0] )
   printf ( "Divisible/n" ) ;
  else
   printf ( "Not divisible/n" ) ;
 }
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章