Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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?
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) .
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.
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)
/************************************************************************/
附上该题对应的中文题
Machine
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
有一个机器,它有 m(2≤m≤30) 个彩灯和一个按钮。每按下按钮时,最右边的彩灯会发生一次变换。变换为:1. 如果当前状态为红色,它将变成绿色;2.如果当前状态为绿色,它将变成蓝色;3.如果当前状态为蓝色,它将变成红色,并且它左边的彩灯(如果存在)也会发生一次变换。初始状态下所有的灯都是红色的。 询问按下按钮 n(1≤n<2?63??) 次以后各个彩灯的颜色。
输入描述
输入包含多组数据. 第一行有一个整数T(1≤T≤15), 表示测试数据的组数. 对于每组数据: 唯一的一行包含2个整数 m(2≤m≤30) 和 n(1≤n<2?63??) 。
输出描述
对于每组数据,输出一个长度为m的字符串,表示从左到右m个彩灯的颜色。 R代表红色;G代表绿色;B代表蓝色。
输入样例
2 3 1 2 3
输出样例
RRG GR
出题人的解题思路:
Machine
红、绿、蓝分别表示0、1、2,每次操作就相当于+1,原问题就转化为求n的三进制
表示的最低的m位,即求 n mod 3?m??的三进制表示。
复杂度 O(m)
考虑到每盏灯变化3次之后会影响到它的前一盏灯,所以我们进行拆分三进制,其中需要注意的一点是,没有被操作的灯为红色('R')
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define ll long long
using namespace std;
const int N = 35;
const int M = 10000;
const int inf = 100000000;
const int mod = 2009;
char s[N];
int main()
{int t,m,k,p,i;__int64 n;scanf("%d",&t);while(t--){scanf("%d%I64d",&m,&n);for(p=m,i=1;i<=m;++i)s[i]='R';while(n){k=n%3;if(k==0)s[p--]='R';else if(k==1)s[p--]='G';else if(k==2)s[p--]='B';n/=3;}for(i=1;i<=m;i++)printf("%c",s[i]);puts("");}return 0;
}
菜鸟成长记