hdoj Machine 5670 (模拟)水

来源:互联网 发布:e25歼击车数据 编辑:程序博客网 时间:2024/06/04 17:59

Machine

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


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
23 12 3
 

Sample Output
RRGGR
 

Source
BestCoder Round #81 (div.2)
问题描述
有一个机器,它有 m (2\leq m\leq 30)m(2m30) 个彩灯和一个按钮。每按下按钮时,最右边的彩灯会发生一次变换。变换为:1. 如果当前状态为红色,它将变成绿色;2.如果当前状态为绿色,它将变成蓝色;3.如果当前状态为蓝色,它将变成红色,并且它左边的彩灯(如果存在)也会发生一次变换。初始状态下所有的灯都是红色的。询问按下按钮 n (1\leq n< {2}^{63})n(1n<263) 次以后各个彩灯的颜色。
输入描述
输入包含多组数据. 第一行有一个整数T (1\leq T\leq 15)T(1T15), 表示测试数据的组数. 对于每组数据:唯一的一行包含2个整数 m (2\leq m\leq 30)m(2m30)n (1\leq n< {2}^{63})n(1n<263)
输出描述
对于每组数据,输出一个长度为mm的字符串,表示从左到右mm个彩灯的颜色。R代表红色;G代表绿色;B代表蓝色。
输入样例
23 12 3
输出样例
RRGGR
//思路:
就是一个模拟找规律的题,先将前面的几次的情况模拟出来,很快就会发现规律:
比如m=3,n=8:
RRR  0
RRG  1
RRB  2
RGR  3
RGG  4
RGB  5
RBR  6
RBG  7
RBB  8
从上面的例子就可以推出规律:
最右面的那一位每三次一个循环,往前一位是每9次一个循环(并且连续3个是相同的),再往前一位是每27次一个循环(连续9个是相同的).......
所以根据这个规律就可以做这个题了。
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define ll long longusing namespace std;ll kp(ll x,ll k){ll s=1;while(k){if(k&1)s*=x;x*=x;k>>=1;}return s;}char judge(int x){if(x==0) return 'R';if(x==1) return 'G';if(x==2) return 'B';}int main(){int t,m;ll n;scanf("%d",&t);while(t--){scanf("%d%lld",&m,&n);for(int i=1;i<=m;i++){ll k=n/kp(3,m-i);k%=3;printf("%c",judge(k));}printf("\n");}return 0;}

0 0
原创粉丝点击