wustoj1284 Gold Medal

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 14
2 14
2 3

Sample Output

LEFT:1 3 9
RIGHT:27

No way!

LEFT:
RIGHT:3


想到三进制就变得无比简单的题

要注意 开始判断下w换成三进制长度是否>n 是的话肯定就no way了

cnt==n时 如果最后一位数刚好进位了 那么就进到n+1位上去了 也不行 


#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1000000007
using namespace std;

ll pow3[25],le[30],ri[30];

int main()
{
    int tmp,n,w,go[35],cnt,i,rn,ln;
    pow3[0]=1;
    for(i=1;i<=20;i++)
        pow3[i]=pow3[i-1]*3;
    while(~scanf("%d%d",&n,&w))
    {
        memset(go,0,sizeof go);
        tmp=w;
        cnt=0;
        while(tmp>0)
        {
            go[cnt++]=tmp%3;
            tmp/=3;
        }
        if(cnt>n)
        {
            printf("No way!\n\n");
            continue;
        }
        ln=0;rn=0;
        for(i=0;i<cnt;i++)
        {
            if(go[i]==3)
            {
                go[i+1]++;
                go[i]=0;
            }
            if(go[i]==2)
            {
                le[ln++]=pow3[i];
                go[i+1]++;
            }
            else if(go[i]==1)
            {
                ri[rn++]=pow3[i];
            }
        }
        if(go[cnt])
        {
            if(cnt>=n)
            {
                printf("No way!\n\n");
                continue;
            }
            ri[rn++]=pow3[i];
        }
        printf("LEFT:");
        for(i=0;i<ln;i++)
        {
            if(i!=0) putchar(' ');
            printf("%lld",le[i]);
        }
        putchar('\n');
        printf("RIGHT:");
        for(i=0;i<rn;i++)
        {
            if(i!=0) putchar(' ');
            printf("%lld",ri[i]);
        }
        putchar('\n');
        putchar('\n');
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值