BearPlaysDiv2
Limak is a little bear who loves to play. Today he is playing by moving some stones between three piles of stones. Initially, the piles contain A, B, and C stones, respectively. Limak's goal is to produce three equal piles. Limak will try reaching his goal by performing a sequence of zero or more operations. In each operation he will start by choosing two unequal piles. Let's label their sizes X and Y in such a way that X < Y. He will then double the size of the smaller chosen pile by moving some stones between the two chosen piles. Formally, the new sizes of the two chosen piles will be X+X and Y-X. You are given the ints A, B, and C. Return "possible" (quotes for clarity) if there is a sequence of operations that will make all three piles equal. Otherwise, return "impossible". | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | A, B and C will be between 1 and 500, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
bool can[1505][1505];
void dfs(int a[3])
{
if(can[a[0]][a[1]]) return;
can[a[0]][a[1]] = true;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(a[i] < a[j])
{
int t2[3] = {a[i]*2,a[j]-a[i],a[3-i-j]};
dfs(t2);
}
}
}
}
string equalPiles( int A, int B, int C ) {
memset(can,0,sizeof(can));
int s = A + B + C;
int a[3] = {A,B,C};
if(s % 3 == 0)
{
dfs(a);
if(can[s/3][s/3]) return "possible";
}
return "impossible"; //
}