首页 > 其他 > 详细

wustoj1284 Gold Medal

时间:2014-03-30 02:48:25      阅读:496      评论:0      收藏:0      [点我收藏+]

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;
}


wustoj1284 Gold Medal,布布扣,bubuko.com

wustoj1284 Gold Medal

原文:http://blog.csdn.net/u011032846/article/details/22541141

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!