题目
题意: 给定仅含A、B、C的字符串,进行至多t = 1e18次迭代,每次迭代是独立地将A->BC,B->CA,C->AB.求t次迭代后,第k个位置的字母是what?
思路: 感觉是除除除,因为我们可以根据k反推推回它是由数组原始位置的哪个字母变来的。然后哩?如果t很大,我们发现这个字母是a、b、c轮着变换的,%3即可。注意这里下标从0开始,方可保证%3正确性和一致性。怎么想出来的俺也不知道,被自己菜哭了,今晚连着两把都被暴捶。
时间复杂度: O(log1e18)
代码:
// Problem: D - ABC Transform
// Contest: AtCoder - AtCoder Beginner Contest 242
// URL: https://atcoder.jp/contests/abc242/tasks/abc242_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
#define int long long
int n,m,k,T;
char s[M];
int fun(int t,int k)
{
if(t==0) return s[k]-'A';
else if(k == 0) return (s[0]-'A'+t)%3;
return (fun(t-1,k/2)+k%2+1)%3;
}
void solve()
{
scanf("%s",s);
read(m);
while(m--)
{
ll t,k; read(t),read(k);
k--;
printf("%c\n",fun(t,k)+'A');
}
}
signed main(void)
{
T = 1;
// OldTomato; cin>>T;
// read(T);
while(T--)
{
solve();
}
return 0;
}