Atcoder - Editorials From 29-07-2023 To 19-08-2023 (6 Weeks)
Atcoder - Editorials From 29-07-2023 To 19-08-2023 (6 Weeks)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by a user
“en_translator”
CONTEST PROBLEM
STATEMENTS
A - Order Something Else
Problem Statement
Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P
yen.
He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must
additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, … , N , the
price of the i-th dish is Di yen.
Print the minimum total amount of money that he must pay to get the drink.
Constraints
1 ≤ N ≤ 100
1 ≤ Q < P ≤ 105
1 ≤ Di ≤ 105
Input
The input is given from Standard Input in the following format:
N P Q
D1 D2 … DN
Output
Print the answer.
Sample Input 1
3 100 50
60 20 40
Sample Output 1
70
If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen
for the dish, for a total of 70 yen, which is the minimum total payment needed.
Sample Input 2
3 100 50
60000 20000 40000
Sample Output 2
100
The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.
B - Strictly Superior
Problem Statement
AtCoder Shop has N products. The price of the i-th product (1 ≤ i ≤ N ) is Pi . The i-th product (1 ≤
Takahashi wonders whether there is a product that is strictly superior to another. If there are i and j
(1 ≤ i, j ≤ N ) such that the i-th and j -th products satisfy all of the following conditions, print Yes;
otherwise, print No.
Pi ≥ Pj .
Constraints
2 ≤ N ≤ 100
1 ≤ M ≤ 100
1 ≤ Pi ≤ 105 (1 ≤ i ≤ N )
1 ≤ Ci ≤ M (1 ≤ i ≤ N )
Input
The input is given from Standard Input in the following format:
N M
P1 C1 F1,1 F1,2 … F1,C1
⋮
PN CN FN ,1 FN ,2 … FN ,CN
Output
Print the answer in a single line.
Sample Input 1
5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6
Sample Output 1
Yes
Sample Input 2
4 4
3 1 1
3 1 2
3 1 2
4 2 2 3
Sample Output 2
No
Sample Output 3
Yes
C - Reversible
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on
it.
For each i = 1, 2, … , N , the letters written on the balls stuck onto the i-th stick are represented by a
string Si . Specifically, the number of balls stuck onto the i-th stick is the length ∣Si ∣ of the string Si , and
Si is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one
stick is equal to the sequence of letters starting from one end of the other stick. More formally, for
integers i and j between 1 and N , inclusive, the i-th and j -th sticks are considered the same if and only if
Si equals Sj or its reversal.
Constraints
N is an integer.
2 ≤ N ≤ 2 × 105
Si is a string consisting of lowercase English letters.
∣Si ∣ ≥ 1
5
∑N i=1 ∣Si ∣ ≤ 2 × 10
Input
The input is given from Standard Input in the following format:
N
S1
S2
⋮
SN
Output
Print the answer.
Sample Input 1
6
a
abc
de
cba
de
abc
Sample Output 1
3
S2 = abc equals the reversal of S4 = cba, so the second and fourth sticks are considered the same.
S2 = abc equals S6 = abc, so the second and sixth sticks are considered the same.
S3 = de equals S5 = de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth),
and third (same as the fifth).
D - Peaceful Teams
Problem Statement
There are N sports players.
Among them, there are M incompatible pairs. The i-th incompatible pair (1 ≤ i ≤ M ) is the Ai -th and
Bi -th players.
You will divide the players into T teams. Every player must belong to exactly one team, and every team
must have one or more players. Additionally, for each i = 1, 2, … , M , the Ai -th and Bi -th players must
Find the number of ways to satisfy these conditions. Here, two divisions are considered different when
there are two players who belong to the same team in one division and different teams in the other.
Constraints
1 ≤ T ≤ N ≤ 10
N (N − 1)
0≤M ≤
2
1 ≤ Ai < Bi ≤ N (1 ≤ i ≤ M )
(Ai , Bi )
= (Aj , Bj ) (1 ≤ i < j ≤ M )
Input
The input is given from Standard Input in the following format:
N T M
A1 B1
A2 B2
⋮
AM BM
Output
Print the answer in a single line.
Sample Input 1
5 2 2
1 3
3 4
Sample Output 1
4
Sample Input 2
5 1 2
1 3
3 4
Sample Output 2
0
Sample Output 3
65
Sample Input 4
10 6 8
5 9
1 4
3 8
1 6
4 10
5 7
5 6
3 7
Sample Output 4
8001
E - NAND repeatedly
Problem Statement
You are given a string S of length N consisting of 0 and 1. It describes a length-N sequence A =
(A1 , A2 , … , AN ). If the i-th character of S (1 ≤ i ≤ N ) is 0, then Ai = 0; if it is 1, then Ai = 1.
1≤i≤j≤N
N N
More formally, find ∑ ∑ f (i, j) for f (i, j) (1
≤ i ≤ j ≤ N ) defined as follows:
i=1 j=i
(i = j)
f (i, j) = {
Ai
f (i, j − 1) ⊼ Aj (i < j)
0 ⊼ 0 = 1, 0 ⊼ 1 = 1, 1 ⊼ 0 = 1, 1 ⊼ 1 = 0.
Constraints
1 ≤ N ≤ 106
S is a string of length N consisting of 0 and 1.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
S
Output
Print the answer in a single line.
Sample Input 1
5
00110
Sample Output 1
9
Here are the values of f (i, j) for the pairs (i, j) such that 1 ≤ i ≤ j ≤ N:
f (1, 1) = 0 = 0
f (1, 2) = 0 ⊼ 0 = 1
f (1, 3) = (0 ⊼ 0) ⊼ 1 = 0
f (1, 4) = ((0 ⊼ 0) ⊼ 1) ⊼ 1 = 1
f (1, 5) = (((0 ⊼ 0) ⊼ 1) ⊼ 1) ⊼ 0 = 1
f (2, 2) = 0 = 0
f (2, 3) = 0 ⊼ 1 = 1
f (2, 4) = (0 ⊼ 1) ⊼ 1 = 0
f (2, 5) = ((0 ⊼ 1) ⊼ 1) ⊼ 0 = 1
f (3, 3) = 1 = 1
f (3, 4) = 1 ⊼ 1 = 0
f (3, 5) = (1 ⊼ 1) ⊼ 0 = 1
f (4, 4) = 1 = 1
f (4, 5) = 1 ⊼ 0 = 1
f (5, 5) = 0 = 0
Their sum is 0 + 1 + 0 + 1 + 1 + 0 + 1 + 0 + 1 + 1 + 0 + 1 + 1 + 1 + 0 = 9, so print 9.
Note that ⊼ does not satisfy the associative property. For instance, (1 ⊼ 1) ⊼ 0 =0⊼0=1=
0=
1 ⊼ 1 = 1 ⊼ (1 ⊼ 0).
Sample Input 2
30
101010000100101011010011000010
Sample Output 2
326
F - Make 10 Again
Time Limit: 2 sec / Memory Limit: 1024 MB
Problem Statement
We have N dice. For each i = 1, 2, … , N , when the i-th die is thrown, it shows a random integer
between 1 and Ai , inclusive, with equal probability.
Find the probability, modulo 998244353, that the following condition is satisfied when the N dice are
thrown simultaneously.
There is a way to choose some (possibly all) of the N dice so that the sum of their results is 10.
Constraints
1 ≤ N ≤ 100
1 ≤ Ai ≤ 106
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the answer.
Sample Input 1
4
1 7 2 9
Sample Output 1
942786334
For instance, if the first, second, third, and fourth dice show 1, 3, 2, and 7, respectively, these results
satisfy the condition. In fact, if the second and fourth dice are chosen, the sum of their results is 3 + 7 =
10. Alternatively, if the first, third, and fourth dice are chosen, the sum of their results is 1 + 2 + 7 = 10.
On the other hand, if the first, second, third, and fourth dice show 1, 6, 1, and 5, respectively, there is no
way to choose some of them so that the sum of their results is 10, so the condition is not satisfied.
11
In this sample input, the probability of the results of the N dice satisfying the condition is 18 . Thus, print
Sample Input 2
7
1 10 100 1000 10000 100000 1000000
Sample Output 2
996117877
G - Takahashi And Pass-The-Ball Game
Problem Statement
There are N Takahashi.
An integer x between 1 and K , inclusive, will be chosen uniformly at random, and they will repeat the
following operation x times.
For every i, the i-th Takahashi gives all his balls to the Ai -th Takahashi.
For each i = 1, 2, … , N , find the expected value, modulo 998244353, of the number of balls the i-th
Takahashi has at the end of the operations.
Constraints
1 ≤ N ≤ 2 × 105
1 ≤ K ≤ 1018
K is not a multiple of 998244353.
1 ≤ Ai ≤ N (1 ≤ i ≤ N )
0 ≤ Bi < 998244353 (1 ≤ i ≤ N )
Input
The input is given from Standard Input in the following format:
N K
A1 A2 ⋯ AN
B1 B2 ⋯ BN
Output
Print the expected value of the number of balls the i-th Takahashi has at the end of the operations for
i = 1, 2, … , N , separated by spaces, in a single line.
Sample Input 1
5 2
3 1 4 1 5
1 1 2 3 5
Sample Output 1
3 0 499122179 499122178 5
During two operations, the five Takahashi have the following number of balls.
Sample Output 2
111 0 0
After one or more operations, the first Takahashi gets all balls.
Sample Input 3
16 1000007
16 12 6 12 1 8 14 14 5 7 6 5 9 6 10 9
719092922 77021920 539975779 254719514 967592487 476893866 368936979 465399362 342544824 540338192
42663741 165480608 616996494 16552706 590788849 221462860
Sample Output 3
817852305 0 0 0 711863206 253280203 896552049 935714838 409506220 592088114 0 413190742 0 363914270
0 14254803
Sample Input 4
24 100000000007
19 10 19 15 1 20 13 15 8 23 22 16 19 22 2 20 12 19 17 20 16 8 23 6
944071276 364842194 5376942 671161415 477159272 339665353 176192797 2729865 676292280 249875565 259
803120 103398285 466932147 775082441 720192643 535473742 263795756 898670859 476980306 12045411 620
291602 593937486 761132791 746546443
Sample Output 4
918566373 436241503 0 0 0 455245534 0 356196743 0 906000633 0 268983266 21918337 0 733763572 173816
039 754920403 0 273067118 205350062 0 566217111 80141532 0
Ex - Negative Cost
Problem Statement
A monster with health H has appeared right in front of you. Your magic power is now 0.
You can use N moves called move 1, move 2, …, move N , any number of times in any order.
For each i= 1, 2, … , N , move i can only be used when your magic power is at least Ci , and its use will
decrease your magic power by Ci and the monster's health by Di . Here, if Ci is negative, decreasing your
Find the minimum possible number of times you use moves before the monster's health is 0 or lower. The
constraints of this problem guarantee that a finite number of uses of moves can make it 0 or lower (see
below).
Constraints
1 ≤ N ≤ 300
1 ≤ H ≤ 1018
−300 ≤ Ci ≤ 300
Ci ≤ 0 for some 1 ≤ i ≤ N .
1 ≤ Di ≤ 109
Input
The input is given from Standard Input in the following format:
N H
C1 D1
C2 D2
⋮
CN DN
Output
Print the answer.
Sample Input 1
3 48
3 20
-4 2
1 5
Sample Output 1
5
From the initial state where your magic power is 0 and the monster's health is 48, consider using moves as
follows.
Use move 2. Now, your magic power is 4, and the monster's health is 46.
Use move 3. Now, your magic power is 3, and the monster's health is 41.
Use move 1. Now, your magic power is 0, and the monster's health is 21.
Use move 2. Now, your magic power is 4, and the monster's health is 19.
Use move 1. Now, your magic power is 1, and the monster's health is −1.
Here, you use moves five times before the monster's health is not greater than 0, which is the minimum
possible number.
Sample Input 2
20 583988303060450752
-64 273760634
-238 960719353
-114 191410838
-250 357733867
232 304621362
-286 644706927
210 37849132
-230 556412112
-142 136397527
101 380675202
-140 152300688
190 442931589
-187 940659077
-12 312523039
32 126515475
-143 979861204
105 488280613
240 664922712
290 732741849
69 541282303
Sample Output 2
595990842
HINTS, ALGORITHMS AND
SAMPLE
IMPLEMENTATIONS FOR
EACH PROBLEM
STATEMENTS
A – ORDER SOMETHING ELSE
Algorithm:
1.2. Read the integer `p` - the price when the coupon is not used.
1.3. Read the integer `q` - the price when the coupon is used and the cheapest dish is ordered.
2.1. Ini!alize the variable `d_min` to a large value, e.g., 1000000000, to ensure that the first dish's
price will be smaller than `d_min`.
2.2. Ini!alize a temporary variable `d` to store the price of each dish while inspec!ng them.
3.1. Start a loop from `i = 1` to `n` (inclusive) using a for loop, to inspect each dish one by one.
3.2. Within the loop, read the price of the current dish and store it in the variable `d`.
3.3. Update the value of `d_min` to be the minimum between the current `d` and the current value of
`d_min`. This will help keep track of the cheapest dish price encountered so far.
4.1. A4er the loop, compute the minimum price between `p` and `q + d_min`.
#include <iostream>
int main(void)
int n, p, q;
cin >> d;
return 0;
This algorithm will help you find and print the cheapest price between ordering without a coupon (`p`)
and ordering with a coupon for the cheapest dish (`q + d_min`).
B – STRICTLY SUPERIOR
Algorithm:
1.1. Read the integers `N` and `M` from the input - `N` represents the number of products, and `M`
represents the number of features each product can have.
2.1. Create a vector of pairs called `products`, where each pair consists of an integer `price` and a
vector of integers `feature`.
2.2. Iterate `N` !mes to read the product informa!on for each product.
2.4. Read the integer `K` - the number of features for the current product.
2.6. Read `K` integers and store them in the `feature` vector to represent the features of the current
product.
3.1. Create a func!on called `is_strictly_superior`, which takes two pairs of `int` and `vector<int>` as
input parameters represen!ng two products - `product_i` and `product_j`.
3.2. Extract `price_i` and `feature_i` from `product_i`, and `price_j` and `feature_j` from `product_j`.
3.3. Check if the price of `product_i` is greater than or equal to the price of `product_j`.
3.4. Use the `std::includes` func!on to check if all the features of `product_i` are contained in the
features of `product_j`.
3.5. Also, check if the price of `product_i` is strictly greater than the price of `product_j`, or if the prices
are the same but the features of `product_j` are not contained in the features of `product_i`.
4.1. Create a func!on called `has_superior_pair`, which takes the `products` vector as an input
parameter.
4.2. Iterate through each product in the `products` vector using `any_of` func!on to check if there
exists any product (`product_i`) that has a superior pair.
4.3. For each `product_i`, again use `any_of` func!on to check if there exists any product (`product_j`)
in the `products` vector such that `product_j` is strictly superior to `product_i` using the
`is_strictly_superior` func!on.
Step 5: Check if a superior pair exists
5.1. Call the `has_superior_pair` func!on with the `products` vector as an argument.
5.2. If the func!on returns `true`, output "Yes"; otherwise, output "No".
#include <iostream>
#include <vector>
#include <u!lity>
#include <algorithm>
});
});
}
int main() {
int N, M;
int K;
cin >> K;
feature = vector<int>(K);
cin >> f;
return 0;
```
This algorithm will determine if there exists a pair of products in the given list such that one product is
strictly superior to the other, based on their prices and features.
C-Reversible
Algorithm:
1.1. Read the integer `n` from the input, represen!ng the number of s!cks.
2.1. Create a variable `ans` and set it to 0. This variable will keep track of the number of dis!nct s!cks
encountered so far.
2.2. Create an empty `set<string>` called `T`. This set will store the strings of the s!cks inspected so far,
along with their reversals.
3.1. Start a loop from `i = 1` to `n` (inclusive) using a for loop, to inspect each s!ck one by one.
3.2. Within the loop, read the string of the current s!ck and store it in the variable `s`.
3.3. Check if the string `s` is not present in the set `T` by using the `count()` func!on. If it is not present,
increment the value of `ans` by 1, as it indicates that this s!ck is a dis!nct one.
3.4. Insert the string `s` into the set `T` using `insert()` to keep track of it.
3.6. Check if the reversed string of `s` is not present in the set `T`. If it is not present, increment the
value of `ans` by 1, as the reversed string is also dis!nct.
3.7. Insert the reversed string of `s` into the set `T` to keep track of it.
4.1. A4er the loop, print the value of `ans`, which represents the number of dis!nct s!cks encountered.
#include <set>
#include <algorithm>
int main(void)
int n;
cin >> n;
int ans = 0;
set<string> T;
string s;
cin >> s;
if (T.count(s) == 0)
ans++;
T.insert(s);
reverse(s.begin(), s.end());
T.insert(s);
return 0;
This algorithm will efficiently count the number of dis!nct s!cks by using a balanced binary tree
(implemented as a `std::set`) to keep track of the strings of the s!cks and their reversals inspected so
far. The algorithm ensures that it finishes within the execu!on !me limit by using the set's fast lookup
opera!on.
D – Peaceful Teams:
Algorithm:
The problem can be solved by adding players to a team one by one with certain constraints on player
incompa!bility. The main idea is to use a recursive func!on to explore all possible combina!ons of
dividing players into teams while ensuring that no incompa!ble players belong to the same team.
1.1. Read the integers `N`, `T`, and `M` from the input.
- `M` represents the number of pairs of players who are incompa!ble with each other.
2.1. Create a vector called `hate` of size `N`, where each element is an unsigned integer.
2.2. Iterate `M` !mes to read the incompa!bility pairs and store them in the `hate` vector.
- Each pair consists of integers `a` and `b`, where `a` and `b` are player indices (0-indexed) who are
incompa!ble with each other.
- Set the corresponding bits in the `hate` vector to indicate player incompa!bility.
3.1. Create a lambda func!on called `dfs`, which takes the following parameters:
- `vector<unsigned>& teams`: A reference to the vector that holds the teams' bitmasks.
3.2. The `dfs` func!on returns the number of valid combina!ons to divide players into teams.
- If all `N` players are considered (`now == N`), check if the number of teams formed is equal to the
desired number of teams `T`. If yes, return 1; otherwise, return 0.
- If the current player `now` can be added to an exis!ng team without causing any incompa!bility
issues, try adding the player to each exis!ng team. Recursively call `dfs` for the next player (`now + 1`),
considering all possible combina!ons.
- If there are fewer teams formed than the desired number of teams `T`, try crea!ng a new team
with the current player. Recursively call `dfs` for the next player (`now + 1`), considering all possible
combina!ons.
5.1. Call the `dfs` func!on with an ini!al empty vector for teams and the star!ng player index `0`.
5.2. Print the result obtained from the `dfs` func!on, which represents the number of valid
combina!ons to divide players into teams without any incompa!ble players in the same team.
```cpp
#include <iostream>
#include <vector>
int main() {
unsigned N, T, M;
// j-th bit of hate[i] is 1 ⟹ i-th and j-th players are incompa!ble (0-indexed)
vector<unsigned> hate(N);
}
// Define the recursive func!on dfs
// OK if there are T teams so far when all the N players are inspected
if(now == N)
return size(teams) == T;
unsigned ans{};
teams.pop_back();
return ans;
}};
cout << [dfs{[N, T, &hate](auto&& f, vector<unsigned>& teams, unsigned now) -> unsigned {
if(now == N)
return size(teams) == T;
unsigned ans{};
teams.pop_back();
return ans;
}}, T]{
vector<unsigned> team;
team.reserve(T);
return 0;
```
This algorithm efficiently explores all possible combina!ons of dividing players into teams while
ensuring that no incompa!ble players belong to the same team. It uses a recursive approach and
bitwise opera!ons to achieve a !me complexity of O(N * N!) with a reasonable number of players.
E – NAND repeatedly
Algorithm
We can efficiently solve the problem by managing the counts of '0' and '1' in the input string 'A'. The
algorithm has a !me complexity of Θ(N) since it only iterates through the input string once, and it does
not require any nested loops or expensive opera!ons.
1.1. Read the integer `N` from the input, represen!ng the length of the string.
1.2. Read the string `A` from the input, which consists of '0's and '1's.
2.1. Create two variables `zero` and `one`, both ini!alized to 0. These variables will keep track of the
counts of '0's and '1's in the processed part of the string.
2.2. Create a variable `ans` and set it to 0. This variable will keep track of the total sum of '1's
encountered so far.
3.2. Inside the loop, update the counts of '0's and '1's based on the current character `a`.
- If `a` is '0', update `zero` to 1 and `one` to the sum of their previous values (`zero + one`).
- If `a` is '1', update `zero` to the sum of their previous values (`zero + one`) and `one` to the sum of
their previous values plus 1 (`zero + one + 1`).
3.3. Add the value of `one` to the `ans` variable to keep track of the total sum of '1's.
4.1. A4er the loop, print the value of `ans`, which represents the sum of '1's obtained by processing
the string `A`.
Python:
N = int(input())
A = input()
zero, one = 0, 0
ans = 0
for a in A:
if a == '0':
else:
ans += one
print(ans)
C++:
#include <vector>
#include <iostream>
#include <u!lity>
int main() {
unsigned long N;
cin >> N;
vector<char> A(N);
count_t counter;
ans += one;
return 0;
Both the Python and C++ implementa!ons efficiently solve the problem and produce the correct
output in linear !me, making the algorithm much more efficient than the ini!al Θ(N^2)-!me solu!on.
G - Takahashi And Pass-The-Ball Game
HINTS:
Algorithm:
With this we can efficiently solves the "Takahashi And Pass-The-Ball Game" problem using the doubling
technique and modular arithme!c. The problem requires finding the number of balls each player will
have a4er performing the pass-the-ball opera!on K !mes.
1.1. Read the integers `N` and `K` from the input.
2.1. Compute `K_inv` using the `atcoder::sta!c_modint` library provided by AtCoder. This step is
necessary for dividing the answer by K later.
3.1. Create a vector called `A` of size `N`, which will store the target player to whom each player will
pass the ball.
3.2. Create a vector called `ball` of size `N`, which will store the number of balls each player currently
has.
5.1. Use the `while` loop to perform the doubling technique to compute the final result efficiently.
5.2. Within the loop, if K is odd, consider the first opera!on and the remaining opera!ons separately.
5.3. Put the two opera!ons together using the `add` func!on and apply the current opera!on to the
`ball` vector.
5.4. Update the `A` vector using the `compose` func!on to represent the composi!on of opera!ons i
→ B[i] → A[B[i]].
6.1. A4er the loop, mul!ply the result vector `ans` by `K_inv` to find the answer.
6.2. Print each element of the `ans` vector separated by spaces to represent the number of balls each
player has a4er K opera!ons.
#include <bits/extc++.h>
#include <atcoder/modint>
template<int m>
os << mint.val();
return os;
int main() {
unsigned long N, K;
for (auto&& a : A) {
cin >> a;
--a;
vector<modint> ball(N);
int x;
cin >> x;
b = modint::raw(x);
A = result;
}};
vector<modint> result(N);
return result;
}};
}};
vector<modint> ans(N);
while (K) {
add(ans, ball);
compose(A, A);
K /= 2;
for (const auto& x : ans) cout << x * K_inv << " ";
return 0;
This algorithm efficiently solves the problem using the doubling technique and modular arithme!c,
making it suitable for large values of N and K. The !me complexity is O(NlogK + logmod), where mod
is the modular value (998244353 in this case).
Note & Credits: Problem statement and Hints from Atcoder.jp by an user en_translator
Thank you.
A DETAILED EDITORIAL FOR
ATCODER - TOYOTA PROGRAMMING CONTEST 2023
(ATCODER BEGINNER CONTEST 311)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by the users
“en_translator (English) & physics (Japanese)”
CONTEST PROBLEM
STATEMENTS, HINTS AND
ALGORITHMS WITH CODE
A - First ABC Editorial
/
Score : 100 points
Problem Statement
You are given a string S consisting of A, B, and C. S is guaranteed to contain all of A, B, and C.
If the characters of S are checked one by one from the left, how many characters will have been checked when the following condition is satisfied for
the first time?
Constraints
3 ≤ N ≤ 100
S is a string of length N consisting of A, B, and C.
S contains all of A, B, and C.
Input
The input is given from Standard Input in the following format:
N
S
Output
Print the answer.
Copy
5
ACABB
Copy
4
In the first four characters from the left, A, B, and C appear twice, once, and once, respectively, satisfying the condition.
The condition is not satisfied by checking three or fewer characters, so the answer is 4.
Copy
4
CABC
Copy
3
In the first three characters from the left, each of A, B, and C appears once, satisfying the condition.
Copy
30
AABABBBABABBABABCABACAABCBACCA
Sample Output 3 Copy
Copy
17
A. First ABC
Algorithm:
Step 1: Input an integer 'N', which represents the length of the string 'S'.
Step 3: Ini alize three boolean variables, 'exist_a', 'exist_b', and 'exist_c', to False. These
variables will be used to track if characters 'A', 'B', and 'C' have appeared in the string 'S'.
Step 4: Start a loop that iterates over the characters in the string 'S' using the index 'i' from 0
to N-1.
Step 5: Check if the current character 'S[i]' is 'A'. If it is, set the boolean variable 'exist_a' to
True, indica ng that 'A' has been found in the string.
Step 6: Check if the current character 'S[i]' is 'B'. If it is, set the boolean variable 'exist_b' to
True, indica ng that 'B' has been found in the string.
Step 7: Check if the current character 'S[i]' is 'C'. If it is, set the boolean variable 'exist_c' to
True, indica ng that 'C' has been found in the string.
Step 8: Check if all three characters 'A', 'B', and 'C' have been found in the string. If 'exist_a',
'exist_b', and 'exist_c' are all True, it means that all three characters have appeared.
Step 9: If all three characters have appeared, print the posi on (index + 1) of the first
occurrence where they appear consecu vely (i.e., the posi on 'i + 1').
Step 10: Break out of the loop since the first occurrence of 'A', 'B', and 'C' appearing
consecu vely has been found.
Step 11: The program terminates a9er the first occurrence is printed, or it finishes itera ng
through the en re string 'S' without finding the desired pa:ern.
Code:
N = int(input())
S = input()
for i in range(N):
print(i + 1)
break
B - Vacation Together Editorial
/
Score : 200 points
Problem Statement
There are N people numbered 1 to N .
You are given their schedule for the following D days. The schedule for person i is represented by a string Si of length D . If the j -th character of Si is o,
person i is free on the j -th day; if it is x, they are occupied that day.
From these D days, consider choosing some consecutive days when all the people are free.
How many days can be chosen at most? If no day can be chosen, report 0.
Constraints
1 ≤ N ≤ 100
1 ≤ D ≤ 100
N and D are integers.
Si is a string of length D consisting of o and x.
Input
The input is given from Standard Input in the following format:
N D
S1
S2
⋮
SN
Output
Print the maximum number of days that can be chosen, or 0 if no day can be chosen.
Copy
3 5
xooox
oooxx
oooxo
Copy
2
All the people are free on the second and third days, so we can choose them.
Choosing these two days will maximize the number of days among all possible choices.
Copy
3 3
oxo
oxo
oxo
Copy
1
Note that the chosen days must be consecutive. (All the people are free on the first and third days, so we can choose either of them, but not both.)
Copy
3 3
oox
oxo
xoo
Copy
0
Copy
1 7
ooooooo
Copy
7
Copy
5 15
oxooooooooooooo
oxooxooooooooox
oxoooooooooooox
oxxxooooooxooox
oxooooooooxooox
Copy
5
B. Vaca on Together
Algorithm:
This C++ code takes two integer inputs, 'N' and 'D', followed by an array of strings 'S' of size 'N'. The
code then finds the maximum number of consecu#ve columns in the matrix where all the elements
in each column are 'o'. Finally, it prints the maximum number of consecu#ve columns with all 'o's.
Let's go through the code step by step:
Step 1: Input integers 'N' and 'D', represen#ng the number of rows and columns in the matrix,
respec#vely.
Step 2: Input 'N' strings and store them in the vector 'S', represen#ng the matrix.
Step 3: Ini#alize variables 'ans' and 'cur' to 0. 'ans' will keep track of the maximum number of
consecu#ve columns with all 'o's, and 'cur' will keep track of the current consecu#ve columns.
Step 4: Start a loop that iterates over each column (j) from 0 to 'D-1'.
Step 5: Ini#alize the variable 'can' to 1. 'can' will be used to check if all elements in the current
column are 'o'.
Step 6: For each column 'j', loop through all the rows (i) from 0 to 'N-1'.
Step 7: Check if the element at row 'i' and column 'j' (S[i][j]) is 'o'. If it is not 'o', set 'can' to 0,
indica#ng that the current column doesn't meet the condi#on of having all 'o's.
Step 8: A9er the inner loop, if 'can' is s#ll 1, it means that all elements in the current column are 'o'.
Step 9: Increment the value of 'cur' to represent the number of consecu#ve columns with all 'o's.
Step 10: If 'can' is 0, it means that the current column does not have all 'o's, so reset 'cur' to 0.
Step 11: Update 'ans' to be the maximum value between 'ans' and 'cur', ensuring it keeps track of the
longest consecu#ve sequence of columns with all 'o's seen so far.
Step 13: Print the final value of 'ans', which represents the maximum number of consecu#ve
columns with all 'o's.
#include <iostream>
#include <string>
#include <vector>
int main() {
int N, D;
vector<string> S(N);
for (auto& s : S) cin >> s;
int can = 1;
if (can) {
cur++;
} else {
cur = 0;
}
C - Find it! Editorial
/
Score : 350 points
Problem Statement
There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex Ai . (The constraints guarantee that i
=
Ai .)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.
Notes
The sequence of vertices B = (B1 , B2 , … , BM ) is called a directed cycle when all of the following conditions are satisfied:
M ≥2
The edge from vertex Bi to vertex Bi+1 exists. (1 ≤ i ≤ M − 1)
The edge from vertex BM to vertex B1 exists.
If i =
j , then Bi
= Bj .
Constraints
All input values are integers.
2 ≤ N ≤ 2 × 105
1 ≤ Ai ≤ N
Ai
=i
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print a solution in the following format:
M
B1 B2 … BM
M is the number of vertices, and Bi is the i-th vertex in the directed cycle.
2≤M
Bi+1 = ABi ( 1 ≤ i ≤ M − 1 )
B1 = AB M
Bi
= Bj ( i =
j )
Copy
7
6 7 2 1 3 4 5
Copy
4
7 5 3 2
4
2 7 5 3
3
4 1 6
Copy
2
2 1
Copy
2
1 2
Copy
8
3 7 4 7 3 3 8 2
Copy
3
2 7 8
Algorithm:
This C++ code takes an integer 'n' as input, followed by an array 'a' of size 'n'. The code finds and prints
a cycle in a given directed graph represented by the array 'a'. The graph is assumed to have nodes
numbered from 1 to n, and the values in the array 'a' represent the next node in the directed graph.
Step 1: Input the integer 'n', which represents the number of nodes in the directed graph.
Step 2: Create a vector 'a' of size 'n+1' to store the directed graph's edges. The index represents the
node number, and the value at index i represents the next node in the directed graph from node i.
Step 4: Create a vector 'fl' of size 'n+1' and ini)alize all its elements to 0. This vector will be used to
mark visited nodes during the cycle-finding process.
Step 5: Create an empty vector 's' to store the nodes visited in the current traversal.
Step 6: Ini)alize 'v' to 1. This variable represents the current node being explored, and we can start
from any vertex (here, we start from vertex 1).
Step 7: While the node 'v' is not visited (fl[v] is 0), do the following:
Add the current node 'v' to the vector 's' to keep track of the traversal path.
Update 'v' to be the next node in the directed graph, i.e., 'v = a[v]'.
Step 8: A:er the above loop, we have either encountered a cycle or reached the end of a connected
component.
Step 9: Create an empty vector 'res' to store the nodes that belong to the cycle.
Step 10: Traverse through the vector 's' and add the nodes to 'res' un)l we reach the node 'v' again.
This means that we have detected a cycle, and we add all the nodes from 's' that belong to the cycle
to 'res'.
Step 11: Print the size of the cycle (number of nodes) followed by the nodes that form the cycle.
Step 12: The program terminates a:er prin)ng the cycle informa)on.
The code essen)ally performs a depth-first search (DFS) on the directed graph, keeping track of the
nodes visited to detect cycles efficiently.
Code:
#include<bits/stdc++.h>
int main(){
int n;
cin >> n;
vector<int> a(n+1);
for(int i=1;i<=n;i++){
vector<int> fl(n+1,0),s;
while(fl[v]==0){
fl[v]=1;
s.push_back(v);
v=a[v];
vector<int> res;
if(nx==v){v=-1;}
if(v==-1){res.push_back(nx);}
for(int i=0;i<res.size();i++){
return 0;
}
D - Grid Ice Floor Editorial
/
Score : 400 points
Problem Statement
There is an N × M grid and a player standing on it.
Let (i, j) denote the square at the i-th row from the top and j -th column from the left of this grid.
Each square of this grid is ice or rock, which is represented by N strings S1 , S2 , … , SN of length M as follows:
The outer periphery of this grid (all squares in the 1-st row, N -th row, 1-st column, M -th column) is rock.
Find the number of ice squares the player can touch (pass or rest on).
Constraints
3 ≤ N , M ≤ 200
Si is a string of length M consisting of # and ..
Input
The input is given from Standard Input in the following format:
N M
S1
S2
⋮
SN
Output
Print the answer as an integer.
Copy
6 6
######
#....#
#.#..#
#..#.#
#....#
######
Copy
12
For instance, the player can rest on (5, 5) by moving as follows:
Copy
21 25
#########################
#..............###...####
#..............#..#...###
#........###...#...#...##
#........#..#..#........#
#...##...#..#..#...#....#
#..#..#..###...#..#.....#
#..#..#..#..#..###......#
#..####..#..#...........#
#..#..#..###............#
#..#..#.................#
#........##.............#
#.......#..#............#
#..........#....#.......#
#........###...##....#..#
#..........#..#.#...##..#
#.......#..#....#..#.#..#
##.......##.....#....#..#
###.............#....#..#
####.................#..#
#########################
Copy
215
D- Grid Ice Floor
Algorithm:
This C++ code is an implementa on of a breadth-first search (BFS) algorithm to find the number of
cells reachable in a grid-based game. The game grid is represented by a 2D matrix, and the player can
move in four direc ons (up, down, le', right) un l it hits a rock (represented by the character '#'). The
player can also choose to stop moving (represented by the character '*'). The goal is to find the
maximum number of reachable cells a'er following these rules. Here's a step-by-step explana on of
the code:
Step 1: Input integers 'n' and 'm', represen ng the number of rows and columns in the grid.
Step 2: Input the 'n' rows of the grid represented by strings and store them in the vector 's'.
Step 3: Ini alize a vector 'fl' of size '5 * n * m' (5 mes the total number of cells) and set all its elements
to 0. This vector will be used to mark visited cells during the BFS traversal.
Step 4: Create a queue 'q' to store cell posi ons during BFS traversal.
Step 5: Ini alize the star ng cell posi on as (x, y) = (m+1, 4) and direc on 'd' = 4 (stop). The star ng
cell is chosen arbitrarily to start the BFS.
Step 6: Mark the star ng cell as visited (fl[5 * (m + 1) + 4] = 1) and add it to the queue.
Step 7: Start the BFS loop, which runs while the queue is not empty.
Step 8: Pop a cell posi on 'od' from the front of the queue.
Step 9: Convert the cell posi on 'od' to x, y, and d using simple arithme c opera ons.
Step 10: If the direc on 'd' is 4 (stop), try to move in all four direc ons (up, down, le', right) and check
if the next cell is empty ('.'). If it is, mark the cell as visited and add it to the queue with the new
direc on.
Step 11: If the direc on 'd' is not 4, it means the player can con nue moving in the current direc on.
Check if the next cell in the same direc on is empty ('.'). If it is, mark the cell as visited and add it to
the queue with the same direc on.
Step 12: If the next cell in the same direc on is not empty, it means the player hit a rock ('#'). In this
case, mark the current cell as visited and add it to the queue with the direc on 4 (stop).
Step 13: Con nue the BFS loop un l all reachable cells are visited.
Step 14: Count the number of reachable cells by itera ng through all the cells and taking the maximum
value among five direc ons (fl[5i], fl[5i+1], fl[5i+2], fl[5i+3], fl[5*i+4]) and adding them to 'res'.
Step 15: Print the final result 'res', which represents the maximum number of reachable cells.
Code:
#include<bits/stdc++.h>
int dx4[4]={1,-1,0,0};
int dy4[4]={0,0,1,-1};
int main(){
int n,m;
vector<string> s(n);
vector<int> fl(5*n*m,0);
queue<int> q;
fl[5*(m+1)+4]=1;
q.push(5*(m+1)+4);
while(!q.empty()){
int od=q.front();q.pop();
int x=(od/5)/m;
int y=(od/5)%m;
int d=od%5;
if(d==4){
// stop
for(int i=0;i<4;i++){
int nx=x+dx4[i];
int ny=y+dy4[i];
int nd=i;
if(s[nx][ny]=='.'){
int nid=5*(nx*m+ny)+nd;
if(fl[nid]==0){
fl[nid]=1;
q.push(nid);
else{
// keep the direc on
int nx=x+dx4[d];
int ny=y+dy4[d];
int nd=d;
if(s[nx][ny]=='.'){
int nid=5*(nx*m+ny)+nd;
if(fl[nid]==0){
fl[nid]=1;
q.push(nid);
else{
// hit a rock
int nid=5*(x*m+y)+4;
if(fl[nid]==0){
fl[nid]=1;
q.push(nid);
int res=0;
for(int i=0;i<n*m;i++){
res+=max({fl[5*i],fl[5*i+1],fl[5*i+2],fl[5*i+3],fl[5*i+4]});
return 0;
}
E - Defect-free Squares Editorial
/
Score : 475 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and j -th column from the left of the grid.
Each square of the grid is holed or not. There are exactly N holed squares: (a1 , b1 ), (a2 , b2 ), … , (aN , bN ).
When the triple of positive integers (i, j, n) satisfies the following condition, the square region whose top-left corner is (i, j) and whose bottom-right
corner is (i + n − 1, j + n − 1) is called a holeless square.
i + n − 1 ≤ H.
j + n − 1 ≤ W.
For every pair of non-negative integers (k, l) such that 0 ≤ k ≤ n − 1, 0 ≤ l ≤ n − 1, square (i + k, j + l) is not holed.
How many holeless squares are in the grid?
Constraints
1 ≤ H, W ≤ 3000
0 ≤ N ≤ min(H × W , 105 )
1 ≤ ai ≤ H
1 ≤ bi ≤ W
Input
The input is given from Standard Input in the following format:
H W N
a1 b1
a2 b2
⋮
aN bN
Output
Print the number of holeless squares.
Copy
2 3 1
2 3
Copy
6
There are six holeless squares, listed below. For the first five, n = 1, and the top-left and bottom-right corners are the same square.
The square region whose top-left and bottom-right corners are (1, 1).
The square region whose top-left and bottom-right corners are (1, 2).
The square region whose top-left and bottom-right corners are (1, 3).
The square region whose top-left and bottom-right corners are (2, 1).
The square region whose top-left and bottom-right corners are (2, 2).
The square region whose top-left corner is (1, 1) and whose bottom-right corner is (2, 2).
Copy
0
Copy
1 1 0
Copy
1
Copy
3000 3000 0
Copy
9004500500
E - Defect-free Squares – Editorial
Algorithm:
def main():
H, W, N = map(int, input().split())
hole = [[0] * (W + 1) for _ in range(H + 1)]
dp = [[0] * (W + 1) for _ in range(H + 1)]
for _ in range(N):
a, b = map(int, input().split())
hole[a][b] = 1
ans = 0
for i in range(1, H + 1):
for j in range(1, W + 1):
if hole[i][j]:
con%nue
dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1
ans += dp[i][j]
print(ans)
if __name__ == "__main__":
main()
F - Yet Another Grid Task Editorial
/
Score : 525 points
Problem Statement
There is an N × M grid and a player standing on it.
Let (i, j) denote the square at the i-th row from the top and j -th column from the left of this grid.
Each square of this grid is black or white, which is represented by N strings S1 , S2 , … , SN of length M as follows:
For every pair of integers (i, j) such that 1 ≤ i ≤ N , 1 ≤ j ≤ M , if square (i, j) is black, the square under (i, j) and the square to the immediate
lower right of (i, j) are also black (if they exist).
Formally, all of the following are satisfied.
If square (i, j) is black and square (i + 1, j) exists, square (i + 1, j) is also black.
If square (i, j) is black and square (i + 1, j + 1) exists, square (i + 1, j + 1) is also black.
Takahashi can paint zero or more white squares black, and he will do so to make the grid beautiful.
Find the number of different beautiful grids he can make, modulo 998244353.
Two grids are considered different when there is a square that has different colors in those two grids.
Constraints
1 ≤ N , M ≤ 2000
Si is a string of length M consisting of . and #.
Input
The input is given from Standard Input in the following format:
N M
S1
S2
⋮
SN
Output
Print the answer as an integer.
Copy
2 2
.#
..
Copy
3
.# .# ##
.# ## ##
Copy
92
Copy
25 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
Copy
604936632
Step-by-step algorithm:
1. Read input:
- Read the integers `n` and `m`, representing the number of rows and columns in the grid.
- Read the grid `s`, which is a vector of strings representing the rows of the grid.
2. Propagate the obstacles ('#'):
- Iterate through the grid `s`.
- If an obstacle is found at cell `(i, j)` in the grid, set the cells `(i+1, j)` and `(i+1, j+1)` to obstacles
as well (if within bounds).
- This step is to simulate the falling of obstacles downward, as indicated by the condition
`if(s[i][j]=='#')`.
3. Initialize the DP array:
- Create a DP array `dp` of size `m+1` and initialize all elements to 0.
- Set the last element of the DP array `dp[m]` to 1, as there's one way to reach the last column (it's
already marked as empty).
4. Dynamic Programming:
- Iterate through all columns in reverse order (from `m-1` to 0).
- Update the DP array values by adding the value of the next column (`j+1`) to the current column
(`j`).
- Ensure the DP array values are modulo `mod` after each addition.
5. Handle obstacles and out-of-bounds:
- Iterate through the columns and rows using the variable `c`, representing the difference between
the row and column indices.
- If `i=j+c` (the corresponding row and column index relationship) is out of bounds (`i<0` or `i>=n`)
or if the cell in the grid is an obstacle (`s[i][j]=='#'`), set the DP value of the next column (`j+1`) to 0.
6. Compute the result:
- Sum all the values in the DP array and store it in the variable `res`.
- Ensure `res` is modulo `mod` before outputting it as the result.
Python Code:
mod = 998244353
def main():
n, m = map(int, input().split())
s = []
for _ in range(n):
s.append(input().strip())
for i in range(n):
for j in range(m):
if s[i][j] == '#':
if i != (n-1):
s[i+1] = s[i+1][:j] + '#' + s[i+1][j+1:]
if i != (n-1) and j != (m-1):
s[i+1] = s[i+1][:j+1] + '#' + s[i+1][j+2:]
dp = [0] * (m + 1)
dp[m] = 1
for c in range(-m + 1, n):
for j in range(m - 1, -1, -1):
dp[j] = (dp[j] + dp[j+1]) % mod
for j in range(m):
i=j+c
if i < 0:
dp[j] = 0
elif i >= n or s[i][j] == '#':
dp[j+1] = 0
res = sum(dp) % mod
print(res)
if __name__ == "__main__":
main()
G - One More Grid Task Editorial
/
Score : 575 points
Problem Statement
There is an N × M grid, where the square at the i-th row from the top and j -th column from the left has a non-negative integer Ai,j written on it.
Find the maximum possible value of f (R) = (the sum of integers written on the squares in R) × (the smallest integer written on a square in R).
Constraints
All input values are integers.
1 ≤ N , M ≤ 300
1 ≤ Ai,j ≤ 300
Input
The input is given from Standard Input in the following format:
N M
A1,1 A1,2 … A1,M
⋮
AN ,1 AN ,2 … AN ,M
Output
Print the answer as an integer.
Copy
3 3
5 4 3
4 3 2
3 2 1
Copy
48
Choosing the rectangular region whose top-left corner is square (1, 1) and whose bottom-right corner is square (2, 2) achieves f (R) = (5 + 4 + 4 +
3) × min(5, 4, 4, 3) = 48, which is the maximum possible value.
Copy
4 5
3 1 4 1 5
9 2 6 5 3
5 8 9 7 9
3 2 3 8 4
Copy
231
Sample Input 3 Copy
Copy
6 6
1 300 300 300 300 300
300 1 300 300 300 300
300 300 1 300 300 300
300 300 300 1 300 300
300 300 300 300 1 300
300 300 300 300 300 1
Copy
810000
G. ONE MORE GRID TASK
Algorithm:
Note: The code uses dynamic programming and prefix sums to efficiently calculate the sum of any
subgrid. It explores various possible heights ('mi') and calculates the maximum subgrid sum for each
height to find the overall maximum subgrid sum. The column heights are manipulated to consider only
columns with heights greater than or equal to 'mi' in each row, reducing the problem to 1D. The 1D
subgrid sum is then efficiently calculated using the 'sum2d' function. The overall time complexity of
the algorithm is O(n * m * 300), which is efficient given the constraints of the problem.
Code:
1. #include<bits/stdc++.h>
2.
3. using namespace std;
4.
5. int sum2d(int fx,int fy,int tx,int ty,vector<vector<int>> &s){
6. if(fx>tx || fy>ty){return 0;}
7. if(fx==0 && fy==0){return s[tx][ty];}
8. else if(fx==0){
9. return s[tx][ty]-s[tx][fy-1];
10. }
11. else if(fy==0){
12. return s[tx][ty]-s[fx-1][ty];
13. }
14. else{
15. return s[tx][ty]-s[tx][fy-1]-s[fx-1][ty]+s[fx-1][fy-1];
16. }
17. }
18.
19. int main(){
20. ios::sync_with_stdio(false);
21. cin.tie(nullptr);
22. int n,m;
23. cin >> n >> m;
24. vector<vector<int>> a(n,vector<int>(m));
25. for(int i=0;i<n;i++){
26. for(int j=0;j<m;j++){
27. cin >> a[i][j];
28. }
29. }
30. vector<vector<int>> s=a;
31. for(int i=1;i<n;i++){
32. for(int j=0;j<m;j++){s[i][j]+=s[i-1][j];}
33. }
34. for(int i=0;i<n;i++){
35. for(int j=1;j<m;j++){s[i][j]+=s[i][j-1];}
36. }
37.
38. long long res=0;
39. for(int mi=1;mi<=300;mi++){
40. vector<pair<int,int>> vp(m);
41. for(int i=0;i<m;i++){
42. vp[i].first=0;
43. vp[i].second=i;
44. }
45.
46. for(int i=0;i<n;i++){
47. vector<pair<int,int>> top,bot;
48. for(auto &nx : vp){
49. if(a[i][nx.second]>=mi){
50. top.push_back({nx.first+1,nx.second});
51. }
52. else{
53. bot.push_back({0,nx.second});
54. }
55. }
56. vp.clear();
57. for(auto &nx : top){vp.push_back(nx);}
58. for(auto &nx : bot){vp.push_back(nx);}
59.
60. vector<int> lef(m),rig(m);
61. for(int j=0;j<m;j++){
62. lef[j]=j-1;
63. rig[j]=j+1;
64. }
65.
66. for(auto &nx : vp){
67. int cl=lef[nx.second];
68. int cr=rig[nx.second];
69. if(cl>=0){rig[cl]=cr;}
70. if(cr<m){lef[cr]=cl;}
71. long long cf=sum2d(i-nx.first+1,cl+1,i,cr-1,s);
72. cf*=mi;
73. res=max(res,cf);
74. }
75. }
76. }
77. cout << res << "\n";
78. return 0;
79. }
Ex - Many Illumination Plans Editorial
/
Score : 675 points
Problem Statement
There is a rooted tree T with N vertices numbered 1 to N . The root is vertex 1, and the parent of vertex i (2 ≤ i ≤ N ) is Pi .
Each vertex has two non-negative integer values called beauty and weight. The beauty and weight of vertex i are Bi and Wi , respectively.
Additionally, each vertex is painted red or blue. The color of vertex i is represented by an integer Ci : vertex i is painted red if Ci
= 0, and blue if Ci = 1.
Let U be the rooted tree that is the subtree of T rooted at v .
You can perform the following sequence of operations on U zero or more times. (The operations do not affect the beauties, weights, or colors of
the vertices that are not being deleted.)
A rooted tree that can be obtained as U after some operations is called a good rooted tree when all of the following conditions are satisfied.
For every edge in U , the edge's endpoints have different colors.
The vertices have a total weight of at most X .
Find the maximum possible total beauty of the vertices in a good rooted tree.
Constraints
2 ≤ N ≤ 200
0 ≤ X ≤ 50000
1 ≤ Pi ≤ i − 1
0 ≤ Bi ≤ 1015
0 ≤ Wi ≤ X
Ci is 0 or 1.
Input
The input is given from Standard Input in the following format:
N X
P2
P3 … PN
B1 W 1 C1
B2 W 2 C2
⋮
BN W N C N
Output
Print N lines. The i-th line should contain F (i).
Copy
4 10
1 2 2
2 1 0
4 2 1
6 8 0
7 4 1
Sample Output 1 Copy
Copy
9
10
6
7
For v = 1, choosing c = 2 and then c = 3 changes U as shown in the figure, making it a good rooted tree. The total beauty of the vertices in this tree
is 2 + 7 = 9, which is the maximum among all good rooted trees, so F (1) = 9.
For v = 2, choosing c = 4 changes U as shown in the figure, making it a good rooted tree. The total beauty of the vertices in this tree is 4 + 6 = 10,
which is the maximum among all good rooted trees, so F (2) = 10.
Copy
5 5
1 2 2 3
1 1 0
10 1 1
100 1 0
1000 1 1
10000 1 1
Copy
11001
10110
10100
1000
10000
Copy
20 100
1 2 1 1 1 6 6 5 1 7 9 4 6 4 15 16 8 2 5
887945036308847 12 0
699398807312293 20 1
501806283312516 17 0
559755618233839 19 1
253673279319163 10 1
745815685342299 11 1
251710263962529 15 0
777195295276573 15 0
408579800634972 17 0
521840965162492 17 1
730678137312837 18 1
370007714721362 14 1
474595536466754 17 0
879365432938644 15 0
291785577961862 20 0
835878893889428 14 1
503562238579284 10 0
567569163005307 18 1
368949585722534 15 0
386435396601075 16 0
Copy
5329161389647368
1570154676347343
501806283312516
2665577865131167
1418696191276572
3952333977838189
982388401275366
1344764458281880
778587515356334
521840965162492
730678137312837
370007714721362
474595536466754
879365432938644
1631226710430574
1339441132468712
503562238579284
567569163005307
368949585722534
386435396601075
Ex – Many Illumina on Plans:
Algorithm:
Step 1: The func on takes a node 'c' as input, which represents a node in a tree data structure.
Step 2: Ini alize two DP tables, `Dp[0]` and `Dp[1]`, to store intermediate results. These DP tables are
represented as arrays.
Step 3: Base case: If 'c' is a leaf node (i.e., it has no children), ini alize both `Dp[0]` and `Dp[1]` to be
the DP table corresponding to the empty set (essen ally, all values in the table are ini alized to a
neutral value, like -infinity).
Step 4: Recursive case: For each child node 'd' of the current node 'c', recursively call the `dfs(d)`
func on to obtain the DP tables `ep[0]` and `ep[1]` for the child node 'd'. These tables will store the
op mal results for the subtree rooted at node 'd'.
Step 5: Merge results from child nodes: Update the DP table `Dp[0]` by performing the max-plus
convolu on with `ep[0]` for all child nodes 'd'. This means taking the element-wise maximum of the
two tables. Similarly, update `Dp[1]` using the max-plus convolu on with `ep[1]`.
Step 6: Perform DP transi on for the current node 'c': For each possible value of 'i' from 0 to (X - W[c]),
where 'X' is a predefined constant and 'W[c]' is the weight of the current node 'c', update the DP table
`Dp[C[c]][i + W[c]]` by taking the maximum value between its current value and the value of `Dp[1 -
C[c]][i] + B[c]`. Here, 'C[c]' represents the type of the current node 'c' (either 0 or 1), and 'B[c]' is a
predefined constant represen ng some value associated with the node 'c'.
Step 7: Return the DP table `Dp`, which now contains the op mal results for the subtree rooted at the
current node 'c'.
Step 8: The algorithm will con nue to execute for all other nodes in the tree, following the recursive
approach, and the final DP tables `Dp[0]` and `Dp[1]` will contain the op mal results for the en re
tree.
Code:
1. function dfs(c):
2. Dp[0] <- (DP table corresponding to empty set)
3. Dp[1] <- (DP table corresponding to empty set)
4. for d in {children of c} do
5. ep = dfs(d)
6. Dp[0] <- max_plus_convolution(Dp[0], ep[0])
7. Dp[1] <- max_plus_convolution(Dp[1], ep[1])
8. end for
9. for i = 0 to X - W[c] do
10. chmax(Dp[C[c]][i + W[c]], Dp[1 - C[c]][i] + B[c])
11. end for
12. return Dp
A DETAILED EDITORIAL FOR
ATCODER - UNIQUE VISION PROGRAMMING
CONTEST 2023 SUMMER(ATCODER BEGINNER
CONTEST 312)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by the users
“en_translator (English) & physics (Japanese)”
CONTEST PROBLEM
STATEMENTS, HINTS AND
ALGORITHMS WITH CODE
A - Chord Editorial
/
Score : 100 points
Problem Statement
Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one
of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Constraints
S is a length-3 string consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
Output
Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Copy
ABC
Copy
No
When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.
Copy
FAC
Sample Output 2 Copy
Copy
Yes
Copy
XYX
Copy
No
A – Chord
Algorithm:
In summary, the algorithm determines whether the input string s exists in the
predefined string t. If it does, the program outputs "Yes"; otherwise, it outputs "No"
to the console.
Code:
#include <iostream>
#include <string>
int main()
string s;
cin >> s;
(OR)
#include <iostream>
#include <string>
int main()
string s;
cin >> s;
string t = "ACEGBDFAC";
}
B - TaK Code Editorial
/
Score : 200 points
Problem Statement
Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following
conditions:
You are given a grid with N horizontal rows and M vertical columns. The state of the grid is described
by N strings, S1 , …, and SN , each of length M . The cell at the i-th row from the top and j -th column
from the left is black if the j -th character of Si is #, and white if it is ..
Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK
Code.
Constraints
9 ≤ N , M ≤ 100
N and M are integers.
Si is a string of length M consisting of . and #.
Input
The input is given from Standard Input in the following format:
N M
S1
⋮
SN
Output
For all pairs (i, j) such that the nine-by-nine region, whose top-left cell is at the i-th row from the top and
j -th columns from the left, satisfies the conditions of a TaK Code, print a line containing i, a space, and j
in this order.
The pairs must be sorted in lexicographical ascending order; that is, i must be in ascending order, and
within the same i, j must be in ascending order.
Copy
19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........
Copy
1 1
1 10
7 7
10 2
A TaK Code looks like the following, where # is a black cell, . is a white cell, and ? can be either black or
white.
###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###
In the grid given by the input, the nine-by-nine region, whose top-left cell is at the 10-th row from the top
and 2-nd column from the left, satisfies the conditions of a TaK Code, as shown below.
###......
###......
###......
.........
..##.....
..##.....
......###
......###
......###
Copy
9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....
Copy
1 1
Copy
18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######
Sample Output 3 Copy
Copy
Algorithm:
The code essentially searches for a specific pattern in the given grid and outputs the
starting positions of regions that match that pattern. The pattern is composed of '#'
characters forming two mirrored 3x3 regions and a central 4x4 region with '.'
characters in certain positions.
Code:
1. #include<stdio.h>
2.
3. char s[110][110];
4. int check(int i,int j){
5. for(int ii=0;ii<3;ii++)for(int
jj=0;jj<3;jj++)if(s[i+ii][j+jj]!='#')return 0;
6. for(int ii=0;ii<3;ii++)for(int jj=0;jj<3;jj++)if(s[i+8-ii][j+8-
jj]!='#')return 0;
7. for(int ii=0;ii<4;ii++)if(s[i+ii][j+3]!='.')return 0;
8. for(int jj=0;jj<4;jj++)if(s[i+3][j+jj]!='.')return 0;
9. for(int ii=0;ii<4;ii++)if(s[i+8-ii][j+8-3]!='.')return 0;
10. for(int jj=0;jj<4;jj++)if(s[i+8-3][j+8-jj]!='.')return 0;
11. return 1;
12. }
13. int main(){
14. int n,m;
15. scanf("%d%d",&n,&m);
16. for(int i=0;i<n;i++)scanf("%s",s[i]);
17. for(int i=0;i<n-8;i++)for(int j=0;j<m-8;j++){
18. if(check(i,j))printf("%d %d\n",i+1,j+1);
19. }
20. }
C - Invisible Hand Editorial
/
Score : 300 points
Problem Statement
There are N sellers and M buyers in an apple market.
The i-th seller may sell an apple for Ai yen or more (yen is the currency in Japan).
Condition: The number of people who may sell an apple for X yen is greater than or equal to the number
of people who may buy an apple for X yen.
Constraints
1 ≤ N , M ≤ 2 × 105
1 ≤ Ai , Bi ≤ 109
Input
The input is given from Standard Input in the following format:
N M
A1 … AN
B1 … BM
Output
Print the answer.
Copy
3 4
110 90 120
100 80 120 10000
Sample Output 1 Copy
Copy
110
Two sellers, the 1-st and 2-nd, may sell an apple for 110 yen; two buyers, the 3-rd and 4-th, may buy an
apple for 110 yen. Thus, 110 satisfies the condition.
Since an integer less than 110 does not satisfy the condition, this is the answer.
Copy
5 2
100000 100000 100000 100000 100000
100 200
Copy
201
Copy
3 2
100 100 100
80 120
Copy
100
C - Invisible Hand
Algorithm:
In summary, the code takes two lists A and B as input, increments each element in B
by 1, merges both lists, sorts the merged list, and then prints the M-th element (0-
indexed) from the sorted list.
Note: The sorting and indexing are zero-indexed in Python, so the M-th element will
be printed as the (M - 1)-th element in the list.
Code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Score : 400 points
Problem Statement
You are given a non-empty string S consisting of (, ), and ?.
There are 2x ways to obtain a new string by replacing each ? in S with ( and ), where x is the number of
occurrences of ? in S . Among them, find the number, modulo 998244353, of ways that yield
a parenthesis string.
It is an empty string.
It is a concatenation of (, A, and ), for some parenthesis string A.
It is a concatenation of A and B , for some non-empty parenthesis strings A and B .
Constraints
S is a non-empty string of length at most 3000 consisting of (, ), and ?.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Copy
(???(?
Copy
2
Copy
)))))
Copy
0
Copy
??????????????(????????(??????)?????????(?(??)
Copy
603032273
Algorithm:
This C++ code utilizes the atcoder library to compute the number of ways to form
balanced parentheses expressions using a given string s. The algorithm uses dynamic
programming to achieve this efficiently.
In summary, the code efficiently calculates the number of ways to form balanced
parentheses expressions using the given string s using a dynamic programming
approach and modular arithmetic. The answer is printed modulo 998244353.
Code:
#include <iostream>
#include <string>
#include <vector>
#include <atcoder/modint>
int main() {
string s;
cin >> s;
int n = s.size();
dp[0][0] = 1;
}
E - Tangency of Cuboids Editorial
/
Score : 500 points
Problem Statement
There are N rectangular cuboids in a three-dimensional space.
These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has
a volume of 0.
The diagonal of the i-th cuboid is a segment that connects two points (Xi,1 , Yi,1 , Zi,1 ) and
(Xi,2 , Yi,2 , Zi,2 ), and its edges are all parallel to one of the coordinate axes.
For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each i, find the number of j with 1 ≤ j ≤ N and j =
i such that the intersection of the
surfaces of the i-th and j -th cuboids has a positive area.
Constraints
1 ≤ N ≤ 105
0 ≤ Xi,1 < Xi,2 ≤ 100
Input
The input is given from Standard Input in the following format:
N
X1,1 Y1,1 Z1,1 X1,2 Y1,2 Z1,2
⋮
XN ,1 YN ,1 ZN ,1 XN ,2 YN ,2 ZN ,2
Output
Print the answer.
Sample Input 1 Copy
Copy
4
0 0 0 1 1 1
0 0 1 1 1 2
1 1 1 2 2 2
3 3 3 4 4 4
Copy
1
1
0
0
The 1-st and 2-nd cuboids share a rectangle whose diagonal is the segment connecting two points
(0, 0, 1) and (1, 1, 1).
The 1-st and 3-rd cuboids share a point (1, 1, 1), but do not share a surface.
Copy
3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10
Copy
2
1
1
Copy
8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2
Algorithm:
This C++ code reads information about cuboids and calculates the number of distinct
neighbors each cuboid has in a 3D grid. The add function is used to keep track of
neighboring relationships between the cuboids.
In summary, the code reads information about cuboids and their coordinates in a 3D
grid. It then determines the neighboring relationships between these cuboids and
outputs the number of distinct neighbors each cuboid has. The neighboring
relationship is established based on the adjacency of cuboid faces (sharing a common
boundary).
Code:
#include<bits/stdc++.h>
using namespace std;
int a[110][110][110];
int main(){
int n;
cin >> n;
rep(i,0,n){
int x1,y1,z1,x2,y2,z2;
rep(x,x1,x2)rep(y,y1,y2)rep(z,z1,z2)a[x][y][z]=i+1;
vector<set<int>>ans(n+1);
ans[i].insert(j);
ans[j].insert(i);
};
rep(x,0,100)rep(y,0,100)rep(z,0,100)if(a[x][y][z]){
if(a[x+1][y][z]&&a[x+1][y][z]!=a[x][y][z])add(a[x][y][z],a[x+1][y][z]);
if(a[x][y+1][z]&&a[x][y+1][z]!=a[x][y][z])add(a[x][y][z],a[x][y+1][z]);
if(a[x][y][z+1]&&a[x][y][z+1]!=a[x][y][z])add(a[x][y][z],a[x][y][z+1]);
}
F - Cans and Openers Editorial
/
Score : 500 points
Problem Statement
There are N items.
Each of these is one of a pull-tab can, a regular can, or a can opener.
The i-th item is described by an integer pair (Ti , Xi ) as follows:
If Ti = 0, the i-th item is a pull-tab can; if you obtain it, you get a happiness of Xi .
If Ti = 1, the i-th item is a regular can; if you obtain it and use a can opener against it, you get a
happiness of Xi .
If Ti = 2, the i-th item is a can opener; it can be used against at most Xi cans.
Find the maximum total happiness that you get by obtaining M items out of N .
Constraints
1 ≤ M ≤ N ≤ 2 × 105
Ti is 0, 1, or 2.
1 ≤ Xi ≤ 109
Input
The input is given from Standard Input in the following format:
N M
T 1 X1
T 2 X2
⋮
T N XN
Output
Print the answer as an integer.
Copy
8 4
0 6
0 6
1 3
1 5
1 15
2 1
2 10
2 100
Copy
27
If you obtain the 1-st, 2-nd, 5-th, and 7-th items, and use the 7-th item (a can opener) against the 5-th
item, you will get a happiness of 6 + 6 + 15 = 27.
There are no ways to obtain items to get a happiness of 28 or greater, but you can still get a happiness
of 27 by obtaining the 6-th or 8-th items instead of the 7-th in the combination above.
Copy
5 5
1 5
1 5
1 5
1 5
1 5
Copy
0
Copy
12 6
2 2
0 1
0 9
1 3
1 5
1 3
0 4
2 1
1 8
2 1
0 1
0 4
Copy
30
E - Tangency of Cuboids
Algorithm:
This code seems to be a C++ program that reads the information about cuboids
defined by their opposite corners' coordinates and calculates the number of distinct
neighbors for each cuboid. The code uses a 3D grid to represent the cuboids and
utilizes a vector of sets to store neighboring relationships between them.
Code:
#include<bits/stdc++.h>
int a[110][110][110];
int main(){
int n;
cin >> n;
rep(i,0,n){
int x1,y1,z1,x2,y2,z2;
rep(x,x1,x2)rep(y,y1,y2)rep(z,z1,z2)a[x][y][z]=i+1;
vector<set<int>>ans(n+1);
ans[i].insert(j);
ans[j].insert(i);
};
rep(x,0,100)rep(y,0,100)rep(z,0,100)if(a[x][y][z]){
if(a[x+1][y][z]&&a[x+1][y][z]!=a[x][y][z])add(a[x][y][z],a[x+1][y][z]);
if(a[x][y+1][z]&&a[x][y+1][z]!=a[x][y][z])add(a[x][y][z],a[x][y+1][z]);
if(a[x][y][z+1]&&a[x][y][z+1]!=a[x][y][z])add(a[x][y][z],a[x][y][z+1]);
}
rep(i,1,n+1)cout << ans[i].size() << endl;
}
G - Avoid Straight Line Editorial
/
Score : 550 points
Problem Statement
You are given a tree with N vertices. The vertices are numbered from 1 through N , and the i-th edge
connects vertex Ai and vertex Bi .
Constraints
1 ≤ N ≤ 2 × 105
1 ≤ Ai , Bi ≤ N
Input
The input is given from Standard Input in the following format:
N
A1 B1
⋮
AN −1 BN −1
Output
Print the answer.
Copy
5
1 2
2 3
2 4
1 5
Sample Output 1 Copy
Copy
2
Copy
6
1 2
2 3
3 4
4 5
5 6
Copy
0
Copy
12
1 6
3 4
10 4
5 9
3 1
2 3
7 2
2 12
1 5
6 8
4 11
Copy
91
G - Avoid Straight Line
Algorithm:
This C++ code is an implementation that calculates the number of triplets of vertices
(u, v, w) in an undirected tree, where u, v, and w are connected by edges. It efficiently
computes the answer by using depth-first search (DFS) and dynamic programming on
trees.
In summary, the code efficiently calculates the number of triplets of vertices (u, v, w)
in an undirected tree using DFS and dynamic programming on trees. It avoids
unnecessary computations and leverages the tree structure to obtain the result in an
optimal way.
Code:
1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. using ll=long long;
5.
6. int main(){
7. ios::sync_with_stdio(false);
8. cin.tie(nullptr);
9.
10. ll n;cin>>n;
11. vector<vector<int>> T(n);
12. for(int _=0;_<n-1;_++){
13. int u,v;cin>>u>>v;u--;v--;
14. T[u].push_back(v);
15. T[v].push_back(u);
16. }
17.
18. ll ans=n*(n-1)*(n-2)/6;
19.
20. vector<ll> sz(n,1);
21. auto dfs=[&](auto dfs,int v,int pre)->void{
22. ll sum1=0,sum2=0;
23. for(int c:T[v]){
24. if(c==pre)continue;
25. dfs(dfs,c,v);
26. sum1 += sz[c];
27. sum2 += sz[c]*sz[c];
28. sz[v]+=sz[c];
29. }
30. sum1 += n-sz[v];
31. sum2 += (n-sz[v])*(n-sz[v]);
32. ans -= (sum1*sum1 - sum2) >> 1;
33. };
34. dfs(dfs,0,-1);
35.
36. cout<<ans<<'\n';
37. }
Ex - snukesnuke Editorial
/
Score : 600 points
Problem Statement
Takahashi is going to decide nicknames of N people, person 1, … , N .
Person i wants a nickname Si . To avoid giving the same nickname to multiple people, he is going to
Repeatedly increment ki by one while the ki -time repetition of Si is someone's nickname.
Constraints
N ≥1
Si is a string of length at least 1 consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N
S1
⋮
SN
Output
Print k1 , …, and kN resulting from deciding the nicknames of the N people by the procedure in the
problem statement.
Copy
3
snuke
snuke
rng
Copy
1 2 1
set to snuke.
Next, he decides person 2's nickname.
Let k2 = 1.
The k2 -time repetition of S2 is snuke, which is already a nickname of person 1, so increment
The k3 -time repetition of S3 is rng, which is nobody's nickname, so person 3's nickname is
set to rng.
Copy
4
aa
a
a
aaa
Copy
1 1 3 2
Copy
5
x
x
x
x
x
Copy
1 2 3 4 5
Ex – snukesnuke
Algorithm:
This C++ code calculates the values of an array `ans` based on the proper es of the input strings `S`.
The code u lizes the `atcoder::z_algorithm` from the `atcoder/string` library, which is used to find the
longest common prefix of a string with each of its suffixes.
1. Include the necessary C++ standard library (`<bits/stdc++.h>`) and the `atcoder/string` library.
2. Define the macro `rep(i,l,r)` as a shorthand for a for-loop with an increment of `1`. It iterates from
`l` (inclusive) to `r` (exclusive).
4. Read the integer `n` from the user, which represents the number of strings in the vector `S`.
5. Create a vector of strings `S` with size `n` to store the input strings.
6. Read the `n` strings and store them in the vector `S`.
7. Create an unordered map `memo` to store informa on about substrings of the strings in `S`. The key
is a substring, and the value is a vector containing the indices of strings in `S` that start with that
substring.
8. Loop through each string `S[i]` in `S`, and for each string, do the following:
b. Find the smallest `j` such that the length of the string `S[i]` is divisible by `j`, and `Z[j]` (the Z-
func on value at index `j`) is equal to the length of the string `S[i]` minus `j`. This means that `S[i]`
repeats itself `j` mes in the string.
c. Update the `memo` map with the substring of `S[i]` from index 0 to `j - 1` as the key and add the
index `i` to the corresponding vector value.
9. Create a vector `ans` of size `n` to store the final answers for each string in `S`.
10. Loop through each key-value pair in the `memo` map, and for each key-value pair, do the following:
a. Create an unordered map `pre` to store the indices of the substrings in the `ans` array that have
already been used.
b. Create an unordered set `used` to store the values that have already been used as answers.
d. For each index `i` in the vector `v` (values), do the following:
- Calculate the value `d` as the length of the string `S[i]` divided by the length of the key `k`.
- Check if the index `crr` (the poten al answer) exists in the `used` set. If it does, increment `crr`
by `d` un l an unused index is found.
- Set the answer for the string `S[i]` in the `ans` vector to be `crr` divided by `d`.
In summary, the code processes a vector of strings `S` and calculates the corresponding answers based
on certain proper es of the strings. It uses the `atcoder::z_algorithm` func on to determine repea ng
substrings and maps the answers to each string in the `ans` array. The final values of `ans` are then
output to the console.
Code:
#include<bits/stdc++.h>
#include<atcoder/string>
int main(){
int n;
cin >> n;
vector<string>S(n);
rep(i,0,n)cin >> S[i];
unordered_map<string,vector<int>>memo;
rep(i,0,n){
auto ret=atcoder::z_algorithm(S[i]);
int len=S[i].size();
int ans=len;
rep(j,1,len)if(len%j==0&&ret[j]==len-j){
ans=j;
break;
memo[S[i].substr(0,ans)].push_back(i);
vector<int>ans(n);
for(auto[k,v]:memo){
unordered_map<int,int>pre;
unordered_set<int>used;
used.insert(0);
for(auto i:v){
int d=S[i].size()/k.size();
int crr=pre[d];
while(used.find(crr)!=used.end())crr+=d;
pre[d]=crr;
used.insert(crr);
ans[i]=crr/d;
rep(i,0,n){
if(i)cout << ' ';
}
THANK YOU
A DETAILED EDITORIAL FOR
ATCODER – ATCODER PROGRAMMING CONTEST 2023
SUMMER (ATCODER BEGINNER CONTEST 313)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by the users
“en_translator (English) & physics (Japanese)”
CONTEST PROBLEM
STATEMENTS, HINTS AND
ALGORITHMS WITH CODE
A - To Be Saikyo Editorial
/
Score : 100 points
Problem Statement
There are N people numbered 1 through N . Each person has a integer score called programming
ability; person i's programming ability is Pi points. How many more points does person 1 need, so that
person 1 becomes the strongest? In other words, what is the minimum non-negative integer x such that
P1 + x > Pi for all i =
1?
Constraints
1 ≤ N ≤ 100
1 ≤ Pi ≤ 100
Input
The input is given from Standard Input in the following format:
N
P1 P2 … PN
Output
Print the answer as an integer.
Copy
4
5 15 2 10
Copy
11
Person 1 becomes the strongest when their programming skill is 16 points or more, so the answer is
16 − 5 = 11.
Sample Input 2 Copy
Copy
4
15 5 2 10
Copy
0
Copy
3
100 100 100
Copy
1
Algorithm:
Note: The algorithm assumes that the strength values are non-negative integers.
This algorithm effectively calculates the minimum strength difference needed for the
first person to become the strongest among the group, while ensuring that negative
differences are treated as 0.
Code:
# Find the maximum strength among the other people (excluding the first person)
m = max(m, p[i])
# Calculate the minimum addi onal strength needed for the first person to become the strongest
Score : 300 points
Problem Statement
There are N competitive programmers numbered person 1, person 2, …, and person N .
There is a relation called superiority between the programmers. For all pairs of distinct programmers (
person X , person Y ), exactly one of the following two relations holds: "person X is stronger than
person Y " or "person Y is stronger than person X ."
The superiority is transitive. In other words, for all triplets of distinct programmers (person X , person Y ,
person Z), it holds that:
if person X is stronger than person Y and person Y is stronger than person Z , then person X is
stronger than person Z .
A person X is said to be the strongest programmer if person X is stronger than person Y for all
people Y other than person X . (Under the constraints above, we can prove that there is always exactly
one such person.)
You have M pieces of information on their superiority. The i-th of them is that "person Ai is stronger
Can you determine the strongest programmer among the N based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest
programmers, print -1.
Constraints
2 ≤ N ≤ 50
0 ≤ M ≤ N (N2−1)
1 ≤ Ai , Bi ≤ N
Ai
= Bi
If i =
j , then (Ai , Bi ) =
(Aj , Bj ).
There is at least one way to determine superiorities for all pairs of distinct programmers, that is
consistent with the given information.
Input
The input is given from Standard Input in the following format:
N M
A1 B1
A2 B2
⋮
AM BM
Output
If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1.
Copy
3 2
1 2
2 3
Copy
1
You have two pieces of information: "person 1 is stronger than person 2" and "person 2 is stronger than
person 3."
By the transitivity, you can also infer that "person 1 is stronger than person 3," so person 1 is the
strongest programmer.
Copy
3 2
1 3
2 3
Copy
-1
Both person 1 and person 2 may be the strongest programmer. Since you cannot uniquely determine
which is the strongest, you should print -1.
Copy
-1
Algorithm:
Complexity Analysis: The complexity of this algorithm is O(N + M), where N is the
number of programmers and M is the number of pairwise comparisons. This is
because you loop through all programmers once to calculate deg values, and then
loop through them again to find the strongest programmer. The array operations
and comparisons within the loops have constant time complexity.
Please note that this algorithm assumes the input data is correctly formatted and
follows the conditions described in the explanation you provided. If the input doesn't
adhere to these conditions, the algorithm might not produce correct results.
Code:
#include <bits/stdc++.h>
int N, M;
int a, b;
if (deg[i] == 0) {
if (ans != -1) {
return 0;
} else {
return 0;
}
C - Approximate Equalization 2 Editorial
/
Score : 400 points
Problem Statement
You are given an integer sequence A = (A1 , A2 , … , AN ). You can perform the following operation any
Choose integers i and j with 1 ≤ i, j ≤ N . Decrease Ai by one and increase Aj by one.
Find the minimum number of operations required to make the difference between the minimum and
maximum values of A at most one.
Constraints
1 ≤ N ≤ 2 × 105
1 ≤ Ai ≤ 109
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the answer as an integer.
Copy
4
4 7 3 7
Copy
3
By the following three operations, the difference between the minimum and maximum values of A
becomes at most one.
You cannot make the difference between maximum and minimum values of A at most one by less than
three operations, so the answer is 3.
Copy
1
313
Copy
0
Copy
10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
Copy
2499999974
Algorithm:
Code:
n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
a.sort()
p = sum_a // n
r = sum_a % n
b = [p] * (n - r) + [p + 1] * r
print(ans)
D - Odd or Even Editorial
/
Score : 550 points
Problem Statement
This is an interactive task (where your program and the judge interact via Standard Input and Output).
While you cannot directly access the elements of sequence A, you are allowed to ask the judge the
following query at most N times.
Choose distinct integers x1 , x2 , …, and xK between 1 and N , inclusive, to ask the parity of Ax1
+
A x2 + ⋯ + A xK .
Here, the judge is adaptive. In other words, the judge may modify the contents of A as long as it is
consistent with the responses to the past queries.
Therefore, your program is considered correct if the output satisfies the following condition, and incorrect
otherwise:
your program prints a sequence consistent with the responses to the queries so far, and that is the
only such sequence.
Constraints
1 ≤ K < N ≤ 1000
K is odd.
Ai is 0 or 1.
N K
Each query should be printed to Standard Output in the following format, where x1 , x2 , …, and xK are K
? x1 x2 … xK
The response to the query is given from Standard Input in the following format.
However, if x1 , x2 , … and xK do not satisfy the constraints, or the number of queries exceeds N , then T
is -1.
If the judge returns -1, your program is already considered incorrect, so terminate the program
immediately.
When you can determine all the elements of A, print those elements in the following format, and
terminate the program immediately.
! A1 A2 … AN
Notes
Print a newline and flush Standard Output at the end of each message. Otherwise, you may
get a TLE verdict.
The verdict will be indeterminate if there is malformed output during the interaction or your
program quits prematurely.
Terminate the program immediately after printing the answer, or the verdict will be indeterminate.
The judge for this problem is adaptive. This means that the judge may modify the contents of A as
long as it is consistent with the responses to the past queries.
Sample Interaction
In the following interaction, N = 5 and K = 3. Note that the following output itself will result
in WA .
Here, A = (1, 0, 1, 1, 0) is indeed consistent with the responses, but so is (0, 0, 1, 0, 0), so sequence A
is not uniquely determined. Thus, this program is considered incorrect.
1. Initialization:
Read input values N (size of the array) and K.
Define a function send that sends queries and receives responses. The
function increments the input vector elements by 1 before sending the
query and returns the response.
Initialize an array ans to store the final results.
2. Solving for N=K+1:
Perform K+1 queries to determine the initial K+1 values of the array.
Calculate the XOR of these initial values, which is used to determine the
parity of the remaining value.
3. Solving for N > K+1:
Iterate over the array indices from K+2 to N, and for each index i, send
a query for the subarray (1, 2, ..., K-1, i).
Use the responses and the previously determined values to calculate
the value of A[i] based on XOR operations.
4. Output the Result:
Print the resulting array ans as the final solution to the problem.
Code:
#include <bits/stdc++.h>
void out(vector<int> v) {
}
}
int main() {
int N, K;
cout.flush();
int x;
cin >> x;
return x;
};
vector<int> ans(N);
int r = 0;
vector<int> v;
if (i != j) v.push_back(j);
ans[i] = send(v);
r ^= ans[i];
vector<int> v(K);
int s = 0;
v.back() = i;
int t = send(v);
ans[i] = s ^ t;
cout.flush();
}
E - Duplicate Editorial
/
Score : 600 points
Problem Statement
For a string S consisting of digits from 1 through 9, let f (S) be the string T obtained by the following
procedure. (Si denotes the i-th character of S .)
integer.
Constraints
2 ≤ N ≤ 106
S is a length-N string consisting of 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Input
The input is given from Standard Input in the following format:
N
S
Output
Print the number of times, modulo 998244353, that you perform the operation until you complete it. If
you will repeat the operation indefinitely, print -1 instead.
Sample Input 1 Copy
Copy
3
313
Copy
4
Copy
9
123456789
Copy
-1
If S = 123456789, you indefinitely repeat the operation. In this case, -1 should be printed.
Copy
2
11
Copy
1
Algorithm:
Code:
#include <bits/stdc++.h>
#include "atcoder/modint.hpp"
char c = S[0];
int n = 1;
if (S[i] == c)
n++;
else {
ret.emplace_back(c, n);
c = S[i], n = 1;
ret.emplace_back(c, n);
return ret;
int main() {
int N;
string S;
exit(0);
mint t = 0;
while (!rle.empty()) {
char c;
mint n;
>e(c, n) = rle.back();
rle.pop_back();
if (c == '1') {
n += t * (last - '1');
t += n;
} else {
t += 1;
last = c;
Sdf
Sdfsd
Sedfs
Dfsd
Sdfsd
}
F - Flip Machines Editorial
/
Score : 625 points
Problem Statement
There are N cards numbered 1 through N . Each face of a card has an integer written on it; card i has
Ai on its front and Bi on its back. Initially, all cards are face up.
There are M machines numbered 1 through M . Machine j has two (not necessarily distinct) integers Xj
and Yj between 1 and N . If you power up machine j , it flips card Xj with the probability of 12 , and flips
card Yj with the remaining probability of 12 . This probability is independent for each power-up.
Among Snuke's possible choices of S , find the maximum expected value of the sum of the integers
written on the face-up sides of the cards after the procedure.
Constraints
1 ≤ N ≤ 40
1 ≤ M ≤ 105
1 ≤ Ai , Bi ≤ 104
1 ≤ Xj , Y j ≤ N
Input
The input is given from Standard Input in the following format:
N M
A1 B1
⋮
AN BN
X1 Y 1
⋮
XM Y M
Output
Print the answer. Your output is considered correct if the absolute or relative difference from the true
value is at most 10−6 .
Copy
3 1
3 10
10 6
5 2
1 2
Copy
19.500000
If S is chosen to be an empty set, no machine is powered up, so the expected sum of the integers
written on the face-up sides of the cards after the procedure is 3 + 10 + 5 = 18.
If S is chosen to be {1}, machine 1 is powered up.
If card X1 = 1 is flipped, the expected sum of the integers written on the face-up sides of the cards
Copy
1 3
5 100
1 1
1 1
1 1
Copy
100.000000
Copy
8 10
6918 9211
16 1868
3857 8537
3340 8506
6263 7940
1449 4593
5902 1932
310 6991
4 4
8 6
3 5
1 1
4 2
5 6
7 5
3 3
1 5
3 1
Copy
45945.000000
Algorithm:
This problem involves a sequence of machines, each with two sides, and the goal is
to determine the expected sum of integers on the face-up sides of the cards after
applying specific operations. The editorial provides insights into the problem,
explains the key observations, and presents two algorithms to solve it. Here's an
overview of the main ideas:
1. Expected Value Calculation: The first part of the editorial discusses how to
calculate the expected value of integers written on the face-up sides of the
cards after the operations are completed. It considers the case where
machines have equal values for their two sides first and provides a simple
formula for the probability that a card ends up being face down.
2. Problem Transformation: The original problem is then transformed into a
more manageable form. The goal is to maximize a value W by choosing a
subset of machines, where the value W is based on the given constraints and
the difference between the values on the face-up and face-down sides of the
cards.
3. Algorithms to Solve the Problem: The editorial presents two algorithms to
solve the problem:
Algorithm 1 (Brute Force): This algorithm tries all possible sets of
indices I∩P and calculates the optimal choice of machines for each set.
It utilizes the observation that machines with Xj∈P and Yj∈P are always
unused.
Algorithm 2 (Bit DP): This algorithm uses dynamic programming (DP)
with a bit mask approach to efficiently explore the possibilities. It
maintains a DP table dp[i][S], where i represents the number of
machines chosen from P, and S is a bitmask indicating which machines
from Q have been chosen.
The choice of which algorithm to use depends on the relative sizes of the sets
P and Q.
4. Sample C++ Implementation: The provided C++ code is a full
implementation of the problem solution using the concepts explained in the
editorial. It reads input, processes the machines and their values, and
calculates the expected value based on the chosen algorithm.
Overall, the code efficiently solves the problem by leveraging expected value
calculations and choosing the appropriate algorithm based on the characteristics of
the input. The complexity of the solution is within the constraints of the problem,
making it suitable for larger inputs.
Code:
#include<bits/stdc++.h>
int main() {
int n, m;
--x[i], --y[i];
int ans = 0;
vector<int> p, q;
vector<int> id(n);
ans += a[i] * 2;
id[i] = p.size();
p.push_back(d);
} else {
id[i] = q.size();
q.push_back(d);
vector<ll> ls(ps);
if (xp == yp) {
if (!xp) {
q[id[x[i]]] = q[id[y[i]]] = 0;
} else {
int mx = 0;
int now = 0;
ll cq = 0;
now -= p[i];
cq |= ls[i];
}
}
mx = max(mx, now);
ans += mx;
} else {
dp[0][0] = 0;
int mx = 0;
mx = max(mx, now);
ans += mx;
}
G - Redistribution of Piles Editorial
/
Score : 625 points
Problem Statement
There are N plates numbered 1 through N . Dish i has ai stones on it. There is also an empty bag.
You can perform the following two kinds of operations any number of times (possibly zero) in any order.
Remove one stone from each plate with one or more stones. Put the removed stones into the bag.
Take N stones out of the bag, and put one stone to each plate. This operation can be performed
only when the bag has N or more stones.
Let bi be the number of stones on plate i after you finished the operations. Print the number, modulo
998244353, of sequences of integers (b1 , b2 , … , bN ) of length N that can result from the operations.
Constraints
1 ≤ N ≤ 2 × 105
0 ≤ ai ≤ 109
Input
The input is given from Standard Input in the following format:
N
a1 a2 … aN
Output
Print the number, modulo 998244353, of possible sequences (b1 , b2 , … , bN ).
Copy
3
3 1 3
Copy
7
For example, b becomes (2, 1, 2) by the following procedure.
(0, 0, 0)
(1, 0, 1)
(1, 1, 1)
(2, 0, 2)
(2, 1, 2)
(2, 2, 2)
(3, 1, 3)
Copy
1
0
Copy
1
Copy
5
1 3 5 7 9
Copy
36
Copy
10
766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044
Sample Output 4 Copy
Copy
666174028
Algorithm:
The provided C++ code solves a problem involving two types of operations, A and B,
applied to a sequence of dishes with stones. The goal is to count the number of valid
sequences of operations that satisfy specific conditions. Here's an overview of the
key observations and the algorithm used to solve the problem:
1. Key Observations: The code makes two important observations that help
simplify the problem:
Operation A is never performed right after operation B.
Operation B is never performed right after taking stones from all the
dishes in an operation A.
2. Problem Transformation: The problem is transformed into counting
appropriate sequences of operations, represented as pairs of integers (x, y),
where x indicates the number of times operation A is performed, and y
indicates the number of times operation B is performed.
3. Expression for Counting Sequences: For a fixed value of x, the valid range of
y and the count of valid sequences are determined based on the following
conditions:
If x is less than or equal to the minimum frequency of operation A, y
must be 0 (one candidate). Performing operation B would violate the
second condition.
If the minimum frequency of operation A is less than x, then 0 ≤ y ≤
⌊s/N⌋, where s is the total number of stones in the dishes (⌊s/N⌋+1
candidates).
4. Floor Sum Algorithm: The problem's count of valid sequences is expressed
as a sum of floor values. The code uses the floor sum algorithm, which
computes these sums efficiently. The AtCoder Library's floor sum function is
utilized for this purpose.
5. Sample C++ Implementation: The provided C++ code reads input, sorts the
given sequence of frequencies of operation A, and then iterates through the
sorted sequence to compute the count of valid sequences of operations. The
algorithm leverages the floor sum function from the AtCoder Library to
calculate these sums efficiently.
Overall, the code demonstrates an approach to efficiently solve the problem using
key observations, transformation, and the floor sum algorithm. The complexity of the
solution is suitable for the given constraints, ensuring a fast and efficient solution.
Code:
#include <bits/stdc++.h>
#include "atcoder/math.hpp"
#include "atcoder/modint.hpp"
int main() {
ll N;
cin >> N;
vector<ll> A(N);
sort(begin(A), end(A));
ll s = A[0] * N;
ll L = A[i - 1];
ll R = A[i];
ll a = N - i;
ll b = s - L * (N - i);
ll m = N;
ll cur = 0;
cur += R - L;
ans += cur;
s += (R - L) * (N - i);
}
cout << ans.val() << "\n";
}
Ex - Group Photo Editorial
/
Score : 650 points
Problem Statement
(2N + 1) people are forming two rows to take a group photograph. There are N people in the front row;
the i-th of them has a height of Ai . There are (N + 1) people in the back row; the i-th of them has a
height of Bi . It is guaranteed that the heights of the (2N + 1) people are distinct. Within each row, we
Suppose that the heights of the people in the front row are a1 , a2 , … , aN from the left, and those in the
back row are b1 , b2 , … , bN +1 from the left. This arrangement is said to be good if all of the following
a1 < b1 .
aN < bN +1 .
Among the N ! ways to rearrange the front row, how many of them, modulo 998244353, are such ways
that we can rearrange the back row to make the arrangement good?
Constraints
1 ≤ N ≤ 5000
1 ≤ Ai , Bi ≤ 109
Ai =
Aj (1 ≤ i < j ≤ N )
Bi =
Bj (1 ≤ i < j ≤ N + 1)
Ai =
Bj (1 ≤ i ≤ N , 1 ≤ j ≤ N + 1)
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
B1 B2 … BN +1
Output
Print the answer as an integer.
Sample Input 1 Copy
Copy
3
1 12 6
4 3 10 9
Copy
2
When a = (1, 12, 6), we can let, for example, b = (4, 3, 10, 9) to make the arrangement good.
When a = (6, 12, 1), we can let, for example, b = (10, 9, 4, 3) to make the arrangement good.
When a is one of the other four ways, no rearrangement of the back row (that is, none of the
possible 24 candidates of b) makes the arrangement good.
Copy
1
5
1 10
Copy
0
Copy
10
189330739 910286918 802329211 923078537 492686568 404539679 822804784 303238506 650287940 1
125660016 430302156 982631932 773361868 161735902 731963982 317063340 880895728 1000000000 707723857 4
Copy
3542400
Algorithm:
The provided C++ code solves a specific problem that involves counting good
arrangements of elements from two sequences, A and B, while satisfying certain
conditions. Here's a step-by-step explanation of the code:
1. Input:
Reads the value of n, the number of elements in the sequences A and
B.
Reads the elements of sequence A.
Reads the elements of sequence B (with an extra element to
accommodate the index shift when checking conditions).
2. Sorting:
Sorts both sequences A and B in ascending order.
3. Dynamic Programming (DP) Approach:
The code uses a 2D DP table dp with dimensions (n+1) x (n+1) to
keep track of the number of good arrangements.
Initializes dp[0][0] to 1, representing the base case where no elements
from A or B are used.
4. Iterative DP:
Loops through the elements of sequence A (indexed by i) to determine
the relative ordering between elements in A and elements in B.
For each element in A (A[i]), the code iterates through possible
numbers of connected components (indexed by j), where j represents
the number of elements in B that are less than or equal to A[i].
For each combination of i and j, the code considers three patterns
(indexed by p):
Pattern 0: A[i] forms a new independent connected component.
Pattern 1: A[i] sticks onto the left or right of an existing
connected component.
Pattern 2: A[i] glues two existing connected components to
reduce the number of connected components by one.
The code checks conditions to ensure that the elements from B are
greater than the elements from A, and updates the DP table
accordingly.
5. Output:
The code outputs the result, which corresponds to the value dp[n][1],
representing the number of good arrangements that satisfy the
conditions.
6. Modular Arithmetic:
The code uses modint998244353 from the AtCoder Library, which is a
modular integer type that ensures operations are performed modulo
998244353. This is commonly used in competitive programming to
avoid integer overflow and efficiently compute results modulo a prime
number.
Overall, the code uses dynamic programming to efficiently count good arrangements
while satisfying specific conditions. The approach is based on the relative ordering
between elements in sequences A and B, and the DP table tracks the number of valid
arrangements.
Code:
#include<bits/stdc++.h>
#include<atcoder/modint>
int main() {
int n;
cin >> n;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
dp[0][0] = 1;
if (p > j) break;
if (i + j + 2 - p > n + 1) con0nue;
mint coef;
if (p == 0) coef = j + 1;
else if (p == 1) coef = 2 * j;
else coef = j - 1;
}
THANK YOU
A DETAILED EDITORIAL FOR
ATCODER – ATCODER PROGRAMMING CONTEST 2023
SUMMER (ATCODER BEGINNER CONTEST 314)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by the users
“en_translator (English) & physics (Japanese)”
CONTEST PROBLEM
STATEMENTS, HINTS AND
ALGORITHMS WITH CODE
A - 3.14 /
Score : 100 points
Problem Statement
The number pi to the 100-th decimal place is
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679.
More precisely, truncate the value of pi to N decimal places and print the result without removing the
trailing 0s.
Constraints
1 ≤ N ≤ 100
N is an integer.
Input
The input is given from Standard Input in the following format:
Output
Print the value of pi to the N -th decimal place in a single line.
Copy
2
Copy
3.14
Truncating the value of pi to 2 decimal places results in 3.14. Thus, you should print 3.14.
Sample Input 2 Copy
Copy
32
Copy
3.14159265358979323846264338327950
Copy
100
Copy
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Algorithm:
Step 1: Start
Step 3: Ini alize a string variable pi with the constant string "3.14".
Step 6: Print the character at the i-th index of the string pi.
Step 9: End
Code:
#include <string>
#include <iostream>
int main() {
string pi =
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208
9986280348253421170679";
int N;
cin >> N;
return 0;
}
B - Roulette /
Score : 200 points
Problem Statement
N people, person 1, person 2, …, person N , are playing roulette. The outcome of a spin is one of the
37 integers from 0 to 36. For each i = 1, 2, … , N , person i has bet on Ci of the 37 possible
The wheel has been spun, and the outcome is X . Print the numbers of all people who have bet on X
with the fewest bets, in ascending order.
More formally, print all integers i between 1 and N , inclusive, that satisfy both of the following
conditions, in ascending order:
Note that there may be no number to print (see Sample Input 2).
Constraints
1 ≤ N ≤ 100
1 ≤ Ci ≤ 37
0 ≤ Ai,j ≤ 36
0 ≤ X ≤ 36
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
C1
C2
⋮
CN
AN ,1 AN ,2 … AN ,CN
X
Output
Let B1 , B2 , … , BK be the sequence of numbers to be printed in ascending order. Using the following
format, print the count of numbers to be printed, K , on the first line, and B1 , B2 , … , BK separated by
K
B1 B2 … BK
Copy
4
3
7 19 20
4
4 19 24 0
2
26 10
3
19 31 24
19
Copy
2
1 4
The wheel has been spun, and the outcome is 19. The people who has bet on 19 are person 1, person
2, and person 4, and the number of their bets are 3, 4, and 3, respectively. Therefore, among the people
who has bet on 19, the ones with the fewest bets are person 1 and person 4.
Copy
3
1
1
1
2
1
3
0
Copy
0
The wheel has been spun and the outcome is 0, but no one has bet on 0, so there is no number to print.
Algorithm:
Step 1: Start
c. For each person i from 1 to n, read the number of bets they made, c[i], and store the bets
in an array a[i].
a. For each person i from 1 to n, if they bet on the value x, add i to vec.
Step 4: Find the minimum value Cmin among the number of bets made by the people in vec.
a. For each person i in vec, if they made the minimum number of bets (c[i] == Cmin), add i to
ans.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
int main(void)
int n, x, c[101];
vector<int> a[101];
cin >> n;
a[i].resize(c[i]);
}
cin >> x;
vector<int> vec;
vector<int> ans;
return 0;
}
C - Rotate Colored Subsequence /
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. Each character of S is
painted in one of the M colors: color 1, color 2, ..., color M ; for each i = 1, 2, … , N , the i-th character
of S is painted in color Ci .
For each i = 1, 2, … , M in this order, let us perform the following operation.
Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p1 -th, p2 -th, p3 -
th, …, pk -th characters are painted in color i from left to right, then simultaneously replace the p1 -
th, p2 -th, p3 -th, …, pk -th characters of S with the pk -th, p1 -th, p2 -th, …, pk−1 -th characters of S ,
respectively.
The constraints guarantee that at least one character of S is painted in each of the M colors.
Constraints
1 ≤ M ≤ N ≤ 2 × 105
1 ≤ Ci ≤ M
Input
The input is given from Standard Input in the following format:
N M
S
C1 C2 … CN
Output
Print the answer.
Copy
cszapqbr
Initially, S = apzbqrcs.
For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th
characters, resulting in S = cpzaqrbs.
For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th
characters, resulting in S = cszapqbr.
For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting
in S = cszapqbr (here, S is not changed).
Copy
2 1
aa
1 1
Copy
aa
Algorithm:
Step 1: Start
c. For each index i from 0 to n-1, read the color value, c[i], and store the posi"on i in the
variable-length array p corresponding to color c[i].
a. Get the size k of the array p[i] (posi"ons for color i).
Step 6: End
Code:
#include <iostream>
#include <vector>
int n, m;
string s;
int c[200000];
vector<int> p[200001];
int main(void)
cin >> s;
int k = p[i].size();
return 0;
}
D - LOWER /
Score : 400 points
Problem Statement
You are given a string S of length N consisting of uppercase and lowercase English letters.
If ti = 2, convert all uppercase letters in S to lowercase (do not use xi , ci for this operation).
If ti = 3, convert all lowercase letters in S to uppercase (do not use xi , ci for this operation).
Constraints
1 ≤ N ≤ 5 × 105
S is a string of length N consisting of uppercase and lowercase English letters.
1 ≤ Q ≤ 5 × 105
1 ≤ ti ≤ 3 (1 ≤ i ≤ Q)
If ti =
1, then xi = 0 and ci = 'a'.
Input
The input is given from Standard Input in the following format:
N
S
Q
t 1 x1 c1
t 2 x2 c2
⋮
t Q xQ cQ
Output
Print the answer in a single line.
Sample Input 1 Copy
Copy
7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y
Copy
atcYber
Copy
35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i
Copy
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
Algorithm:
Step 1: Start
b. Ini alize a vector status to store the status of each character in the string. Each element is a
pair of (last update me, current character).
a. Read the me, index, and character for the opera on.
b. If the opera on is an individual opera on (t == 1), update the status for the character at
index (x-1) in the vector status.
c. If the opera on is a global opera on (t == 2), record the last global opera on (last update
me, kind of last update).
a. Check if there's no global opera on (!fill) or if the last update me of the character is greater
than or equal to the last global opera on's me.
Step 6: End
This algorithm maintains a vector status to keep track of each character's status, including the last
update me and the current character. It also tracks the last global opera on. During the prin ng step,
it checks whether the character's last update was affected by a global opera on and adjusts the
character accordingly.
Code:
#include <string>
#include <iostream>
#include <vector>
int main() {
int N;
string S;
vector<pair<int, char>> status(N); // Individual opera on (last update me, current character)
int Q;
cin >> Q;
op onal<pair<int, int>> fill = nullopt; // Global opera on (last update me, kind of last update)
int t, x;
char c;
if (t == 1) // individual opera on
else if (fill->second == 2) // otherwise, depending on the kind of the global opera on,
cout << sta c_cast<char>(tolower(c)); // make it lower
else
return 0;
}
E - Roulettes /
Score : 475 points
Problem Statement
There are N roulette wheels. The i-th (1 ≤ i ≤ N ) wheel has Pi integers Si,1 , Si,2 , … , Si,Pi written on it,
and you can play it once by paying Ci yen. When you play the i-th wheel once, an integer j between 1
The points you earn from the wheels are determined independently of past results.
Takahashi wants to earn at least M points. Takahashi will act to minimize the amount of money he pays
before he earns at least M points. After each play, he can choose which wheel to play next based on the
previous results.
Find the expected amount of money Takahashi will pay before he earns at least M points.
Constraints
1 ≤ N ≤ 100
1 ≤ M ≤ 100
1 ≤ Ci ≤ 104 (1 ≤ i ≤ N )
1 ≤ Pi ≤ 100 (1 ≤ i ≤ N )
0 ≤ Si,j ≤ M (1 ≤ i ≤ N , 1 ≤ j ≤ Pi )
Pi
j=1
All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
C1 P1 S1,1 S1,2 … S1,P1
⋮
CN PN SN ,1 SN ,2 … SN ,PN
Output
Print the expected amount of money Takahashi will pay until he earns at least M points in a single line.
Your output will be considered correct when the relative or absolute error from the true value is at most
10−5 .
Copy
3 14
100 2 5 9
50 4 1 2 4 8
70 5 2 4 2 8 8
Copy
215.913355350494384765625
Your output will be considered correct when the relative or absolute error from the true value is at most
10−5 , so outputs such as 215.9112 and 215.9155 would also be considered correct.
Copy
2 100
1 2 1 2
10 6 0 0 0 0 0 100
Copy
60
Copy
20 90
3252 9 0 4 2 7 3 2 3 2 4
2147 1 1
4033 8 0 4 1 7 5 2 5 0
3795 6 6 6 2 3 2 2
3941 7 2 4 4 7 2 0 5
2815 6 2 1 0 5 2 2
3020 2 3 6
3858 9 4 2 7 3 0 4 4 6 5
4533 10 3 6 4 0 6 4 4 2 7 7
4198 8 6 7 0 6 3 6 5 6
3739 8 2 7 1 5 1 4 4 7
2465 4 1 4 0 1
4418 9 7 6 2 4 6 1 5 0 7
5450 12 0 4 4 7 7 4 4 5 4 5 3 7
4196 9 1 6 5 5 7 2 3 6 3
4776 9 2 2 7 3 6 6 1 6 6
2286 3 3 5 6
3152 3 4 1 5
3509 7 0 6 7 0 1 0 3
2913 6 0 1 5 0 5 6
Copy
45037.072314895291126319493887599716
Algorithm:
The provided editorial explains the algorithm for solving problem E called "Roule es" from the
programming contest. The problem involves finding the expected amount of money needed to reach
a certain number of points in a game of roule e with mul ple wheels.
1. Construct the Wheel Informa on: Read the input, which includes the parameters for each wheel:
cost `C`, number of integers `P`, and the values `S` on the wheel.
- Remove zeros from the list of values `S` on each wheel and adjust the cost `C` and the number of
integers `P` accordingly. This is done to avoid 0 points in the computa on.
- Compute the expected amount of money required to reach `M` points, star ng from `i` points, for
each `i` from `M-1` down to `0`.
- Calculate the expected value a0er playing the wheel, considering the values `S` on the wheel that
don't exceed the target `M` points.
- Update the expected amount `e[i]` with the minimum cost (`C`) plus the average of the expected
values a0er playing the wheel, considering the number of integers (`P`).
- The answer is the expected amount of money (`e[0]`) required to reach the target `M` points
star ng from 0 points.
Code:
#include <string>
#include <iostream>
#include <vector>
int main() {
int N;
string S;
int Q;
cin >> Q;
op onal<pair<int, int>> fill = nullopt; // Global opera on (last update me, kind of last update)
int t, x;
char c;
if (t == 1) // individual opera on
else if (fill->second == 2) // otherwise, depending on the kind of the global opera on,
else
return 0;
}
F - A Certain Game /
Score : 475 points
Problem Statement
N players, player 1, player 2, ..., player N , participate in a game tournament. Just before the
tournament starts, each player forms a one-person team, so there are N teams in total.
The tournament has a total of N − 1 matches. In each match, two different teams are chosen. One team
goes first, and the other goes second. Each match will result in exactly one team winning. Specifically,
for each i = 1, 2, … , N − 1, the i-th match proceeds as follows.
The team with player pi goes first, and the team with player qi goes second.
Let a and b be the numbers of players in the first and second teams, respectively. The first team
a b
wins with probability a+b , and the second team wins with probability a+b
.
For each of the N players, print the expected number of times the team with that player wins throughout
the tournament, modulo 998244353.
Constraints
2 ≤ N ≤ 2 × 105
1 ≤ pi , qi ≤ N
Just before the i-th match, player pi and player qi belong to different teams.
Input
The input is given from Standard Input in the following format:
N
p1 q1
p2 q2
⋮
pN −1 qN −1
Output
For each i = 1, 2, … , N , print Ei , the expected number, modulo 998244353, of times the team with
E1 E2 … EN
Copy
5
1 2
4 3
5 3
1 4
Copy
698771048 698771048 964969543 964969543 133099248
The first match is played by team {1}, with player 1, and team {2}, with player 2. Team {1} wins
with probability 12 , and team {2} wins with probability 12 . Then, the two teams are combined into a
The expected numbers of times the teams with players 1, 2, 3, 4, 5 win throughout the tournament,
9 9 53 53 14
E1 , E2 , E3 , E4 , E5 , are 10
, 10 , 30 , 30 , 15 , respectively.
Copy
15
9 2
8 10
13 6
12 11
7 10
4 10
14 2
5 4
1 15
15 2
6 9
8 11
6 3
2 8
Copy
43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280
Algorithm:
Code:
MOD = 998244353
def mod_inverse(a):
parent = list(range(N))
size = [1] * N
base_expect = [0] * N
delta = [0] * N
wins = [1] * N
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
if px != py:
parent[px] = py
size[py] += size[px]
base_expect[py] += base_expect[px] + w
delta[py] += delta[px]
wins[px] = size[px] * w * mod_inverse(size[px] + size[py])
merge(p - 1, q - 1, i + 2)
total_wins = sum(wins)
total_expect = 0
for i in range(N):
return result
# Sample Input 1
N1 = 5
# Sample Input 2
N2 = 15
matches2 = [(9, 2), (8, 10), (13, 6), (12, 11), (7, 10), (4, 10), (14, 2), (5, 4), (1, 15), (15, 2), (6, 9), (8, 11),
(6, 3), (2, 8)]
print(*solve_game(N2, matches2))
G - Amulets /
Score : 575 points
Problem Statement
There are N monsters in a cave: monster 1, monster 2, …, monster N . Each monster has a positive
integer attack power and a type represented by an integer between 1 and M , inclusive. Specifically,
for i = 1, 2, … , N , the attack power and type of monster i are Ai and Bi , respectively.
Takahashi will go on an adventure in this cave with a health of H and some of the M amulets: amulet 1,
amulet 2, …, amulet M .
In the adventure, Takahashi performs the following steps for i = 1, 2, … , N in this order (as long as his
health does not drop to 0 or below).
If Takahashi has not brought amulet Bi with him, monster i will attack him and decrease his health
by Ai .
Then,
if his health is greater than 0, he defeats monster i;
otherwise, he dies without defeating monster i and ends his adventure.
Find the maximum number of monsters that Takahashi can defeat when choosing K of the M
amulets to bring on the adventure.
The constraints guarantee that there is at least one monster of type i for each i = 1, 2, … , M .
Constraints
1 ≤ M ≤ N ≤ 3 × 105
1 ≤ H ≤ 109
1 ≤ Ai ≤ 109
1 ≤ Bi ≤ M
Input
The input is given from Standard Input in the following format:
N M H
A1 B1
A2 B2
⋮
AN BN
Output
For each i
= 0, 1, 2, … , M , let Xi be the maximum number of monsters that Takahashi can defeat
X0 X1 … XM
Copy
7 3 7
3 2
1 1
4 2
1 2
5 1
9 3
2 3
Copy
2 5 7 7
Consider the case K = 1. Here, Takahashi can bring amulet 2 to defeat the maximum possible number
of monsters, which is 5. The adventure proceeds as follows.
For i = 1, he avoids the attack of monster 1 since he has amulet 2. Then, he defeats monster 1.
For i = 2, he takes the attack of monster 2 and his health becomes 6 since he does not have
amulet 1. Then, he defeats monster 2.
For i = 3, he avoids the attack of monster 3 since he has amulet 2. Then, he defeats monster 3.
For i = 4, he avoids the attack of monster 4 since he has amulet 2. Then, he defeats monster 4.
For i = 5, he takes the attack of monster 5 and his health becomes 1 since he does not have
amulet 1. Then, he defeats monster 5.
For i = 6, he takes the attack of monster 6 and his health becomes −8 since he does not have
amulet 3. Then, he dies without defeating monster 6 and ends his adventure.
Copy
15 5 400
29 5
27 4
79 1
27 2
30 3
4 1
89 2
88 3
75 5
3 1
39 4
12 1
62 4
38 2
49 1
Copy
8 12 15 15 15 15
Algorithm:
Code:
#include <bits/stdc++.h>
using std::cout;
using std::cin;
using std::endl;
const ll ILL=2167167167167167167;
namespace atcoder {
namespace internal {
// @param n `0 <= n`
int ceil_pow2(int n) {
int x = 0;
return x;
// @param n `1 <= n`
#ifdef _MSC_VER
_BitScanForward(&index, n);
return index;
#else
#endif
} // namespace internal
public:
segtree() : segtree(0) {}
log = internal::ceil_pow2(_n);
update(i);
}
void set(int p, S x) {
p += size;
d[p] = x;
S get(int p) {
S prod(int l, int r) {
l += size;
r += size;
while (l < r) {
l >>= 1;
r >>= 1;
assert(f(e()));
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
return l - size;
sm = op(sm, d[l]);
l++;
return _n;
}
template <class F> int min_leP(int r, F f) {
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
if (!f(op(d[r], sm))) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
return r + 1 - size;
sm = op(d[r], sm);
return 0;
private:
std::vector<S> d;
};
} // namespace atcoder
using F= pair<ll,ll>;
F e(){return {0,0};}
ll target;
return target>a.first;
void solve();
// oddloop
int main() {
ios::sync_with_stdio(false);
cin. e(nullptr);
int t=1;
//cin>>t;
rep(i,0,t) solve();
void solve(){
int N,M;
ll H;
cin>>N>>M>>H;
target=H;
vector<ll> A(N),B(N);
rep(i,0,N) cin>>A[i]>>B[i],B[i]--;
vector<ll> S(N);
vector<pair<ll,int>> p;
rep(i,0,M){
p.push_back({0,i});
rep(i,0,N){
S[B[i]]+=A[i];
p.push_back({S[B[i]],B[i]});
vector<vector<int>> G(M);
vector<int> ind(M);
So(p);
segtree<F,op,e> seg(N+M);
rep(i,0,N+M){
G[p[i].second].push_back(i);
rep(i,0,M) seg.set(i,{0,1});
vector<int> ans(M+1);
rep(i,0,M) S[i]=0;
rep(i,0,N){
seg.set(G[B[i]][ind[B[i]]],e());
ind[B[i]]++;
S[B[i]]+=A[i];
seg.set(G[B[i]][ind[B[i]]],{S[B[i]],1});
int b=seg.max_right<f>(0);
auto tmp=seg.prod(0,b);
ans[M-tmp.second]=i+1;
rep(i,0,M) chmax(ans[i+1],ans[i]);
vec_out(ans);
}
Ex - Disk and Segments /
Score : 625 points
Problem Statement
There are N line segments in a coordinate plane, and the i-th line segment (1 ≤ i ≤ N ) has two points
(ai , bi ) and (ci , di ) as its endpoints. Here, each line segment includes its endpoints. Additionally, no two
We want to place a single closed disk in this plane so that it shares a point with each line segment. In
other words, we want to draw a single circle so that each line segment shares a point with either the
circumference of the circle or its interior (or both). Find the smallest possible radius of such a disk.
Constraints
2 ≤ N ≤ 100
0 ≤ ai , bi , ci , di ≤ 1000 (1 ≤ i ≤ N )
(ai , bi )
= (ci , di ) (1 ≤ i ≤ N )
Input
The input is given from Standard Input in the following format:
N
a 1 b 1 c1 d 1
a 2 b 2 c2 d 2
⋮
a N b N cN d N
Output
Print the answer in a single line. Your output will be considered correct when the absolute or relative
error from the true value is at most 10−5 .
Copy
4
2 3 2 10
4 0 12 6
4 8 6 3
7 8 10 8
Copy
3.319048676309097923796460081961
The given line segments are shown in the figure below. The closed disk shown in the figure, centered at
32 − 115 21 − 115 24 − 115
( , ) with a radius of
It is impossible to place a disk with a radius less than so that it shares a point with all the
4
24 − 115
Your output will be considered correct if the absolute or relative error from the true value is at most 10−5 ,
so outputs such as 3.31908 and 3.31902 would also be considered correct.
Copy
20
0 18 4 28
2 21 8 21
3 4 10 5
3 14 10 13
5 9 10 12
6 9 10 6
6 28 10 18
12 11 15 13
12 17 12 27
13 17 20 18
13 27 19 26
16 1 16 13
16 22 19 25
17 22 20 19
18 4 23 4
18 5 23 11
22 16 22 23
23 15 30 15
23 24 30 24
24 0 24 11
Copy
12.875165712523887403637822024952
18 9
3757 29 − 44 206618
Copy
30
526 655 528 593
628 328 957 211
480 758 680 794
940 822 657 949
127 23 250 385
281 406 319 305
277 598 190 439
437 450 725 254
970 478 369 466
421 225 348 141
872 64 600 9
634 460 759 337
878 514 447 534
142 237 191 269
983 34 554 284
694 160 589 239
391 631 22 743
377 656 500 606
390 576 184 312
556 707 457 699
796 870 186 773
12 803 505 586
343 541 42 165
478 340 176 2
39 618 6 651
753 883 47 833
551 593 873 672
983 729 338 747
721 77 541 255
0 32 98 597
Copy
485.264732620930836460637042310401
Algorithm:
Detailed step-by-step algorithm for the given code, which finds the minimum radius
of a closed disk centered at a point (x, y) that shares a point with all the given segments:
1. Input:
Read the number of segments, N.
Read the coordinates of N segments, where each segment is represented
by four real numbers (a, b, c, d) representing the endpoints (a, b) and (c,
d) of the segment.
2. Distance Calculation:
Define a function distance_point_and_point that calculates the Euclidean
distance between two points given their coordinates (x, y) and (z, w).
3. Distance from Point to Segment:
Define a function distance_segment_and_point that calculates the minimum
distance from a given point (x, y) to a segment defined by its endpoints
(a, b) and (c, d). It considers both cases where the point is outside the
segment's bounding box and where it is inside.
4. Minimum Crossing Circle:
Define a function minimum_crossing_circle that calculates the minimum
radius of a closed circle centered at a given point (x, y) that shares a point
with all segments. This function iterates through all segments and
updates the maximum distance from the point to any segment.
5. Trinary Search for Unimodal Function:
Define a function minimize_unimodal_function that uses trinary search to
find the minimum value of a unimodal function within a given range. This
function takes as input a left bound (L), a right bound (R), and a function
to minimize within that range.
6. Minimize the Function:
Use the minimize_unimodal_function function twice:
First, to find the minimum value of f(x, y) for a fixed x by using a
nested trinary search that minimizes the function
minimum_crossing_circle for a fixed x and varying y.
Second, to find the minimum value of the unimodal function from
the previous step by varying x.
7. Output:
Output the minimum radius of the closed circle that shares a point with
all segments, which is the result of the trinary search.
8. Convexity Proofs:
Prove that the distance function d_p between a point p and another point
(x, y) is convex (Lemma 1).
Prove that the distance function d_s between a segment s and a point (x,
y) is convex (Lemma 2).
Prove that the maximum of convex functions remains convex (Lemma 3).
9. Convexity of f(x, y):
Prove that the function f(x, y), which represents the minimum radius of
the circle, is convex based on the convexity properties of d_s and Lemma
3.
10. Convexity of F(x):
Prove that the function F(x), which represents the minimum value of f(x, y) for a
fixed x, is convex (F(x) is a unimodal function).
Code:
#include <iostream>
#include <tuple>
#include <vector>
#include <cmath>
#include <ranges>
int main() {
unsigned N;
cin >> N;
vector<segment_type> segments(N);
}};
if ((a - c) * (a - x) + (b - d) * (b - y) < 0)
if ((c - a) * (c - x) + (d - b) * (d - y) < 0)
}};
// minimum radius of a closed circle centered at the given point that shares a point with all
segments
real ret{};
return ret;
}};
R = M2;
else
L = M1;
return func2on(L);
}};
});
}) << endl;
return 0;
}
THANK YOU
A DETAILED EDITORIAL FOR
ATCODER – KEYENCE PROGRAMMING CONTEST 2023
SUMMER (ATCODER BEGINNER CONTEST 315)
Editorial by
Dr. Dinesh Kumar Anguraj, Asso. Prof., Dept. of CSE.
Website Link: https://atcoder.jp/
Note & Credits: Problem statement and Hints from Atcoder.jp by the users
“en_translator (English) & physics (Japanese)”
CONTEST PROBLEM
STATEMENTS, HINTS AND
ALGORITHMS WITH CODE
A - tcdr /
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Remove all occurrences of a, e, i, o, u from S and print the resulting string.
S contains at least one character other than a, e, i, o, u.
Constraints
S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
S contains at least one character other than a, e, i, o, u.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Copy
atcoder
Copy
tcdr
Copy
xyz
Sample Output 2 Copy
Copy
xyz
Copy
aaaabbbbcccc
Copy
bbbbcccc
Algorithm:
1. Input: Prompt the user to enter a string and store it in the variable S.
2. Ini alize a string vowels with the characters "aeiou" which represent the vowels.
3. Ini alize an empty string result to store the final string with vowels removed.
4. Itera on: Loop through each character char in the input string S:
Check if char is not in the vowels string (i.e., if it's not a vowel).
5. Output: Print the result string which contains the input string with all vowels removed.
Code:
S = input().strip()
vowels = "aeiou"
print(result)
B - The Middle Day /
Score : 200 points
Problem Statement
In the calendar of AtCoderLand, a year consists of M months: month 1, month 2, …, month M . The i-th
month consists of Di days: day 1, day 2, …, day Di .
Find what day of what month is the middle day of the year.
In other words, let day 1 of month 1 be the first day, and find a and b such that the ((D1 + D2 + ⋯ +
Constraints
All input values are integers.
1 ≤ M ≤ 100
1 ≤ Di ≤ 100
D1 + D2 + ⋯ + DM is odd.
Input
The input is given from Standard Input in the following format:
M
D1 D2 … DM
Output
Let the answer be day b of month a, and print it in the following format:
a b
Copy
12
31 28 31 30 31 30 31 31 30 31 30 31
Copy
1
1
Copy
1 1
Copy
6
3 1 4 1 5 9
Copy
5 3
Algorithm:
1. Input: Prompt the user to input an integer M, represen ng the number of months. Then
prompt the user to input M integers separated by spaces and store them in a list D. These
integers represent the number of days in each month.
2. Calculate the total number of days by summing up the elements in the list D, and store the
result in the variable total_days.
3. Calculate the index of the middle day using the formula (total_days + 1) // 2. This index will
be used to find the middle day in the total days span.
current_month with 0, which will be used to keep track of the current month being
considered.
5. Loop:
While the middle_day_index is greater than the number of days in the current
month (D[current_month]):
6. Output: Print the current month index plus 1 (since months are typically represented star ng
from 1) and the remaining value of middle_day_index. This represents the month and day in
the middle of the total days spanned by the given months.
Code:
M = int(input())
D = list(map(int, input().split()))
total_days = sum(D)
middle_day_index = (total_days + 1) // 2
current_month = 0
middle_day_index -= D[current_month]
current_month += 1
print(current_month + 1, middle_day_index)
C - Flavors /
Score : 300 points
Problem Statement
We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are Fi and Si , respectively (Si is an even number).
Constraints
All input values are integers.
2 ≤ N ≤ 3 × 105
1 ≤ Fi ≤ N
2 ≤ Si ≤ 109
Si is even.
Input
Input is given from Standard Input in the following format:
N
F1 S 1
F2 S 2
⋮
FN S N
Output
Print the answer as an integer.
Copy
16
Copy
4
4 10
3 2
2 4
4 12
Copy
17
Main Program:
Create a 2D vector bk of size n+1 to store the items. Each sub-vector bk[i] represents items
with the same first component (f) as i.
Processing Steps:
Create a vector best to store the best values from each sub-vector.
Sort the sub-vector in descending order (to consider items with the highest second
component first).
If the sub-vector has at least two items, calculate the value by adding the two largest
second components, divided by 2, and update res if the calculated value is greater
than the current res.
If the sub-vector has at least one item, add the largest second component to the best
vector.
If the best vector has at least two items, calculate the value by adding the two largest values
and update res if the calculated value is greater than the current res.
Summary:
The code processes a list of items, where each item has two components f and s. It iterates through
the items, categorizing them based on their f component into sub-vectors. It then calculates the
maximum possible value by considering various combina"ons of items based on specific condi"ons
and outputs the result. The goal of the code is to maximize the total value by selec"ng and combining
items op"mally.
Code:
#include<bits/stdc++.h>
int n;
cin >> n;
vector<vector<int>> bk(n+1);
for(int i=0;i<n;i++){
int f,s;
bk[f].push_back(s);
int res=0;
vector<int> best;
for(int i=1;i<=n;i++){
sort(bk[i].begin(),bk[i].end());
reverse(bk[i].begin(),bk[i].end());
if(bk[i].size()>=2){
res=max(res,bk[i][0]+bk[i][1]/2);
if(bk[i].size()>=1){
best.push_back(bk[i][0]);
sort(best.begin(),best.end());
reverse(best.begin(),best.end());
if(best.size()>=2){res=max(res,best[0]+best[1]);}
return 0;
}
D - Magical Cookies /
Score : 400 points
Problem Statement
There are H × W cookies in H rows and W columns.
The color of the cookie at the i-row from the top and j -th column from the left is represented by a
lowercase English letter ci,j .
1. For each row, perform the following operation: if there are two or more cookies remaining in the row
and they all have the same color, mark them.
2. For each column, perform the following operation: if there are two or more cookies remaining in the
column and they all have the same color, mark them.
3. If there are any marked cookies, remove them all and return to 1; otherwise, terminate the procedure.
Constraints
2 ≤ H, W ≤ 2000
ci,j is a lowercase English letter.
Input
The input is given from Standard Input in the following format:
H W
c1,1 c1,2 … c1,W
⋮
cH,1 cH,2 … cH,W
Output
Print the answer.
Sample Input 1 Copy
Copy
4 3
aaa
aaa
abc
abd
Copy
2
At this point, the cookies look like the following, where . indicates a position where the cookie has been
removed.
...
...
.bc
.bd
1. Do nothing.
2. Mark the cookies in the second column.
3. Remove the marked cookies.
At this point, the cookies look like the following, where . indicates a position where the cookie has been
removed.
...
...
..c
..d
1. Do nothing.
2. Do nothing.
3. No cookies are marked, so terminate the procedure.
Copy
2 5
aaaaa
abcde
Sample Output 2 Copy
Copy
4
Copy
3 3
ooo
ooo
ooo
Copy
0
Algorithm:
Main Program:
Read two integers h and w, which represent the dimensions of the grid.
Loop through the rows and columns of the grid and read each character into the corresponding
posi on in the vector c.
Processing Steps:
Create two 2D vectors x and y, each of size (h, 26) and (w, 26) respec vely.
Increment the corresponding character count in the x vector for each row and in the
y vector for each column.
Ini alize two variables hc and wc with the values of h and w respec vely. These variables will
keep track of the current number of rows and columns that haven't been excluded.
Create two boolean vectors fx and fy, each of size h and w respec vely. These vectors will be
used to mark rows and columns that are excluded.
Create empty vectors ux and uy to store pairs of row and column indices that sa sfy
certain condi ons.
For each character count x[i][j], if it equals wc and wc is greater than or equal
to 2, add the pair (i, j) to ux.
For each character count y[i][j], if it equals hc and hc is greater than or equal
to 2, add the pair (i, j) to uy.
For each pair (i, j) in ux, mark the row i as excluded (fx[i] = true), decrement all y[i][j]
counts by 1, and decrement hc by 1.
For each pair (i, j) in uy, mark the column i as excluded (fy[i] = true), decrement all
x[i][j] counts by 1, and decrement wc by 1.
Summary:
The code processes a grid of characters and itera vely eliminates certain rows and columns based on
certain condi ons. It then calculates and prints the product of the remaining row and column counts,
effec vely giving the count of the remaining cells in the grid a1er applying the opera ons. The
opera ons involve selec ng rows and columns where certain character counts are met, and excluding
those that fulfill the condi ons.
Code:
#include <iostream>
#include <vector>
int main() {
int h, w;
rep(i, h) rep(j, w) {
x[i][c[i][j] - 'a']++;
y[j][c[i][j] - 'a']++;
int hc = h, wc = w;
rep(_, h + w) {
rep(i, h) {
ux.push_back({ i, j });
rep(i, w) {
rep(j, 26) {
uy.push_back({ i, j });
fx[p.first] = true;
rep(i, w) y[i][p.second]--;
hc--;
fy[p.first] = true;
rep(i, h) x[i][p.second]--;
wc--;
}
E - Prerequisites /
Score : 425 points
Problem Statement
We have N books numbered 1 to N .
Book i assumes that you have read Ci books, the j -th of which is book Pi,j : you must read all these Ci
You are trying to read the minimum number of books required to read book 1.
Print the numbers of the books you must read excluding book 1 in the order they should be read. Under
this condition, the set of books to read is uniquely determined.
If there are multiple reading orders that satisfy the condition, you may print any of them.
Constraints
2 ≤ N ≤ 2 × 105
0 ≤ Ci < N
N
∑i=1 Ci ≤ 2 × 105
C1 ≥ 1
1 ≤ Pi,j ≤ N
Pi,j =
Pi,k for 1 ≤ j < k ≤ Ci .
Input
The input is given from Standard Input in the following format:
N
C1 P1,1 … P1,C1
C2 P2,1 … P2,C2
⋮
CN PN ,1 … PN ,CN
Output
Print the numbers of the books you must read to read book 1 in the order they should be read, with
spaces in between.
Sample Input 1 Copy
Copy
6
3 2 3 4
2 3 5
0
1 5
0
0
Copy
5 3 4 2
To read book 1, you must read books 2, 3, 4; to read book 2, you must read books 3, 5; to read book 4,
you must read book 5. To read books 3, 5, 6, you do not have to read any other books.
For example, if you read books 5, 3, 4, 2 in this order, you can read book 1. This is a correct answer,
because you will never be able to read book 1 with three or fewer books read. As another example,
reading books 3, 5, 4, 2 in this order also allows you to read book 1 with 4 books read.
Copy
6
1 2
1 3
1 4
1 5
1 6
0
Copy
6 5 4 3 2
Copy
8
1 5
1 6
1 7
1 8
0
0
0
0
Sample Output 3 Copy
Copy
5
Algorithm:
It calculates the in-degrees of each node (vertex) by itera ng through the graph and
incremen ng in-degrees for adjacent nodes.
It ini alizes a queue and adds all nodes with in-degree 0 to the queue.
The main loop processes nodes in the queue, reducing in-degrees for their neighbors and
adding neighbors with reduced in-degrees of 0 to the queue.
The nodes are added to the result vector res in the order they are dequeued from the queue.
Main Program:
Read the integer n, which represents the number of nodes (tasks) in the graph.
Create a graph vector of vectors to represent the adjacency list of the graph.
Read the integer c, which represents the number of tasks dependent on this node.
Read the indices of the dependent nodes and store them in the corresponding
adjacency list of the current node.
Perform a breadth-first traversal from node 0 to iden fy reachable nodes (using the f vector)
and mark them as reachable.
Calculate the topological order of the graph using the topological_sort func on and store it in
vector t.
Ini alize an ans vector to store the tasks that are reachable and need to be performed.
Sort the tasks in descending order of their posi on in the topological order and store the
indices of tasks in the ans vector.
Print the tasks in the order specified in the problem statement (each task index + 1) with
spaces and a newline character.
Summary: The code performs a topological sort to determine the order in which tasks can be executed
based on their dependencies. It then sorts the reachable tasks in descending topological order and
outputs the result. This kind of algorithm is o.en used in scheduling problems where tasks have
dependencies on each other.
Code:
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
int n = graph.size();
vector<int> indegree(n);
vector<int> res;
queue<int> que;
while (!que.empty()) {
res.push_back(ver);
indegree[i]--;
if (indegree[i] == 0) que.push(i);
return res;
int main() {
int n;
cin >> n;
rep(i, n) {
int c;
cin >> c;
rep(_, c) {
int v;
cin >> v;
v--;
graph[i].push_back(v);
queue<int> que;
que.push(0);
vector<bool> f(n);
while (!que.empty()) {
if (!f[i]) {
f[i] = true;
que.push(i);
vector<int> t = topological_sort(graph);
vector<int> order(n);
rep(i, n) order[t[i]] = i;
vector<int> ans;
for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " \n"[i == ans.size() - 1];
}
F - Shortcuts /
Score : 500 points
Problem Statement
There is a race through checkpoints 1, 2, … , N in this order on a coordinate plane.
The coordinates of checkpoint i are (Xi , Yi ), and all checkpoints have different coordinates.
Let s be the total distance traveled (Euclidean distance) from checkpoint 1 to checkpoint N plus the
penalty.
Find the minimum achievable value as s.
Constraints
All input values are integers.
2 ≤ N ≤ 104
0 ≤ Xi , Yi ≤ 104
(Xi , Yi )
= (Xj , Yj ) if i =
j.
Input
The input is given from Standard Input in the following format:
N
X1 Y 1
X2 Y 2
⋮
XN Y N
Output
Print the answer. Your output is considered correct if the absolute or relative error from the true value is
at most 10−5 .
Sample Input 1 Copy
Copy
6
0 0
1 1
2 0
0 1
1 0
2 1
Copy
5.82842712474619009753
Copy
10
1 8
3 7
9 4
4 9
6 1
7 5
0 0
1 3
6 8
6 4
Copy
24.63441361516795872523
Copy
10
34 24
47 60
30 31
12 97
87 93
64 46
82 50
14 7
17 24
3 78
Copy
110.61238353245736230207
Algorithm:
Step 1: dist Func on This func on calculates the Euclidean distance between two points represented
by the pairs (x1, y1) and (x2, y2) using the formula sqrt((x1 - x2)^2 + (y1 - y2)^2).
Create a vector vp to store the coordinates of the points as pairs (x, y).
Create a set st to keep track of the points encountered (used to remove duplicates).
Read the coordinates of the point (x, y) and store it in the vector vp.
Create a 2D vector dp of size (n, 60) to store the dynamic programming table.
For each point vp[i], iterate over the previous points from index i-1 to 0 (inner loop).
Calculate the number of points skipped between vp[j] and vp[i] as skip = i - j
- 1.
For each possible skip count k, update the entry dp[i][k + skip] by considering
the minimum of the current value and the sum of dp[j][k] (cost of reaching
vp[j]) and the distance between vp[j] and vp[i].
The idea here is to calculate the cost of reaching vp[i] from all previous points
vp[j] with different skip counts k.
Ini alize res as a large value lg to store the minimum total cost.
Iterate over the possible skip counts (from 0 to 59) to calculate the minimum total cost for
each skip count:
For each skip count i, calculate a penalty value pen that depends on the skip count.
The penalty is 2^(i-1) if i is greater than 0 and 0 otherwise.
Update res with the minimum of the sum of dp[n-1][i] (cost of reaching the last point)
and the calculated penalty pen.
Code:
#include<bits/stdc++.h>
using pi=pair<int,int>;
int dx=a.first-b.first;
int dy=a.second-b.second;
int ss=dx*dx+dy*dy;
int main(){
int n;
cin >> n;
vector<pi> vp(n);
set<pi> st;
for(int i=0;i<n;i++){
st.insert(vp[i]);
dp[0][0]=0.0;
for(int i=1;i<n;i++){
for(int j=i-1;j>=0;j--){
int skip=i-j-1;
for(int k=0;(k+skip)<60;k++){
dp[i][k+skip]=min(dp[i][k+skip],dp[j][k]+dist(vp[j],vp[i]));
for(int i=0;i<60;i++){
if(i>0){
pen=pow(2.0,(long double)(i-1));
res=min(res,dp[n-1][i]+pen);
prinC("%.6Lf\n",res);
return 0;
}
G - Ai + Bj + Ck = X (1 <= i, j, k <= N) /
Score : 550 points
Problem Statement
You are given integers N , A, B, C, X . Find the number of triples of integers (i, j, k) that satisfy all of the
following conditions.
1 ≤ i, j, k ≤ N
Ai + Bj + Ck = X
Constraints
All input values are integers.
1 ≤ N ≤ 106
1 ≤ A, B, C ≤ 109
1 ≤ X ≤ 3 × 1015
Input
The input is given from Standard Input in the following format:
N A B C X
Output
Print the answer as an integer.
Copy
5 3 1 5 15
Copy
3
(1, 2, 2) : 3 × 1 + 1 × 2 + 5 × 2 = 15
(2, 4, 1) : 3 × 2 + 1 × 4 + 5 × 1 = 15
(3, 1, 1) : 3 × 3 + 1 × 1 + 5 × 1 = 15
Sample Input 2 Copy
Copy
1 1 1 1 1
Copy
0
Copy
100000 31415 92653 58979 1000000000
Copy
2896
Algorithm:
Step 1: Template Func on extgcd This part of the code defines an extended greatest common divisor
(extgcd) template func on. This func on computes the greatest common divisor of two numbers a
and b, and simultaneously finds coefficients x and y that sa sfy the equa on ax+by=extgcd(a,b).
Step 2: llceil and llfloor Func ons These func ons calculate the ceiling and floor division of two
integers a and b. They return the smallest integer x such that x×b is greater than or equal to a (llceil)
and the largest integer x such that x×b is less than or equal to a (llfloor).
Step 3: findseg Func on This func on takes a pair of integers represen ng a segment
(seg.first,seg.second)(seg.first,seg.second), an ini al value ini, and a step value step. It calculates a new
segment that represents the possible range of values for the variable a'er applying the step. The result
considers both the ceiling and floor divisions of the segment boundaries.
Main Program
It then adjusts the value of x by subtrac ng a+b+c. The purpose of this is to work with a
modified variable x in the following calcula ons.
If x becomes nega ve a'er the adjustment, the output is "0" since there can't be any valid
solu ons in that case.
The program ini alizes the variable res to count the number of valid triplets.
It calculates rx as x−a×i.
If rx becomes nega ve, it breaks the loop since further increasing i would only make
rx more nega ve.
The program checks whether rx is divisible by gbc (the common divisor of b and c).
If rx is not divisible by gbc, the program con nues to the next itera on.
If rx is divisible by gbc, the program uses the extgcd func on to calculate possible
values of j and k that sa sfy b×j+c×k=rx. The calculated values are then used to
determine valid ranges for j and k using the findseg func on.
The program calculates the intersec on of valid ranges for j and k to find the range of
values for which the triplet (i,j,k) is valid.
If the intersec on range is non-empty (i.e., fr−fl+1>0), the count fr−fl+1 is added to
res.
Summary: The code solves a specific problem involving modular linear equa ons and coun ng valid
triplets that sa sfy certain condi ons. It employs mathema cal concepts like modular arithme c,
greatest common divisors, and linear Diophan ne equa ons. The approach is based on itera ng over
one variable and calcula ng valid ranges for the other two variables to count the solu ons.
Code:
#include<bits/stdc++.h>
// h7ps://math.stackexchange.com/ques ons/670405/does-the-extended-euclidean-algorithm-
always-return-the-smallest-coefficients-of
// h7ps://teratail.com/ques ons/176282
// h7ps://ei1333.github.io/luzhiled/snippets/math/extgcd.html
T d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
return d;
if(a%b==0){return a/b;}
if(a>=0){return (a/b)+1;}
else{return -((-a)/b);}
if(a>=0){return (a/b);}
else{return -((-a)/b)-1;}
if(step>0){
else{
step*=-1;
int main(){
x -= (a+b+c);
if(rx<0){break;}
// b*j + c*k = rx
if(rx%gbc){con nue;}
extgcd(b,c,j,k);
j*=((rx%b)/gbc);
k*=((rx%b)/gbc);
j+=jadd;
pl sj=findseg({0,n-1},j,stepj);
pl sk=findseg({0,n-1},k,stepk);
res+=max(0ll,fr-fl+1);
return 0;
}
Ex - Typical Convolution Problem /
Score : 650 points
Problem Statement
You are given a sequence (A1 , A2 , … , AN ). Let us define a sequence (F0 , F1 , … , FN ) by the
following formulae.
F0 = 1
Fn = An ∑ Fi Fj (1 ≤ n ≤ N )
i+j<n
Find F1 , … , FN modulo 998244353.
Constraints
1 ≤ N ≤ 2 × 105
0 ≤ Ai < 998244353
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print F1 , … , FN modulo 998244353 in this order, with spaces in between.
Copy
5
1 2 3 4 5
Copy
1 6 48 496 6240
F1 = A1 F0 F0 = 1. F2 = A2 (F0 F0 + F0 F1 + F1 F0 ) = 6. Similarly, we find F3 = 48, F4 = 496, F5 =
6240.
Copy
3
12345 678901 2345678
Copy
12345 790834943 85679169
Algorithm:
The provided problem seems to be an implementa on of polynomial mul plica on using the Fast
Fourier Transform (FFT) technique in a prime field modulo p=998244353. The algorithm is designed to
mul ply two polynomials f and g of arbitrary degree efficiently by u lizing the FFT algorithm for
polynomial mul plica on.
1. Ini aliza on: Ini alize the prime p=998244353, generator g=3, and its inverse ig=332748118.
Compute W and iW arrays to store powers of g and ig, respec vely.
2. FFT and IFFT Func ons: The ‘ ’ and ‘i ’ func ons implement the FFT and inverse FFT
algorithms, respec vely. These func ons perform the Fast Fourier Transform and its inverse
on the input array f.
3. Convolu on Func on: The convolve func on performs polynomial convolu on using FFT. If
the lengths of the input polynomials a and b are smaller than a threshold value (50 in this
case), it performs a direct polynomial mul plica on using nested loops. Otherwise, it uses FFT
to compute the convolu on efficiently.
4. Relaxed Mul plica on Class: The RelaxedMul plica on class encapsulates the process of
mul plying two polynomials in a relaxed manner. The class maintains arrays for f, g, and h,
represen ng the input polynomials and their product, respec vely. The append method
appends a new polynomial to the arrays and calculates the updated product polynomial. The
calc method calculates the convolu on between segments of f and g and accumulates the
result in h.
5. Main Program: The main program reads an integer n followed by n integers represen ng the
coefficients of polynomial f. It ini alizes a RelaxedMul plica on instance and computes arrays
F and G using the algorithm. The computed array F represents the coefficients of the
polynomial f mul plied by an auxiliary polynomial G. The result F is then printed.
This algorithm is quite advanced and involves concepts from both number theory (modular arithme c
and prime fields) and signal processing (FFT). It's used for efficient mul plica on of
polynomials in certain situa ons, par cularly in the context of computer algebra systems and
certain mathema cal algorithms. Keep in mind that this code assumes a deep understanding
of the FFT algorithm and modular arithme c. If you're planning to use or modify this code,
ensure that you thoroughly understand these concepts.
Ini alize the prime p=998244353, which is used as the modulo for arithme c opera ons.
Compute arrays W and iW to store powers of g and ig respec vely, using bitwise right shi7
(division by 2) and modulo p opera ons.
Loop over different levels of FFT stages, denoted by l ranging from k down to 1.
Calculate d, the distance between elements in each bu9erfly group for the current
stage.
Calculate the powers of W required for the current stage and store them in the array
U.
Iterate over the bu9erfly groups and update the elements of the array f using FFT
formulas involving W.
Implement the i func on that takes an array f and performs the Inverse Fast Fourier
Transform on it.
Similar to the func on, loop over different levels of IFFT stages.
Calculate the powers of iW required for the current stage and store them in the array
U.
Iterate over the bu9erfly groups and update the elements of the array f using IFFT
formulas involving iW.
Implement the convolve func on that calculates the convolu on of two input arrays a and b
using FFT.
If either a or b is small (e.g., both have lengths less than 50), perform a direct
convolu on using nested loops.
Pad the input arrays a and b with zeros to a length of n, where n=2k.
Apply the IFFT to the result and adjust for the scaling factor by mul plying with
pow(n,p−2,p).
Implement the RelaxedMul plica on class that handles the mul plica on of polynomials in
a relaxed manner.
The class maintains arrays f, g, and h to store the input polynomials and the product
polynomial.
The append method adds a new polynomial to the arrays and calculates the updated product
polynomial h using the calc method.
The calc method calculates the convolu on between segments of f and g and accumulates the
result in the array h.
The RelaxedMul plica on class is designed to handle polynomial mul plica on efficiently by
exploi ng the proper es of FFT.
Ini alize arrays F and G with the first element 1, which will be used to store intermediate
results.
Loop through the coefficients of polynomial f and perform the following for each coefficient
ai:
Summary: This algorithm efficiently mul plies polynomials using FFT techniques and takes advantage
of the relaxed mul plica on proper es. The code's structure revolves around FFT for
polynomial mul plica on and maintains auxiliary arrays to track intermediate results. It's
important to have a solid understanding of FFT, modular arithme c, and polynomial
opera ons to comprehend and use this algorithm effec vely.
Code:
P = 998244353
p, g, ig = 998244353, 3, 332748118
def K(f):
d = 1 << l - 1
U = [1]
for i in range(d):
U.append(U[-1] * W[l] % p)
for j in range(d):
s=i*2*d+j
t=s+d
def iK(f):
d = 1 << l - 1
U = [1]
for i in range(d):
U.append(U[-1] * iW[l] % p)
for j in range(d):
s=i*2*d+j
t=s+d
n0 = len(a) + len(b) - 1
ret = [0] * n0
for i, aa in enumerate(a):
for j, bb in enumerate(b):
return ret
k = (n0).bit_length()
n = 1 << k
a = a + [0] * (n - len(a))
b = b + [0] * (n - len(b))
K(a), K(b)
for i in range(n):
iK(a)
invn = pow(n, p - 2, p)
for i in range(n0):
del a[n0:]
return a
#h=f*g
def __init__(self):
self.f = []
self.g = []
self.h = []
self.n = 0
self.h[i] = (self.h[i] + a) % p
self.f.append(a)
self.g.append(b)
self.n += 1
n = self.n
m = (n + 1) & -(n + 1)
s=0
if m <= n:
a=1
while a <= m:
self.calc(n - a, n, s, s + a)
self.calc(s, s + a, n - a, n)
s += a
a <<= 1
else:
a=1
self.calc(n - a, n, s, s + a)
self.calc(s, s + a, n - a, n)
s += a
a <<= 1
self.calc(n - a, n, s, s + a)
return self.h[n-1]
n = input()
a = list(map(int, input().split()))
F = [1]
G = [rel_mul.append(1, 1)]
for ai in a:
F.append(ai * G[-1] % P)