UPC Blink && Luxury River Cruise(循環節)

Blink中文題面友鏈 傳送門

Blink

題目描述
Unhappy with the dim lighting in his barn, Farmer John has just installed a fancy new chandelier consisting of N (3 <= N <= 16) lights bulbs arranged in a circle.

The cows are fascinated by this new light fixture, and enjoy playing the following game: at time T, they toggle the state of each light bulb if its neighbor to the left was turned on at time T-1. They continue this game for B units of time (1 <= B <= 10^15). Note that B might be too large to fit into a standard 32-bit integer.

Given the initial states of the light bulbs, please determine their final states after B units of time have elapsed.
輸入

  • Line 1: Two space-separated integers, N and B.
  • Lines 2…1+N: Line i+1 contains the initial state of bulb i, either 0 (off) or 1 (on).
    輸出
  • Lines 1…N: Line i should contain the final state of bulb i, either 0 (off) or 1 (on).
    樣例輸入 Copy
    5 6
    1
    0
    0
    0
    0
    樣例輸出 Copy
    1
    1
    1
    0
    1
    提示
    There are five light bulbs. The first is initially on, and the others are off.The light bulb states are as follows:
    Time T=0: 1 0 0 0 0
    Time T=1: 1 1 0 0 0
    Time T=2: 1 0 1 0 0
    Time T=3: 1 1 1 1 0
    Time T=4: 1 0 0 0 1
    Time T=5: 0 1 0 0 1
    Time T=6: 1 1 1 0 1

題意:
給定一個操作和初始狀態,問經過b個操作後的狀態。
思路:
先看一眼題目,好了暴力模擬。
看一眼數據範圍,退出比賽(手動滑稽)

其實想想就知道這麼多操作肯定是有規律(循環)的,我們要做的就是找出循環節。因爲每一次給的初始狀態不一樣,所以每次循環節的開頭也不一樣。當我們還未找到循環節但是已經進行了b個操作後,可以停止找循環節的過程,直接輸出。

代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
int st[550][20];//儲存每次燈的狀態 
bool last[20];//判斷上次燈的狀態 
int n,t=1,s=0;//燈的數量,循環次數,週期,循環起始位置 
ll m;
void check(int a,int b){
	bool flag=1;
	for(int i=0;i<n;i++)
		if(st[a][i]!=st[b][i]) flag=0;
	if(flag) s=a,t=b-a;
}
void AC(){
   n=read();scanf("%lld",&m);
   for(int i=0;i<n;i++) st[0][i]=read();
   for(int i=1;i<=500;i++){
   		memset(last,0,sizeof last);
   		for(int j=0;j<n;j++){
   			if(st[i-1][j]) last[j]=1;
			st[i][j]=st[i-1][j];//轉移上次狀態	
		}
		for(int j=0;j<n;j++)
			if(last[j]) st[i][(j+1)%n]^=1;
		for(int j=1;j<i;j++){
			check(j,i);
			if(t>1)  break;
		}
		if(i==m){
			for(int j=0;j<n;j++){
				out(st[m][j]);
				puts("");
			}
			return ;
		}
   } 
   for(int i=0;i<n;i++){
   		out(st[(m-s)%t+s][i]);
   		puts("");
   }
}
int main(){
    AC();
    return 0;
}

Luxury River Cruise

題目描述
Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with N ports (1 <= N <= 1,000) labeled 1…N, and Bessie starts at port 1. Each port has exactly two rivers leading out of it which
lead directly to other ports, and rivers can only be sailed one way.

At each port, the tour guides choose either the “left” river or the “right” river to sail down next, but they keep repeating the same choices over and over. More specifically, the tour guides have chosen a short sequence of M
directions (1 <= M <= 500), each either “left” or “right”, and have repeated it K times (1 <= K <= 1,000,000,000). Bessie thinks she is going in circles – help her figure out where she ends up!

輸入

  • Line 1: Three space-separated integers N, M, and K.
  • Lines 2…N+1: Line i+1 has two space-separated integers, representing the number of the ports that port i’s left and right rivers lead to, respectively.
  • Line N+2: M space-separated characters, either ‘L’ or ‘R’. ‘L’ represents a choice of ‘left’ and ‘R’ represents a choice of ‘right’.
    輸出
  • Line 1: A single integer giving the number of the port where Bessie’s cruise ends.
    樣例輸入 Copy
    4 3 3
    2 4
    3 1
    4 2
    1 3
    L L R
    樣例輸出 Copy
    4
    提示
    The port numbers are arranged clockwise in a circle, with ‘L’ being a clockwise rotation and ‘R’ being a counterclockwise rotation. The sequence taken is LLRLLRLLR.After the first iteration of the sequence of directions, Bessie is at port 2 (1 -> 2 -> 3 -> 2); after the second, she is at port 3 (2 -> 3 -> 4 -> 3), and at the end she is at port 4 (3 -> 4 -> 1 -> 4).

題意:
思路: 有了上一題的思路想着這題就很容易了~還是一個找循環節的過程

代碼:
未完待續

參考資料:
P2203 Blink 題解 - 洛谷 | 計算機科學教育新生態

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章