AtCoder Beginner Contest 242
比賽連結
AtCoder Beginner Contest 242
D - ABC Transform
Problem Statement
You are given a string S consisting of A, B, C.
Let \(S^{(0)}\)
:=S. For \(i=1,2,3,…,\) let \(S^(i)\)
be the result of simultaneously replacing the characters of \(S^(i−1)\)
as follows: A → BC
, B → CA
, C → AB
.
Answer \(Q\) queries. The i-th query is as follows.
Print the ki-th character from the beginning of \(S(t_i)\).
Constraints
- \(S\) is a string of length between \(1\) and \(10^5\)(inclusive) consisting of \(A, B, C\).
- \(1≤Q≤10^5\)
- $0≤t_i≤10^{18}
- $1≤k_i≤min(10^18, the length of S^{(t_i)})
- $Q,t_i ,k_i are integers.
Input
Input is given from Standard Input in the following format:
S
Q
t1 k1
t2 k2
⋮
tQ kQ
Output
Process the \(Q\) queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.
Sample Input 1
ABC
4
0 1
1 1
1 3
1 6
Sample Output 1
A
B
C
B
Sample Input 2
CBBAACCCCC 5 57530144230160008 659279164847814847 29622990657296329 861239705300265164 509705228051901259 994708708957785197 176678501072691541 655134104344481648 827291290937314275 407121144297426665
Sample Output 2
A
A
C
A
A
解題思路
思維,遞迴
考慮當前字元是由前面那個字元轉移過來的,相當於一棵二叉樹,\(k\) 每次找根節點,當 \(k\) 為 \(0\) 時即由最開始的 \(0\) 轉移過來的,且轉移時是以迴圈轉移的
- 時間複雜度:\(O(logk)\)
程式碼
// 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)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
string s;
int q;
LL t,k;
int get(LL t,LL k)
{
if(t==0)return s[k]-'A';
else if(k==0)return (s[k]-'A'+t%3)%3;
else
return (get(t-1,k/2)+1+k%2)%3;
}
int main()
{
cin>>s;
cin>>q;
while(q--)
{
cin>>t>>k;
k--;
cout<<char(get(t,k)+'A')<<'\n';
}
return 0;
}
E - (∀x∀)
Problem Statement
Solve the following problem for \(T\) test cases.
Given an integer \(N\) and a string \(S\), find the number of strings \(X\) that satisfy all of the conditions below, modulo 998244353 .
- \(X\) is a string of length \(N\) consisting of uppercase English letters.
- \(X\) is a palindrome.
- \(X \leq S\) in lexicographical order.
-
- That is, \(X=S\) or \(X\) is lexicographically smaller than \(S\).
Constraints
- \(1 \leq T \leq 250000\)
- \(N\) is an integer between 1 and \(10^{6}\) (inclusive).
- In a single input, the sum of \(N\) over the test cases is at most \(10^{6}\).
- \(S\) is a string of length \(N\) consisting of uppercase English letters.
Input
Input is given from Standard Input in the following format:
\(T\)
case \(_{1}\)
case \(_{2}\)
case \(_{T}\)
Here, case \(_{i}\) represents the \(i\)-th test case.
Each test case is in the following format:
\(N\)
\(S\)
Output
Print \(T\) lines. The \(i\)-th line should contain the answer for the \(i\)-th test case as an integer.
Sample Input 1
5
3
AXA
6
A B C Z A Z
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW
Sample Output 1
24
29
212370247
36523399
231364016
This input contains five test cases.
Test case #1:
The 24 strings satisfying the conditions are AAA, ABA, ACA, ..., AXA.
Test case #2:
\(S\) may not be a palindrome.
Test case #3:
Be sure to find the count modulo \(998244353 .\)
解題思路
思維
找出前半部分,小於前半部分的迴文串肯定都是滿足條件的,這裡的 \(A\) 相當於十進位制的 \(1\),而對於長度為奇數的情況特判一下即可
- 時間複雜度:\(O(tlogn)\)
程式碼
// Problem: E - (∀x∀)
// Contest: AtCoder - AtCoder Beginner Contest 242
// URL: https://atcoder.jp/contests/abc242/tasks/abc242_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int mod=998244353;
int t,n;
string s;
LL get()
{
LL res=0;
string t=s.substr(0,(n+1)/2);
string t1=t;
reverse(t1.begin(),t1.end());
for(int i=0;i<t.size();i++)res=(res*26+t[i]-'A')%mod;
if(n&1)t1.erase(t1.begin());
if(t+t1<=s)res=(res+1)%mod;
return res;
}
int main()
{
for(cin>>t;t;t--)
{
cin>>n>>s;
cout<<get()<<'\n';
}
return 0;
}