ural 1437. Gasoline Station
1437. Gasoline Station
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Once a gasoline meter broke at a filling station. Fortunately, there was an overflow clipper (a device that makes it possible not to overfill a tank).
An ACM-programmer came to this station and saw an announcement saying that the station didn't work properly. As he was meditating on what to do next, he noticed three empty cans at the station's counter. He thought: "If I fill the first can and then empty it to the second one, and then take the third one… Maybe, I'll be able to measure out the needed amount of gasoline?"
As usual, he began to think about a general formulation of the problem, forgetting the partial case: "How many different capacities can I measure out using these cans?"
Of course, it is forbidden to spill gasoline because of ecology reasons. Also, the station's owner requires that gasoline only be transferred from the storage tank to a can or between cans. The car's tank may be filled from one or several of the cans only after all of the transfers.
Input
Each of the three input lines contains an integer from 0 to 255, which is the capacity of a gasoline can in liters.
Output
The result is an integer that is the number of different answers to the question how many liters the programmer can measure out using the gasoline cans with the specified capacities.
Sample
input | output |
---|---|
0 3 4 |
6 |
Notes
There is no sense to measure out 0 liters, so this value must not be counted.
Problem Author: Alexey Lakhtin
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Tags: dynamic programming
Difficulty: 570
题意:经典倒水问题,问三个不同容量、没有刻度的杯子,只能加水,且只能一次加满,可以在杯子间互相倒水,水不能倒掉,问能得到多少种不同体积的水?
分析:记忆化搜索。
真的一句话就够了。。。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 38 39 inline int Getint() 40 { 41 int Ret = 0; 42 char Ch = ' '; 43 bool Flag = 0; 44 while (!(Ch >= '0' && Ch <= '9')) 45 { 46 if (Ch == '-') Flag ^= 1; 47 Ch = getchar(); 48 } 49 while (Ch >= '0' && Ch <= '9') 50 { 51 Ret = Ret * 10 + Ch - '0'; 52 Ch = getchar(); 53 } 54 return Flag ? -Ret : Ret; 55 } 56 57 const int N = 255 * 3 + 1, M = 256; 58 int Arr[3], Now[3]; 59 bool Visit[M][M][M], Ans[N]; 60 int Answer; 61 62 inline void Input() 63 { 64 Rep(i, 3) cin >> Arr[i]; 65 Rep(i, 3) swap(Arr[i], Arr[rand() % 3]); 66 } 67 68 inline void Full(int *Can, int x, int y) 69 { 70 int t = min(Can[x], Arr[y] - Can[y]); 71 Can[x] -= t; 72 Can[y] += t; 73 } 74 75 inline void Search(int *Can) 76 { 77 int x = Can[0], y = Can[1], z = Can[2]; 78 if (Visit[x][y][z]) return; 79 Visit[x][y][z] = 1; 80 //cout << x << ' ' << y << ' ' << z << endl; 81 Rep(i, 3) 82 if (!Ans[Can[i]]) Ans[Can[i]] = 1; 83 Rep(i, 3) 84 Rep(j, 3) 85 if (i != j && !Ans[Can[i] + Can[j]]) 86 Ans[Can[i] + Can[j]] = 1; 87 if (!Ans[Can[0] + Can[1] + Can[2]]) 88 Ans[Can[0] + Can[1] + Can[2]] = 1; 89 90 int tmp[3]; 91 Rep(i, 3) tmp[i] = Can[i]; 92 Rep(i, 3) 93 Rep(j, 3) 94 if (i != j) 95 { 96 Full(tmp, i, j); 97 Search(tmp); 98 tmp[i] = Can[i]; 99 tmp[j] = Can[j]; 100 } 101 Rep(i, 3) 102 { 103 tmp[i] = Arr[i]; 104 Search(tmp); 105 tmp[i] = Can[i]; 106 } 107 } 108 109 inline void Solve() 110 { 111 Rep(i, 3) 112 if (Arr[i] == 1) 113 { 114 cout << Arr[0] + Arr[1] + Arr[2] << endl; 115 return; 116 } 117 118 Search(Now); 119 120 Answer = 0; 121 For(i, 1, N - 1) 122 { 123 Answer += Ans[i]; 124 //if(Ans[i]) cout << i << endl; 125 } 126 127 cout << Answer << endl; 128 } 129 130 int main() 131 { 132 //SetIO("G"); 133 Input(); 134 Solve(); 135 return 0; 136 }