HDU 5670 Machine(水题锻炼思维)

Problem Description
There is a machine with m(2≤m≤30) coloured bulbs and a button.When the button is pushed, the rightmost bulb changes.
For any changed bulb,

if it is red now it will be green;

if it is green now it will be blue;

if it is blue now it will be red and the bulb that on the left(if it exists) will change too.

Initally all the bulbs are red. What colour are the bulbs after the button be
pushed n(1≤n<263) times?

Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤15) indicating the number of test cases. For each test case:

The only line contains two integers m(2≤m≤30) and n(1≤n<263).

Output
For each test case, output the colour of m bulbs from left to right.
R indicates red. G indicates green. B indicates blue.

Sample Input
2
3 1
2 3

Sample Output
RRG
GR

Source
BestCoder Round #81 (div.2)

Recommend
wange2014 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5669

标程:

红、绿、蓝分别表示0、1、2,每次操作就相当于+1,原问题就转化为求nn的三进制

表示的最低的mm位,即求 nn mod 3^m3
​m的三进制表示。

复杂度 O(m)O(m)

我的思路:从后往前依次找因为后面的变化了三次前面的才变化依次,所以就是边取余边除。
下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a[32];
int main()
{
    int m,t;
    long long n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&m,&n);
        memset(a,0,sizeof(a));
        int i=1;
        while(n)
        {
            a[m-i]=n%3;
            n=n/3;
            i++;
        }
        for(int i=0;i<m;i++)
        {
            if(a[i]==0)
            {
                printf("R");
            }
            else if(a[i]==1)
            {
                printf("G");
            }
            else
            {
                printf("B");
            }
        }
        printf("\n");
    }
}

你可能感兴趣的:(HDU)