Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 以上 N 以下の整数からなる長さ N の数列 a = (a_1, \dots, a_N) が与えられます。
以下の条件を全て満たす整数 i, j の組の総数を求めてください。
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
制約
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 1 3 2 4
出力例 1
2
(i, j) = (1, 4), (2, 3) が条件を満たします。
入力例 2
10 5 8 2 2 1 6 7 2 9 10
出力例 2
8
Score : 300 points
Problem Statement
You are given a sequence a = (a_1, \dots, a_N) of length N consisting of integers between 1 and N.
Find the number of pairs of integers i, j that satisfy all of the following conditions:
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
Constraints
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 1 3 2 4
Sample Output 1
2
(i, j) = (1, 4), (2, 3) satisfy the conditions.
Sample Input 2
10 5 8 2 2 1 6 7 2 9 10
Sample Output 2
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数からなる数列 A=(A_1,\ldots,A_N),B=(B_1,\ldots,B_N) が与えられます。
以下の条件を全て満たす長さ N の数列 X=(X_1,\ldots,X_N) が存在するかを判定してください。
-
すべての i(1\leq i\leq N) について、X_i = A_i または X_i = B_i
-
すべての i(1\leq i\leq N-1) について、|X_i - X_{i+1}| \leq K
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq K \leq 10^9
- 1 \leq A_i,B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 \ldots A_N B_1 \ldots B_N
出力
条件を全て満たす X が存在するならば Yes と、存在しないならば No と出力せよ。
入力例 1
5 4 9 8 3 7 2 1 6 2 9 5
出力例 1
Yes
X=(9,6,3,7,5) が全ての条件を満たします。
入力例 2
4 90 1 1 1 100 1 2 3 100
出力例 2
No
条件を満たす X は存在しません。
入力例 3
4 1000000000 1 1 1000000000 1000000000 1 1000000000 1 1000000000
出力例 3
Yes
Score : 300 points
Problem Statement
You are given two sequences, each of length N, consisting of integers: A=(A_1, \ldots, A_N) and B=(B_1, \ldots, B_N).
Determine whether there is a sequence of length N, X=(X_1, \ldots, X_N), satisfying all of the conditions below.
-
X_i = A_i or X_i = B_i, for every i(1\leq i\leq N).
-
|X_i - X_{i+1}| \leq K, for every i(1\leq i\leq N-1).
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq K \leq 10^9
- 1 \leq A_i,B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 \ldots A_N B_1 \ldots B_N
Output
If there is an X that satisfies all of the conditions, print Yes; otherwise, print No.
Sample Input 1
5 4 9 8 3 7 2 1 6 2 9 5
Sample Output 1
Yes
X=(9,6,3,7,5) satisfies all conditions.
Sample Input 2
4 90 1 1 1 100 1 2 3 100
Sample Output 2
No
No X satisfies all conditions.
Sample Input 3
4 1000000000 1 1 1000000000 1000000000 1 1000000000 1 1000000000
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
2 以上の整数 K が与えられます。
正の整数 N であって、N! が K の倍数となるようなもののうち最小のものを求めてください。
ただし、N! は N の階乗を表し、問題の制約下で、そのような N が必ず存在することが証明できます。
制約
- 2\leq K\leq 10^{12}
- K は整数
入力
入力は以下の形式で標準入力から与えられる。
K
出力
N! が K の倍数となるような最小の正整数 N を出力せよ。
入力例 1
30
出力例 1
5
- 1!=1
- 2!=2\times 1=2
- 3!=3\times 2\times 1=6
- 4!=4\times 3\times 2\times 1=24
- 5!=5\times 4\times 3\times 2\times 1=120
より、N! が 30 の倍数となる最小の正整数 N は 5 です。よって、5 を出力します。
入力例 2
123456789011
出力例 2
123456789011
入力例 3
280
出力例 3
7
Score : 400 points
Problem Statement
You are given an integer K greater than or equal to 2.
Find the minimum positive integer N such that N! is a multiple of K.
Here, N! denotes the factorial of N. Under the Constraints of this problem, we can prove that such an N always exists.
Constraints
- 2\leq K\leq 10^{12}
- K is an integer.
Input
The input is given from Standard Input in the following format:
K
Output
Print the minimum positive integer N such that N! is a multiple of K.
Sample Input 1
30
Sample Output 1
5
- 1!=1
- 2!=2\times 1=2
- 3!=3\times 2\times 1=6
- 4!=4\times 3\times 2\times 1=24
- 5!=5\times 4\times 3\times 2\times 1=120
Therefore, 5 is the minimum positive integer N such that N! is a multiple of 30. Thus, 5 should be printed.
Sample Input 2
123456789011
Sample Output 2
123456789011
Sample Input 3
280
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。また、f(l,r) を以下で定義します。
- (A_l,A_{l+1},\ldots,A_{r-1},A_{r}) に含まれる値の種類数
次の式の値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq N
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 1 2 2
出力例 1
8
f(1,2) について考えます。(A_1,A_2)=(1,2) に含まれる値の種類数は 2 なので f(1,2)=2 です。
f(2,3) について考えます。(A_2,A_3)=(2,2) に含まれる値の種類数は 1 なので f(2,3)=1 です。
f の総和は 8 となります。
入力例 2
9 5 4 2 2 3 2 4 4 1
出力例 2
111
Score : 475 points
Problem Statement
You are given a sequence of integers A = (A_1, A_2, \ldots, A_N) of length N. Define f(l, r) as:
- the number of distinct values in the subsequence (A_l, A_{l+1}, \ldots, A_r).
Evaluate the following expression:
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 1 2 2
Sample Output 1
8
Consider f(1,2). The subsequence (A_1, A_2) = (1,2) contains 2 distinct values, so f(1,2)=2.
Consider f(2,3). The subsequence (A_2, A_3) = (2,2) contains 1 distinct value, so f(2,3)=1.
The sum of f is 8.
Sample Input 2
9 5 4 2 2 3 2 4 4 1
Sample Output 2
111
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
(1,2, \dots, N) を並び替えた長さ N の順列 P = (p_1, p_2, \dots, p_N) に対して、 P のスコア S(P) を次のように定めます。
- N 人の人とすぬけ君がいて、N 人の人には 1,2,\dots,N の番号がついています。はじめ、人 i (1 \leq i \leq N) はボール i を持っています。
すぬけ君が叫ぶたびに、i \neq p_i であるようなすべての人 i は人 p_i に持っているボールを同時に渡します。
すぬけ君は、1 回以上叫んだ後にすべての人 i がボール i を持っている状態になると叫ぶのをやめます。
すぬけ君が叫ぶのをやめるまでに叫んだ回数が順列のスコアとなります。ここでスコアは有限の値を取ることが保証されます。
P としてあり得るものは N! 通りありますが、それらのスコアを K 乗した値の総和を 998244353 で割ったあまりを計算してください。
-
厳密に言い換えると、(1,2, \dots, N) を並び替えた長さ N の順列全体の集合を S_N として
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}
を計算してください。
制約
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353} を出力せよ。
入力例 1
2 2
出力例 1
5
N = 2 のとき P としてあり得る順列は (1,2),(2,1) の 2 つです。
順列 (1,2) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 1 となります。
順列 (2,1) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 2 を、人 2 はボール 1 を持っています。
すぬけ君が 2 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 2 となります。
よって 1^2 + 2^2 = 5 がこの問題の答えになります。
入力例 2
3 3
出力例 2
79
すべての順列とスコアの組を列挙すると以下のようになります。
- 順列 : (1,2,3), スコア : 1
- 順列 : (1,3,2), スコア : 2
- 順列 : (2,1,3), スコア : 2
- 順列 : (2,3,1), スコア : 3
- 順列 : (3,1,2), スコア : 3
- 順列 : (3,2,1), スコア : 2
よって 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79 を出力します。
入力例 3
50 10000
出力例 3
77436607
Score : 500 points
Problem Statement
For a permutation P = (p_1, p_2, \dots, p_N) of (1,2, \dots, N), let us define the score S(P) of P as follows.
- There are N people, numbered 1,2,\dots,N. Additionally, Snuke is there. Initially, Person i (1 \leq i \leq N) has Ball i.
Each time Snuke screams, every Person i such that i \neq p_i gives their ball to Person p_i simultaneously.
If, after screaming at least once, every Person i has Ball i, Snuke stops screaming.
The score is the number of times Snuke screams until he stops. Here, it is guaranteed that the score will be a finite value.
There are N! permutations P of (1,2, \dots, N). Find the sum, modulo 998244353, of the scores of those permutations, each raised to the K-th power.
- Formally, let S_N be the set of the permutations of (1,2, \dots, N). Compute the following: \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Constraints
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Sample Input 1
2 2
Sample Output 1
5
When N = 2, there are two possible permutations P: (1,2),(2,1).
The score of the permutation (1,2) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 1.
The score of the permutation (2,1) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 2, and Person 2 has Ball 1.
After Snuke's second scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 2.
Therefore, the answer in this case is 1^2 + 2^2 = 5.
Sample Input 2
3 3
Sample Output 2
79
All permutations and their scores are listed below.
- (1,2,3): The score is 1.
- (1,3,2): The score is 2.
- (2,1,3): The score is 2.
- (2,3,1): The score is 3.
- (3,1,2): The score is 3.
- (3,2,1): The score is 2.
Thus, we should print 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79.
Sample Input 3
50 10000
Sample Output 3
77436607