Translation Notice
This article was machine-translated using DeepSeek-R1.
- Original Version: Authored in Chinese by myself
- Accuracy Advisory: Potential discrepancies may exist between translations
- Precedence: The Chinese text shall prevail in case of ambiguity
- Feedback: Technical suggestions regarding translation quality are welcomed
C - 1111gal password
Problem Statement
Given a positive integer $N$, find the number of integers $X$ satisfying the following conditions, modulo $998244353$:
- $X$ is an $N$-digit positive integer
- Every digit of $X$ is between $[1,9]$ (==0 is forbidden==)
- The absolute difference between adjacent digits in $X$ is at most 1.
$2\le N\le 10^6$
Input Format
$N$
Output Format
Print the answer.
Sample
$N$ | Output |
---|---|
$4$ | $203$ |
$2$ | $25$ |
$1000000$ | $248860093$ |
Analysis
By the multiplication principle, the maximum possible count of valid $N$-digit numbers is $9^N$, which is clearly infeasible for brute force. However, since each digit is constrained by its predecessor, a dynamic programming (DP) approach is natural.
$$ f(i,j)=\begin{cases} 1&(i=1)\\ f(i-1,1)+f(i-1,2)&(j=1)\\ f(i-1,8)+f(i-1,9)&(j=9)\\ f(i-1,j-1)+f(i-1,j)+f(i-1,j+1)&(i>1,2\le j\le8) \end{cases} $$
The final answer is $\sum\limits_{i=1}^9f(n,i)$.
Code
This code employs rolling table optimization to reduce memory usage. Directly allocating an $N\times9$ array is possible but memory-intensive.
|
|
D - ABC Transform
Problem Statement
Given a string $S$ composed of A
, B
, C
. Define $S_0=S$ and $S_i$ as the string formed by replacing each character in $S_{i-1}$:
A
βBC
B
βCA
C
βAB
Answer $Q$ queries where the $i$-th query asks for the $k_i$-th character in $S_{t_i}$.
$1\le |S|\le 10^5$
$1\le Q\le 10^5$
$1\le t_i\le 10^{18}$
$1\le k_i\le min(10^{18},$ length of $S_{t_i})$
Input Format
$S$
$Q$
$t_1~k_1$
$\vdots$
$t_Q~k_Q$
Sample
Sample Input 1
|
|
Sample Output 1
|
|
- $S_0=~$
ABC
- $S_1=~$
AABCB
Sample Input 2
|
|
Sample Output 2
|
|
Beware of integer overflow issues.
Analysis
$$ f(t,k)=\begin{cases} 0 & (t=0)\\ g(0,t) & (k=0)\\ g(f(t-1,\lfloor\frac k2\rfloor),(k\bmod2)+1) & (t>0,k>0) \end{cases} $$
where $g(c,x)$ returns the $(c+x)$-th character modulo 3 in the cyclic sequence AβBβCβA...
.
The solution involves determining which original character in $S$ generates the queried position. The final answer is $\mathrm{ans}=g(f(t,(k-1)\bmod2^t),S_{\lfloor\frac {k-1}{2^t}\rfloor})$.
Code
Both implementations below use iterative approaches. Recursive versions are also feasible.
Code 1 (Standard)
|
|
Code 2 (Optimized)
|
|
E - (βxβ)
Problem Statement
For $T$ test cases, solve the following:
Given integer $N$ and string $S$, count the number of valid palindromic strings $X$ satisfying:
- $|X|=N$
- $X$ consists of uppercase letters
- $X\le S$ lexicographically
$1\le T\le 250000$
$1\le N\le 10^6$
$1\le \sum N\le 10^6$
$|S|=N$ with uppercase letters.
Analysis
The palindrome $X$ is uniquely determined by its first $\lceil\frac N2\rceil$ characters. Consider ABCDE
:
- The first 3 characters are
ABC
- There are $28$ valid prefixes lex-order smaller than
ABC
(treated as a base-26 number) - Check if
ABCBA
β€ABCDE
- If valid, total becomes $28+1=29$
Thus, output $29$. Other cases follow similarly.
Code
|
|