wustoj1284Gold Medal(三进制思想)

来源:互联网 发布:sp版羽毛球拍淘宝店 编辑:程序博客网 时间:2024/06/11 09:26
1284: Gold Medal

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

Problem Description

Gold medal is the aim of every one in WHUACM training team. Once upon a time, when Xioumu was a new member of team, he used to be a very curious boy. One day when he saw the gold medal of big cow Xay. Xioumu wanted to know the total weight of Xay‟s gold medal. Unfortunately,there are just exactly N weights and one balance. And the mark in i-th weights is 3^(i-1). So these N weights are 1, 3, 9, 27… Xioumu could put these weights on the left side of balance or right side of balance arbitrarily. Well, the gold medal is always on the left side. Now give you N and the weight of gold medal, please output the way to measure it. If there is more than one way, please output the way with the least number of weights. If Xioumu could not measure the gold medal with these weights, output “No way!”.

Input

There are several test cases. For each case there are two integer numbers. The number of weights N and the weight of the gold medal W . Technical Specification 1 <= N <= 30, 1 <= W <= 1,000,000,000

Output

For each case, if there exists a way to measure it. Then the output should contain two lines, the first line start with “LEFT:” and output the weights in the left side in ascending order. The second line start with “RIGHT:” and output the weights in the right side in ascending order. Else, just output “No way!”. Print a blank line after each case. See the following samples for more details.

Sample Input

4 142 142 3

Sample Output

LEFT:1 3 9RIGHT:27No way!LEFT:RIGHT:3

HINT

Source

Difficulty



题目大意:题面很清楚,给定n,w。开始天平左边放w重的物品。然后有1,3,9...,3^n-1这样的砝码可以使用。选取这样的砝码,通过左右摆放使得砝码能够平衡。

    解题思路:开始看题目说的意思有多组答案。。想到了爆搜。我们直接将开始的重量转换为三进制,然后通过给加法使得每位的权值为1或者为0,所要加的值便是左边需要放的砝码,加起来得到的数便是右边需要摆放的砝码。答案肯定唯一。

    题目地址:http://wusttest.sinaapp.com/problem.php?id=1284

AC代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;int a[35];  //表示w的三进制表示。int b[35];  //表示left砝码int c[35];  //表示right砝码int res1[35];int res2[35];int t;int mypow(int base,int n){    int ans=1;    while(n--)        ans=ans*base;    return ans;}void tran(int w){    t=0;    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    memset(c,0,sizeof(c));    while(w)    {        a[t++]=w%3;        w/=3;    }    /*for(int i=0;i<t;i++)        printf("%d ",a[i]);    printf("\n");*/}void cal(){    int i;    for(i=0;i<t+1;i++)    {        if(a[i]==0)        {            b[i]=0,c[i]=0;        }        else if(a[i]==1)        {            b[i]=0,c[i]=1;        }        else if(a[i]==2)        {            b[i]=1,c[i]=0;            a[i+1]++;        }        else if(a[i]==3)        {            b[i]=0,c[i]=0;            a[i+1]++;        }    }}int main(){    int n,w,i;    while(cin>>n>>w)    {        tran(w);        cal();        int ans=0;        for(i=t;i>=0;i--)        {            if(b[i]!=0||c[i]!=0)            {                ans=i+1;                break;            }        }        if(ans>n)        {            printf("No way!\n\n");            continue;        }        int t1=0,t2=0;        for(i=0;i<t+1;i++)        {            if(b[i])            {                res1[t1++]=mypow(3,i);            }            if(c[i])            {                res2[t2++]=mypow(3,i);            }        }        printf("LEFT:");        for(i=0;i<t1;i++)        {            if(i>0) printf(" ");            printf("%d",res1[i]);        }        printf("\nRIGHT:");        for(i=0;i<t2;i++)        {            if(i>0) printf(" ");            printf("%d",res2[i]);        }        printf("\n\n");    }    return 0;}/*4 142 142 3*/



0 0