版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

仓库源文站点原文


title: 题解 - Japanese Student Championship 2021 categories:


比赛链接

<!-- more -->

题目概览

题号1 标题 做法
A Competition 签到
B Xor of Sequences 签到
C Max GCD 2 签到, 枚举
D Nowhere $P$ 签到, 找规律
*E Level K Palindrome
*F Max Matrix 线段树 / 平衡树
G Spanning Tree 并查集 + 矩阵树定理
*H Shipping

官方题解

A - Competition

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 100 points

Problem Statement

A Supermarket Takahashi sells an $X$-gram beef pack for $Y$ yen

Another Supermarket Snuke has decided to sell a beef pack at a lower price per gram

In Snuke, one beef pack weighs $Z$ grams. What is the greatest possible price (a non-negative integer) for Snuke's beef pack such that it is strictly cheaper than Takahashi's beef pack per gram?

Constraints

Input

Input is given from Standard Input in the following format:

$X\ Y\ Z$

Output

Print the answer

Sample Input 1

100 200 100

Sample Output 1

199

Both stores sell $100$-gram packs, so Snuke can just make it one yen cheaper than that in Takahashi

Sample Input 2

103 971 593

Sample Output 2

5590

Takahashi sells beef for $\frac{971}{103}=9.4271...$ yen per gram. Snuke can sell $593$ grams of beef for $5590$ yen to make it $\frac{5590}{593}=9.4266...$ yen per gram

Sample Input 3

1000 1 1

Sample Output 3

0

The price is allowed to be $0$ yen

解题思路

$\lfloor\frac{yz}{x}\rfloor-[x\mid yz]$

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:AtCoder_jsc2021_a AtCoder/jsc2021_a/0.cpp %} </details>

B - Xor of Sequences

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 200 points

Problem Statement

We have two strictly increasing integer sequences $A=(A_1,A_2,...,A_N)$ and $B=(B_1,B_2,...,B_M)$

Find all integers that appear in exactly one of $A$ and $B$ and print them in ascending order

Constraints

Input

Input is given from Standard Input in the following format:

$N\ M$

$A_1\ A_2\ ...\ A_N$

$B_1\ B_2\ ...\ B_M$

Output

Print all integers that appear in exactly one of $A$ and $B$

in ascending order, with space as separator

Sample Input 1

2 2
1 2
1 3

Sample Output 1

2 3

$1$ is contained in both $A$ and$B$

$2$ is contained in only $A$

$3$ is contained in only $B$

Thus, we should print $2$ and $3$

Sample Input 2

4 4
1 2 3 4
1 2 3 4

Sample Output 2


You can print an empty line or nothing

题意简述

给两串严格递增的序列, 求这两个序列的对称差

解题思路

随便做

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:AtCoder_jsc2021_b AtCoder/jsc2021_b/0.cpp %} </details>

C - Max GCD 2

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 300 points

Problem Statement

Given are integers $A$ and $B$. Find the maximum possible value of $\gcd(x,y)$ when we choose integers x and y so that $A\leqslant x<y\leqslant B$

Here, $\gcd(x,y)$ denotes the greatest common divisor of $x$ and $y$

Constraints

Input

Input is given from Standard Input in the following format:

$A\ B$

Output

Print the answer

Sample Input 1

2 4

Sample Output 1

2

We have three ways to choose $(x,y)$ such that $A\leqslant x<y\leqslant B$: $(2,3),(2,4),(3,4)$, where the greatest common divisors are $1,2,1$, respectively, so the maximum possible value is $2$

Sample Input 2

199999 200000

Sample Output 2

1

We have $\gcd(199999,200000)=1$

Sample Input 3

101 139

Sample Output 3

34

题意简述

给定 $A,B$, 求 $\displaystyle\max_{A\leqslant x<y\leqslant B}(x,y)$

解题思路

直接暴力枚举即可

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:AtCoder_jsc2021_c AtCoder/jsc2021_c/0.cpp %} </details>

D - Nowhere P

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 400 points

Problem Statement

You are given a prime number $P$ not less than $2$, which you don't like

Let's call an array of integers $A_1,A_2,...,A_N$ very good if it satisfies the following condition:

Consider all $(P-1)^N$ arrays of length $N$ with elements from $1$ to $P-1$. How many of them are very good?

As this number can be very big, output it modulo $10^9+7$

Constraints

Input

Input is given from Standard Input in the following format:

$N\ P$

Output

Print the count modulo $10^9+7$

Sample Input 1

3 3

Sample Output 1

2

Two arrays, $(1,1,2)$ and $(2,2,1)$, satisfy the condition

Sample Input 2

3 2

Sample Output 2

0

Sample Input 3

45108 2571593

Sample Output 3

224219544

题意简述

给定质数 $P$, 求有多少序列 $(A_1,A_2,\dots,A_N)$ 满足:

$$ \forall i\in[1,n]{\mathbb{N}},~\sum{j=1}^iA_j{\equiv}\llap{/\,}0\pmod P $$

解题思路

显然, 当 $n=1$ 时答案为 $p-1$, 对应合法序列即为 $(1),(2),...,(p-1)$

之后在这些合法序列后插入新数时, 每个序列都有且仅有一个数使得这个数插入后该序列非法 (该数即为 $(-\sum_{i}a_i)\bmod p$)

故答案为 $(p-1)(p-2)^{n-1}$

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:AtCoder_jsc2021_d AtCoder/jsc2021_d/0.cpp %} </details>

E - Level K Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 500 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give Snuke a level-$K$ palindrome. A level-$L$ palindrome, where $L$ is a non-negative integer, is defined as follows:

Now, Takahashi has a string $S$. Determine whether it is possible to make $S$ an exactly level-$K$ palindrome by doing the following action zero or more times: choose a character in $S$ and change it to another lowercase English letter. If it is possible, find the minimum number of changes needed to make $S$ a level-$K$ palindrome

Constraints

Input

Input is given from Standard Input in the following format:

$K\ S$

Output

If it is possible to get an exactly level-$K$ palindrome, print the minimum number of changes needed. If it is impossible, print impossible

Sample Input 1

4
aabaaaabaa

Sample Output 1

0

We can find the level of aabaaaabaa as follows:

Thus, aabaaaabaa is already a level-$4$ palindrome and needs no changes

Sample Input 2

2
aabaaaabaa

Sample Output 2

4

We can, for example, change aabaaaabaa to acbcaacbca to get a level-$2$ palindrome

Note that aabaaaabaa is not a level-$2$ palindrome

Sample Input 3

3
aabaaaabaa

Sample Output 3

impossible

Sample Input 4

5
aabaaaabaa

Sample Output 4

impossible

Sample Input 5

2
acaabcbababaaac

Sample Output 5

6

题意简述

本题所有的字符串均指只由小写英文字母构成的字符串

对字符串 $s$,

定义 $k$ 阶回文串如下:

给一字符串 $s$, 问至少经几次变换可使其恰好为 $k$ 阶回文串

解题思路

显然, 若有解则 $k$ 不可能过大

复杂度

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> </details>

F - Max Matrix

Time Limit: 3 sec / Memory Limit: 1024 MB

Score: 600 points

Problem Statement

We have a sequence a of length $N$ and a sequence $b$ of length $M$. Initially, every element in $a$ and $b$ is $0$

You are asked to process $Q$ queries. In the $i$-th query, given three integers $T_i$, $X_i$, and $Y_i$, do the following:

Then, after processing each query, print the value $\displaystyle\sum{i=1}^N\sum{j=1}^M\max(a_i,b_j)$

Constraints

Input

Input is given from Standard Input in the following format:

$N\ M\ Q$

$T_1\ X_1\ Y_1$

$T_2\ X_2\ Y_2$

$T_3\ X_3\ Y_3$

$\vdots$

$T_Q\ X_Q\ Y_Q$

Output

Print $Q$ integers as instructed in Problem Statement, with newline as separator

Sample Input 1

2 2 4
1 1 10
2 1 20
2 2 5
1 1 30

Sample Output 1

20
50
55
85

If we write $\max(a_i,b_j)$ at the $i$-th row and $j$-th column in a grid, the four queries will change it as follows:

Sample Input 2

3 3 5
1 3 10
2 1 7
1 3 5
2 2 10
2 1 1

Sample Output 2

30
44
31
56
42

Sample Input 3

200000 200000 4
2 112219 100000000
1 73821 100000000
2 26402 100000000
1 73821 100000000

Sample Output 3

20000000000000
39999900000000
59999800000000
59999800000000

The integers to be printed may not fit into a $32$-bit integer type

题意简述

有一个长为 $n$ 的全零序列 $a$ 和长为 $m$ 的全零序列 $b$, 对其做如下操作

这两种操作一共进行 $Q$ 次, 要求每次操作后都要输出 $\displaystyle\sum{i=1}^n\sum{j=1}^M\max{a_i,b_j}$

解题思路

复杂度

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> </details>

G - Spanning Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 600 points

Problem Statement

We have a graph with $N$ vertices numbered $1,2,...,N$. Initially, it has no edges

Now, let us add some number of undirected edges to $G$ so that the following condition holds for any $i,j(i\ne j)$

after addition

Among the graphs that can be $G$ after addition, how many are trees?

Since the count can be enormous, find it modulo $10^9+7$

Constraints

Input

Input is given from Standard Input in the following format:

$N$

$A{1,1}\ ...\ A{1,N}$

$\vdots$

$A{N,1}\ ...\ A{N,N}$

Output

Print the count modulo $10^9+7$

Sample Input 1

4
0 1 -1 0
1 0 -1 -1
-1 -1 0 0
0 -1 0 0

Sample Output 1

2

We need an edge between Vertex $1$ and Vertex $2$, and we must not add an edge between Vertex $1$ and Vertex $4$ or between Vertex $3$ and Vertex $4$

Thus, we have the following two valid graphs:

Sample Input 2

3
0 1 1
1 0 1
1 1 0

Sample Output 2

0

Sample Input 3

3
0 0 0
0 0 0
0 0 0

Sample Output 3

0

Sample Input 4

11
0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0

Sample Output 4

357947677

When we distinguish the vertices, there are $11^9$ trees with $11$ vertices

题意简述

有 $n$ 个点, 考虑以这 $n$ 个点为顶点, 满足如下条件的所有图:

求这些图中树的个数

解题思路

首先, 考虑所以已经存在的边构成的图, 如果有环了, 则答案一定为 $0$, 否则森林中的每个树都可缩成一个点, 之后用矩阵树定理即可

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:AtCoder_jsc2021_g AtCoder/jsc2021_g/0.cpp %} </details>

H - Shipping

Time Limit: 5 sec / Memory Limit: 1024 MB

Score: 600 points

Problem Statement

In the Republic of AtCoder, there are $N$ cities called City $1$ through City $N$ and $N$ canals called Canal $1$ through Canal $N$

Canal $i$ connects City $i$ and City $A_i$ bidirectionally, and you have to pay the toll of $C_i$

yen to go through it, but after paying the toll once, you can use it any number of times in any direction

It is guaranteed that you can reach from any city to any city using some canals

You are asked to deliver $M$ cargoes in this country. The $i$-th cargo should be delivered from City $X_i$ to City $Y_i$

There is no way other than using the canals to deliver the cargoes, but you yourself can travel between the cities freely without using the canals

Find the minimum total toll you have to pay to deliver all $M$ cargoes

Constraints

Input

Input is given from Standard Input in the following format:

$N\ M$

$A_1\ C_1$

$A_2\ C_2$

$A_3\ C_3$

$\vdots$

$A_N\ C_N$

$X_1\ Y_1$

$X_2\ Y_2$

$X_3\ Y_3$

$\vdots$

$X_M\ Y_M$

Output

Print the minimum total toll you have to pay, as an integer

Sample Input 1

4 2
3 3
1 7
2 5
1 2
4 3
2 1

Sample Output 1

10

The figure below shows the cities and canals in this country, where numbers along the lines representing canals represent the tolls:

You can deliver the cargoes as follows to make the total toll $10$ yen:

Sample Input 2

5 2
2 2
5 5
5 7
2 4
3 10
3 5
4 1

Sample Output 2

13

Multiple canals may connect the same pair of cities

Sample Input 3

11 4
8 1
9 9
8 10
8 3
1 2
11 3
9 2
6 5
3 4
1 7
3 2
7 8
10 1
4 9
11 6

Sample Output 3

26

题意简述

给一个带权无向图, 求满足如下条件的子图的最小边权和

解题思路

复杂度

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> </details>

  1. 打*的是还没写的题