USACO 之 Section 1.4 More Search Techniques (已解决)
Arithmetic Progressions:
/*
需要注意两个可以优化的地方:
1)预处理,找到all integers of the form p^2 + q^2
(where p and q are non-negative integers)
2)判断以a为等差数列的第一个值,b为等差数列的公差时,会不会超过(p^2 + q^2)的上界
代码中相应位置做了说明。
*/
1 #define _CRT_SECURE_NO_WARNINGS 2 #define HOME 3 #define _CRTDBG_MAP_ALLOC 4 #include <crtdbg.h> 5 6 /* 7 ID: Jming 8 PROG: ariprog 9 LANG: C++ 10 */ 11 #include <iostream> 12 #include <fstream> 13 #include <sstream> 14 #include <cstdlib> 15 #include <cstdio> 16 #include <cstddef> 17 #include <iterator> 18 #include <algorithm> 19 #include <string> 20 #include <locale> 21 #include <cmath> 22 #include <vector> 23 #include <cstring> 24 #include <map> 25 #include <utility> 26 #include <queue> 27 #include <stack> 28 #include <set> 29 #include <functional> 30 using namespace std; 31 typedef pair<int, int> PII; 32 typedef long long int64; 33 const int INF = 0x3f3f3f3f; 34 const int modPrime = 3046721; 35 const double eps = 1e-9; 36 const int MaxA = 250 * 250 + 250 * 250 + 10; 37 const int MaxB = 10010; 38 39 int N, M; 40 41 priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pair_ans; 42 bool isLegal[MaxA]; 43 44 int limVal; 45 46 47 void ini() 48 { 49 /* 50 预处理,找到all integers of the form p^2 + q^2 51 (where p and q are non-negative integers) 52 */ 53 fill(isLegal, isLegal + MaxA + 1, false); 54 int doubleVal[255]; 55 for (int i = 0; i <= M; ++i) 56 { 57 doubleVal[i] = i*i; 58 } 59 for (int i = 0; i <= M; ++i) 60 { 61 for (int j = 0; j <= M; ++j) 62 { 63 isLegal[doubleVal[i] + doubleVal[j]] = true; 64 } 65 } 66 67 } 68 69 70 void Solve() 71 { 72 for (int a = 0; a <= limVal; ++a) 73 { 74 for (int b = 1; b <= limVal; ++b) 75 { 76 if (a + (N - 1)*b > limVal) 77 { 78 /* 79 判断以a为等差数列的第一个值,b为等差数列的公差时, 80 会不会超过(p^2 + q^2)的上界 81 */ 82 break; 83 } 84 int nowVal = a; 85 int cnt = 0; 86 while (true) 87 { 88 if (isLegal[nowVal]) 89 { 90 ++cnt; 91 } 92 else 93 { 94 break; 95 } 96 if (cnt == N) 97 { 98 pair<int, int> pII; 99 pII.first = b; 100 pII.second = a; 101 pair_ans.push(pII); 102 break; 103 } 104 nowVal += b; 105 } 106 } 107 } 108 } 109 110 111 void printAns() 112 { 113 if (pair_ans.empty()) 114 { 115 puts("NONE"); 116 } 117 else 118 { 119 while (!pair_ans.empty()) 120 { 121 printf("%d %d\n", pair_ans.top().second, pair_ans.top().first); 122 pair_ans.pop(); 123 } 124 } 125 } 126 127 int main() 128 { 129 #ifdef HOME 130 freopen("in", "r", stdin); 131 //freopen("out", "w", stdout); 132 #endif 133 134 freopen("ariprog.in", "r", stdin); 135 freopen("ariprog.out", "w", stdout); 136 137 scanf("%d %d", &N, &M); 138 limVal = M*M + M*M; 139 ini(); 140 Solve(); 141 printAns(); 142 143 144 #ifdef HOME 145 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 146 _CrtDumpMemoryLeaks(); 147 #endif 148 return 0; 149 }
Mother's Milk:
/*
题意关键:
FJ pours milk from one bucket to another until
the second bucket is filled or the first bucket is empty.
(每一次倒牛奶都遵守这个原则)
深搜,判重
*/
1 #define _CRT_SECURE_NO_WARNINGS 2 #define HOME 3 #define _CRTDBG_MAP_ALLOC 4 #include <crtdbg.h> 5 /* 6 ID: Jming 7 PROG: milk3 8 LANG: C++ 9 */ 10 #include <iostream> 11 #include <fstream> 12 #include <sstream> 13 #include <cstdlib> 14 #include <cstdio> 15 #include <cstddef> 16 #include <iterator> 17 #include <algorithm> 18 #include <string> 19 #include <locale> 20 #include <cmath> 21 #include <vector> 22 #include <cstring> 23 #include <map> 24 #include <utility> 25 #include <queue> 26 #include <stack> 27 #include <set> 28 #include <functional> 29 using namespace std; 30 typedef pair<int, int> PII; 31 typedef long long int64; 32 const int INF = 0x3f3f3f3f; 33 const int modPrime = 3046721; 34 const double eps = 1e-9; 35 const int MaxN = 25; 36 const int MaxM = 10010; 37 38 int A, B, C; 39 bool isVisited[25][25][25]; 40 set<int> ansSet; 41 42 void Solve(int a, int b, int c) 43 { 44 if (!isVisited[a][b][c]) 45 { 46 isVisited[a][b][c] = true; 47 if (0 == a) 48 { 49 ansSet.insert(c); 50 } 51 52 // C->A 53 if (c >= (A - a)) 54 { 55 Solve(A, b, c - (A - a)); 56 } 57 else 58 { 59 Solve(a + c, b, 0); 60 } 61 62 // C->B 63 if (c >= (B - b)) 64 { 65 Solve(a, B, c - (B - b)); 66 } 67 else 68 { 69 Solve(a, b + c, 0); 70 } 71 72 // B->A 73 if (b >= (A - a)) 74 { 75 Solve(A, b - (A - a), c); 76 } 77 else 78 { 79 Solve(a + b, 0, c); 80 } 81 82 // B->C 83 if (b >= (C - c)) 84 { 85 Solve(a, b - (C - c), C); 86 } 87 else 88 { 89 Solve(a, 0, c + b); 90 } 91 92 // A->B 93 if (a >= (B - b)) 94 { 95 Solve(a - (B - b), B, c); 96 } 97 else 98 { 99 Solve(0, b + a, c); 100 } 101 102 // A->C 103 if (a >= (C - c)) 104 { 105 Solve(a - (C - c), b, C); 106 } 107 else 108 { 109 Solve(0, b, c + a); 110 } 111 } 112 } 113 114 115 int main() 116 { 117 #ifdef HOME 118 freopen("in", "r", stdin); 119 //freopen("out", "w", stdout); 120 #endif 121 122 freopen("milk3.in", "r", stdin); 123 freopen("milk3.out", "w", stdout); 124 125 scanf("%d %d %d", &A, &B, &C); 126 memset(isVisited, false, sizeof(isVisited)); 127 Solve(0, 0, C); 128 for (set<int>::iterator it = ansSet.begin(); it != ansSet.end(); ) 129 { 130 printf("%d", *it); 131 ++it; 132 if (it != ansSet.end()) 133 { 134 printf(" "); 135 } 136 } 137 printf("\n"); 138 139 #ifdef HOME 140 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 141 _CrtDumpMemoryLeaks(); 142 #endif 143 return 0; 144 }