USACO Chapter 1 Section 1.4

 1 /*
 2 ID:xiekeyi1
 3 PROG:ariprog
 4 LANG:C++
 5  */
 6 #include<bits/stdc++.h>
 7 using namespace std ;
 8 const int maxn = 250 * 250 * 2 ; 
 9 bool a[maxn + 10 ] ; 
10 void init( int m )
11 {
12     memset( a , false , sizeof(a) ) ;
13     for( int i = 0 ; i <= m ; i++)
14         for( int j = i ; j <= m ; j++)
15             a[ i*i + j*j ] = true ;
16     return ; 
17 }
18 struct node
19 {
20     int a , b ;    
21     node( int _a , int _b) : a(_a) , b(_b) {}  
22 };
23 vector<node> ans ; 
24 
25 bool cmp( struct node a , struct node b )
26 {
27     if( a.b < b.b )
28         return true ;
29     else if( a.b == b.b )
30         return a.a < b.a ;
31     else
32         return false ;
33 }
34 
35 int main()
36 {
37     freopen("ariprog.in","r",stdin);
38     freopen("ariprog.out","w",stdout);
39     int n , m;
40     cin >> n >> m ; 
41     init(m) ;
42     int d = 1 ;
43     int maxx = m*m + m*m ; 
44     bool flag = false ;
45     while( ( n - 1 ) * d <= maxx) 
46     {
47         for( int i = 0 ; i <= maxx - ( n - 1 ) * d ; i++)
48         {
49             bool f = true ; 
50             for( int j = i ; j <= i + ( n - 1 ) * d ; j +=d )
51             {
52                 if( a[j] == false )
53                 {
54                     f = false ;
55                     break ;
56                 }
57             }
58             if( f ) 
59             {
60                 ans.push_back( node(i,d) ) ;
61                 flag = true ;
62             }
63         }
64 
65         d++ ;
66     }
67 
68     sort( ans.begin() , ans.end() , cmp ) ; 
69     if( flag )  
70         for( vector<node> :: iterator iter = ans.begin() ; iter != ans.end() ; iter++)
71             cout << iter->a << ' ' << iter->b << endl ;
72 
73     else
74         cout << "NONE" << endl ;
75 
76     return 0 ; 
77 }
ariprog

 

 

 

 1 /*
 2 ID:xiekeyi1
 3 PROG:milk3
 4 LANG:C++
 5 */
 6 
 7 #include<bits/stdc++.h>
 8 using namespace std ;
 9 const int maxn =25;
10 bool vis[maxn][maxn];
11 bool note[maxn];
12 
13 int aa[4];
14 void dfs( int a , int b , int c )
15 {
16     if( vis[a][b] )
17         return ;
18     vis[a][b] = true ;
19     if( a == 0 )
20         note[c] = true ;
21 
22     int temp[4];
23     temp[1] = a , temp[2] = b , temp[3]  = c ;
24 
25     for( int i = 1 ; i <= 3 ; i++)
26     {
27         for( int j = 1 ; j <= 3 ; j++)
28         {
29             if( i != j )
30             {
31                 if( temp[i] != 0 && temp[j] < aa[j] )
32                 {
33                     if( temp[i] <= aa[j] - temp[j] )
34                     {
35                         int t1 = temp[i] , t2 = temp[j] ;
36                         temp[j] += temp[i] ;
37                         temp[i] = 0 ;
38                         dfs( temp[1] ,temp[2] , temp[3]);
39                         temp[i] = t1 , temp[j] = t2 ; 
40                     }
41 
42                     if( temp[i] > aa[j] - temp[j] )
43                     {
44                         int t1= temp[i] , t2 = temp[j] ;
45                         temp[i] -= aa[j] - temp[j] ;
46                         temp[j] = aa[j] ;
47                         dfs( temp[1] , temp[2] , temp[3]);
48                         temp[i] = t1 , temp[j] = t2 ;
49                     }
50                 }
51             }
52         }
53     }
54 
55 }
56 
57 int main()
58 {
59     freopen("milk3.in","r",stdin);
60     freopen("milk3.out","w",stdout);
61     cin >> aa[1] >> aa[2] >> aa[3] ; 
62     dfs( 0,0,aa[3]) ;
63     for( int i = 0 ; i <= aa[3] - 1 ; i++)
64     {
65         if( note[i] )
66             cout << i << ' ' ;
67     }
68 
69     cout <<  aa[3]  << endl ;
70     return 0 ; 
71 }
milk3

 

posted on 2017-04-27 22:53  Dark猫  阅读(108)  评论(0)    收藏  举报

导航