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(i1)
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(ti).

Constraints

  • S is a string of length between 1 and 105(inclusive) consisting of A,B,C.
  • 1Q105
  • $0≤t_i≤10^
  • $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 每次找根节点,当 k0 时即由最开始的 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.
  • XS in lexicographical order.
    • That is, X=S or X is lexicographically smaller than S.

Constraints

  • 1T250000
  • N is an integer between 1 and 106 (inclusive).
  • In a single input, the sum of N over the test cases is at most 106.
  • 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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15970367.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(140)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示