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 |
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
Difficulty: 570
题意:经典倒水问题,问三个不同容量、没有刻度的杯子,只能加水,且只能一次加满,可以在杯子间互相倒水,水不能倒掉,问能得到多少种不同体积的水?
分析:记忆化搜索。
真的一句话就够了。。。
/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while (!(Ch >= '' && Ch <= ''))
{
if (Ch == '-') Flag ^= ;
Ch = getchar();
}
while (Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = * + , M = ;
int Arr[], Now[];
bool Visit[M][M][M], Ans[N];
int Answer; inline void Input()
{
Rep(i, ) cin >> Arr[i];
Rep(i, ) swap(Arr[i], Arr[rand() % ]);
} inline void Full(int *Can, int x, int y)
{
int t = min(Can[x], Arr[y] - Can[y]);
Can[x] -= t;
Can[y] += t;
} inline void Search(int *Can)
{
int x = Can[], y = Can[], z = Can[];
if (Visit[x][y][z]) return;
Visit[x][y][z] = ;
//cout << x << ' ' << y << ' ' << z << endl;
Rep(i, )
if (!Ans[Can[i]]) Ans[Can[i]] = ;
Rep(i, )
Rep(j, )
if (i != j && !Ans[Can[i] + Can[j]])
Ans[Can[i] + Can[j]] = ;
if (!Ans[Can[] + Can[] + Can[]])
Ans[Can[] + Can[] + Can[]] = ; int tmp[];
Rep(i, ) tmp[i] = Can[i];
Rep(i, )
Rep(j, )
if (i != j)
{
Full(tmp, i, j);
Search(tmp);
tmp[i] = Can[i];
tmp[j] = Can[j];
}
Rep(i, )
{
tmp[i] = Arr[i];
Search(tmp);
tmp[i] = Can[i];
}
} inline void Solve()
{
Rep(i, )
if (Arr[i] == )
{
cout << Arr[] + Arr[] + Arr[] << endl;
return;
} Search(Now); Answer = ;
For(i, , N - )
{
Answer += Ans[i];
//if(Ans[i]) cout << i << endl;
} cout << Answer << endl;
} int main()
{
//SetIO("G");
Input();
Solve();
return ;
}