atcoder abc242 - d题解
题目大意
给定由 A,B,C 组成的字符串串 ,每次操作中A能变成BC,B能变成CA,C能变成AB。共次询问,每次询问经过 次操作后,第 个字符是什么。
思路
我们先考虑使用暴力。 有 ,这么大,无法直接求出字符串,不可能实现。
题中的操作会将字符串长度乘上,所以的第个字符是由的第个字符变化过来。
得出这一结论后,我们先用代替A, B, C,可以得出,每次操作后,每个字符会变成和
通过以上结论,我们考虑使用递归分治解决这道题,每次,,直到或
时间复杂度:
标程
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string s;
int q, n;
ll t, k;
char dfs(ll t, ll k){
if(!t){
return s[k];
}
if(!k){
return (s[0] - 'A' + t) % 3 + 'A';
}
return (dfs(t - 1, k / 2) - 'A' + k % 2 + 1) % 3 + 'A';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> s >> q;
while(q--){
cin >> t >> k;
k--;
cout << dfs(t, k) << '\n';
}
return 0;
}