HDU 5670

Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 163    Accepted Submission(s): 99


Problem Description
There is a machine with m(2m30) 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(1n<263) times?
 
Input
There are multiple test cases. The first line of input contains an integer T(1T15) indicating the number of test cases. For each test case:

The only line contains two integers m(2m30) and n(1n<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
 
题意:有一个机器,它有 m(2≤m≤30)m (2leq mleq 30)m(2m30) 个彩灯和一个按钮。每按下按钮时,最右边的彩灯会发生一次变换。变换为:

1. 如果当前状态为红色,它将变成绿色;

2.如果当前状态为绿色,它将变成蓝色;

3.如果当前状态为蓝色,它将变成红色,并且它左边的彩灯(如果存在)也会发生一次变换。

初始状态下所有的灯都是红色的。
询问按下按钮 n(1≤n<263)n (1leq n< {2}^{63})n(1n<263​​) 次以后各个彩灯的颜色。
 
题解: 可以转换为求 n%(3^m)的三进制数
 
 1  #include<iostream>
 2  #include<cstring>
 3  #include<cstdio>
 4  #include<queue>
 5  #include<stack>
 6  #include<map>
 7  #include<set>
 8  #include<algorithm>
 9  #define ll __int64
10  #define pi acos(-1.0)
11  #define mod 1
12  #define maxn 10000
13  using namespace std;
14  int t;
15  map<int,char> mp;
16  ll n;
17  int m;
18  int b[35];
19  int main()
20  {
21      mp[0]='R';
22      mp[1]='G';
23      mp[2]='B';
24      scanf("%d",&t);
25      for(int i=1;i<=t;i++)
26      {
27          scanf("%d %I64d",&m,&n);
28          int k=0;
29          for(int j=0;j<=31;j++)
30           b[j]=0;
31          ll exm=1;
32           for(int i=1;i<=m;i++)
33              exm*=3;
34         if(n>=exm)
35             n=n%exm;
36          while(n)
37          {
38              b[k]=n%3;
39              n/=3;
40              k++;
41         }
42         if(k<m)
43         {
44           
45             for(int j=m;j>k;j--)
46              cout<<"R";
47              for(int j=k-1;j>=0;j--)
48              printf("%c",mp[b[j]]);
49         }
50         else
51         { 
52          for(int j=k-1;j>=k-m;j--)
53          printf("%c",mp[b[j]]);
54         }
55         cout<<endl;    
56 }
57      return 0;
58  }
原文地址:https://www.cnblogs.com/hsd-/p/5423490.html