[No.8 Middle School test] bisector

Keywords: less

3524: bisector

Time limit: 1 Sec
Memory limit: 128 MB

Title Description

There are 3 heaps of stones, and the initial quantity is a, B and C respectively. Each operation is aimed at two piles. Set the current number of stones in these two piles as X,Y and x < y. then take out x stones from the pile with more quantity and put them into the pile with less quantity to double the pile with less quantity. The quantity of two piles of stones after operation: x + X,Y-X
Q: after any number of operations, can the number of three piles of stones be equal?

input

Line 1: 3 integers a, B, C (1 < = a, B, C < = 1000)

output

Line 1: if it can be equal, output "possible", otherwise output "impossible"

sample input

10 15 35

sample output

possible

Tips

The first operation: 10, (15, 35) = > 10, 20, 30

The second operation: 20, (10, 30) = > 20, 20, 20

source

nothing

[ideas]

I started to see this question. The first feeling was to simulate and find the rules, but I was stuck in the back Although on the way of simulation, I have thought about searching, but it seems that there is no way to use -? - or search. Well, let's not talk about it. Let me talk about the idea of deep search.
In fact, deep search is not difficult. It's just enumeration by enumeration. But remember it and a special judgment, which can save a lot of time.
① Deep search (regular)

#include<cstdio>
#include<algorithm>
using namespace std;
bool dis[3001][3001],flag;
void dfs(int A,int B,int C)
{
    if(A==B&&B==C)
    {
        flag=1;
        return ;
    }
    if(dis[A][B]||!A||!B||!C) return ;
    dis[A][B]=1;
    if(A>B) dfs(A-B,B*2,C);
    else dfs(A*2,B-A,C);
    if(flag)
        return ;
    if(A>C) dfs(A-C,B,C*2);
    else dfs(A*2,B,C-A);
    if(flag)
        return ;
    if(B>C) dfs(A,B-C,C*2);
    else dfs(A,B*2,C-B);
    if(flag)
        return ;
}
int main()
{
    //freopen("pile.in","r",stdin);
    //freopen("pile.out","w",stdout);
    int A,B,C;
    scanf("%d %d %d",&A,&B,&C);
    if((A+B+C)%3==0) dfs(A,B,C);
    if(!flag) printf("impossible");
    else printf("possible");
}

② Deep search (big God version)

#include<cstdio>
#define MAXN 3000
bool memo[MAXN+1][MAXN+1];
void dfs(int s[]) 
{
    int t[3];
    if(memo[s[0]][s[1]])return;
    memo[s[0]][s[1]]=true;//Memorization
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++) 
            if(s[i]<s[j])//A magical place
            {
                t[0]=s[i]*2;
                t[1]=s[j]-s[i];
                t[2]=s[0+1+2-i-j];
                dfs(t);
            } 

}
int main(){
    //freopen("pile.in","r",stdin);
    //freopen("pile.out","w",stdout);
    int a[3], sum;
    scanf("%d%d%d", &a[0], &a[1], &a[2]);
    sum = a[0]+a[1]+a[2];
    if( sum % 3 != 0) {//Special judgement
        printf("impossible\n");
        return 0;
    }
    dfs(a);
    if(memo[sum/3][sum/3])
        printf("possible\n");
    else
        printf("impossible\n");
    return 0;
}

Posted by jej1216 on Mon, 04 May 2020 23:04:47 -0700