Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 個の荷物 1,2,\dots,N があります。
荷物 i は重さが w_i で、価値が v_i です。
i=2,3,\dots,N について、以下が成り立っています。
- \sum_{j=1}^{i-1}{w_j}<w_i
重さの合計が W 以下になるようにいくつか(0 個でもよい)の荷物を選びます。
選んだ荷物の価値の合計としてあり得る値の最大値を求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 60
- 1 \leq w_i \leq W \leq 10^{18}
- \sum_{j=1}^{i-1}{w_j}<w_i \; (2 \leq i \leq N)
- 1 \leq v_i \leq 10^{16}
- すべてのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
3 3 10 1 2 3 1 9 3 3 20 1 2 3 1 9 3 4 15 1 1 3 2 5 3 10 4
出力例 1
5 6 7
1 つ目のテストケースでは、荷物 1,3 の 2 つを選ぶと重さの合計は 10 であり、価値の合計は 5 です。
また、条件を満たす範囲で価値の合計を 6 以上にできません。
Score : 400 points
Problem Statement
There are N items 1, 2, \dots, N.
Item i has weight w_i and value v_i.
For i = 2, 3, \dots, N, the following holds:
- \sum_{j=1}^{i-1}{w_j} < w_i
Choose some (possibly zero) items such that the total weight is at most W.
Find the maximum possible total value of the chosen items.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 60
- 1 \leq w_i \leq W \leq 10^{18}
- \sum_{j=1}^{i-1}{w_j} < w_i \; (2 \leq i \leq N)
- 1 \leq v_i \leq 10^{16}
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
Output
Output the answers over a total of T lines. The t-th line should contain the answer for the t-th test case.
Sample Input 1
3 3 10 1 2 3 1 9 3 3 20 1 2 3 1 9 3 4 15 1 1 3 2 5 3 10 4
Sample Output 1
5 6 7
For the first test case, choosing items 1 and 3 gives a total weight of 10 and a total value of 5.
Moreover, it is impossible to achieve a total value of 6 or more while satisfying the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A と正整数 K が与えられます。
A に対して、以下の操作を何回でも行うことができます。
- A_i+A_{i+1} が K で割り切れるような 1 以上 N-1 以下の整数 i を選び、A_i と A_{i+1} を入れ替える。
操作を 0 回以上行った後の A としてあり得る数列の総数を 998244353 で割った余りを求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 1 \leq A_i \leq 10^9
- すべてのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
N K A_1 A_2 \dots A_N
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
3 4 3 1 2 4 7 6 4 1 5 3 6 2 4 3 2 2 2 3
出力例 1
4 6 1
1 つ目のテストケースでは、操作を 0 回以上行った後の A としてあり得る数列は (1,2,4,7),(1,4,2,7),(1,4,7,2),(2,1,4,7) の 4 つです。
Score : 500 points
Problem Statement
You are given an integer sequence A of length N and a positive integer K.
You can perform the following operation on A any number of times:
- Choose an integer i with 1 \leq i \leq N-1 such that A_i + A_{i+1} is divisible by K, and swap A_i and A_{i+1}.
Find the total number, modulo 998244353, of distinct sequences that A can become after performing the operation zero or more times.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 1 \leq A_i \leq 10^9
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
N K A_1 A_2 \dots A_N
Output
Output the answers over a total of T lines. The t-th line should contain the answer for the t-th test case.
Sample Input 1
3 4 3 1 2 4 7 6 4 1 5 3 6 2 4 3 2 2 2 3
Sample Output 1
4 6 1
For the first test case, the sequences that A can become after performing the operation zero or more times are these four: (1,2,4,7),(1,4,2,7),(1,4,7,2),(2,1,4,7).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の正整数からなる数列 A が与えられます。
\prod_{1 \leq i < j \leq N}|A_i-A_j| を N で割った余りを求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- すべてのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
N A_1 A_2 \ldots A_N
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
3 3 1 9 5 3 2 2 3 5 11 33 22 55 44
出力例 1
2 0 3
1 つ目のテストケースについて、|A_1-A_2| \times |A_1-A_3| \times |A_2-A_3|=8 \times 4 \times 4=128 であり、これを 3 で割った余りは 2 です。
Score : 500 points
Problem Statement
You are given a sequence A of N positive integers.
Find the remainder when \prod_{1 \leq i < j \leq N}|A_i-A_j| is divided by N.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answers over a total of T lines. The t-th line should contain the answer for the t-th test case.
Sample Input 1
3 3 1 9 5 3 2 2 3 5 11 33 22 55 44
Sample Output 1
2 0 3
For the first test case, |A_1-A_2| \times |A_1-A_3| \times |A_2-A_3|=8 \times 4 \times 4=128, and the remainder when this is divided by 3 is 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
ドローカード A 枚と普通のカード B 枚があります。 これら A+B 枚のカードからなる順列は (A+B)! 通りありますが、そのうちの 1 つを一様ランダムに選び、その順番に上下に積み重ねて山札とします。 そして山札の上から K 枚を引き、手札にします。 手札にドローカードが 1 枚以上ある限り、以下の操作を繰り返します。
- 手札にあるドローカードを 1 枚捨てる。捨てたカードは手札からなくなり、山札にも戻らない。
- 山札にあるカードの枚数を c として、山札の上から \min(c,2) 枚のカードを手札に加える。
最終的な手札のカード枚数の期待値を \text{mod } 998244353 で求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
\text{mod } {998244353} の定義
この問題の制約のもとでは、求める答えは有理数であることが証明でき、さらにこれを既約分数 \frac{P}{Q} で表した時、Q {{}\not\equiv{}} 0 \pmod{998244353} となることが証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。
制約
- 1 \leq T \leq 10^5
- 1 \leq A,B
- 1 \leq K \leq A+B \leq 10^7
- すべてのテストケースにおける A+B の総和は 10^7 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
A B K
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
4 2 2 3 1 2 1 10 20 4 20 10 4
出力例 1
2 332748119 944679050 774829082
1 つ目のテストケースでは、最初の手札にドローカードが必ず 1 枚以上あり、山札にあるカードの枚数は 1 枚です。
操作を 1 回行うと手札にはドローカードが 1 枚と普通のカードが 2 枚あることになります。
もう一度操作を行って手札にはドローカードが 0 枚と普通のカードが 2 枚ある状態になります。
2 つ目のテストケースでは、最初に引くカードがドローカードと普通のカードの場合で最終的な手札の枚数はそれぞれ 2,1 枚となります。期待値は \frac{1}{3} \times 2 + \frac{2}{3} \times 1 = \frac{4}{3} です。
Score : 600 points
Problem Statement
There are A "draw" cards and B normal cards. Among the (A+B)! permutations of these A+B cards, one is chosen uniformly at random, and the cards are stacked top to bottom in that order to form a deck. Then, K cards are drawn from the top of the deck and placed in your hand. As long as your hand contains at least one draw card, repeat the following operation:
- Discard one draw card from your hand. The discarded card is removed from your hand and does not return to the deck.
- Let c be the number of cards remaining in the deck. Add \min(c,2) cards from the top of the deck to your hand.
Find the expected number, modulo 998244353, of cards in your final hand.
Solve T test cases per input.
Definition of modulo 998244353
Under the constraints of this problem, it can be proved that the answer is a rational number, and that when it is expressed as an irreducible fraction \frac{P}{Q}, it satisfies Q {{}\not\equiv{}} 0 \pmod{998244353}. Thus, the integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 is uniquely determined. Output this R.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq A, B
- 1 \leq K \leq A+B \leq 10^7
- The sum of A+B over all test cases is at most 10^7.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
A B K
Output
Output the answers over a total of T lines. The t-th line should contain the answer for the t-th test case.
Sample Input 1
4 2 2 3 1 2 1 10 20 4 20 10 4
Sample Output 1
2 332748119 944679050 774829082
For the first test case, the initial hand always contains at least 1 draw card, and the deck has 1 card remaining.
After performing the operation once, the hand contains 1 draw card and 2 normal cards.
After performing the operation once more, the hand contains 0 draw cards and 2 normal cards.
For the second test case, the final hand size is 2 if the first drawn card is a draw card, and 1 if it is a normal card. The expected value is \frac{1}{3} \times 2 + \frac{2}{3} \times 1 = \frac{4}{3}.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
0 と 1 からなる長さ 2 以上の整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_M) が与えられます。
A に対し、以下の操作を何回でも行うことができます。
- 1 \leq i \leq |A|-1 を満たす整数 i を選ぶ。
- A_i と A_{i+1} の間に 1-A_i と 1-A_{i+1} をこの順で挿入する。
A を B に一致させることができるかどうかを判定してください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq M \leq 2 \times 10^5
- A_i,B_i \in \{0,1\}
- すべてのテストケースにおける N+M の総和は 4 \times 10^5 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを合計 T 行で出力せよ。
t 行目には、t 番目のテストケースについて A を B に一致させることができるならば Yes を、できないならば No を出力せよ。
入力例 1
3 3 7 0 1 1 0 1 0 1 1 0 1 2 4 0 1 0 1 0 1 3 4 0 0 0 0 0 0 1
出力例 1
Yes Yes No
1 つ目のテストケースについて、最初、A=(0,1,1) です。
まず、i として 2 を選ぶと、A=(0,1,0,0,1) となります。
次に、i として 3 を選ぶと、A=(0,1,0,1,1,0,1) となります。
Score : 700 points
Problem Statement
You are given integer sequences A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_M) of length 2 or more, consisting of 0s and 1s.
You can perform the following operation on A any number of times:
- Choose an integer i satisfying 1 \leq i \leq |A|-1.
- Insert 1-A_i and 1-A_{i+1} in this order between A_i and A_{i+1}.
Determine whether it is possible to make A equal to B.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq M \leq 2 \times 10^5
- A_i, B_i \in \{0,1\}
- The sum of N+M over all test cases is at most 4 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Output the answers over a total of T lines.
The t-th line should contain Yes if it is possible to make A equal to B for the t-th test case, and No otherwise.
Sample Input 1
3 3 7 0 1 1 0 1 0 1 1 0 1 2 4 0 1 0 1 0 1 3 4 0 0 0 0 0 0 1
Sample Output 1
Yes Yes No
For the first test case, initially A=(0,1,1).
First, choosing i=2 gives A=(0,1,0,0,1).
Next, choosing i=3 gives A=(0,1,0,1,1,0,1).
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
長さ 2 以上の正整数列のスコアをその数列の最大値と最小値の和で定義します。
長さ 2 以上の正整数列 A に対して、A を長さ 2 以上であるような 1 つ以上の連続部分列に分割したときに各連続部分列のスコアの総和としてあり得る値の最大値を f(A) とします。
より形式的には、ある正整数 K に対して長さが 2 以上であるような K 個の正整数列 B_1,B_2,\dots,B_K をこの順に連結させると A と同じ数列になるときに \sum_{k=1}^{K}\left(\max(B_k)+\min(B_k)\right) としてあり得る値の最大値を f(A) とします。
各要素が 1 以上 N 以下の整数か -1 であるような長さ N の整数列 Q と、正整数 X が与えられます。
以下の条件をすべて満たすような (1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) の総数を 998244353 で割った余りを求めてください。
- i=1,2,\dots,N について、Q_i \neq -1 ならば P_i=Q_i
- f(P)=X
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 10^5
- 1 \leq X \leq 10^{18}
- Q_i=-1 または 1 \leq Q_i \leq N
- Q_i \neq -1 かつ Q_j \neq -1 ならば Q_i \neq Q_j\;(i \neq j)
- すべてのテストケースにおける N の総和は 10^5 以下
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_t は以下の形式で与えられる。
N X Q_1 Q_2 \dots Q_N
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
3 3 4 2 -1 -1 4 10 1 3 4 2 9 42 -1 -1 -1 -1 -1 -1 -1 -1 -1
出力例 1
2 1 155520
1 つ目のテストケースについて、1 つ目の条件を満たすような P は (2,1,3),(2,3,1) の 2 つです。
どちらの場合でも、P を長さ 2 以上であるような連続部分列に分割する方法は、P 自身のみからなる 1 つの連続部分列に分割するという方法しかありません。その場合のスコアの総和は 3+1=4 なので、f(P)=4 となります。
2 つ目のテストケースでは、P=(1,3,4,2) を (1,3) と (4,2) に分割するとスコアの総和は 10 となり、これ以上は大きくできないため、f(P)=10 となります。
Score : 800 points
Problem Statement
Define the score of a positive integer sequence with length 2 or more as the sum of its maximum and minimum values.
For a positive integer sequence A of length 2 or more, let f(A) be the maximum possible value of the total score of the contiguous subsequences when A is divided into one or more contiguous subsequences each of length 2 or more.
More formally, for a positive integer K, let f(A) be the maximum possible value of \sum_{k=1}^{K}\left(\max(B_k)+\min(B_k)\right) when concatenating K positive integer sequences B_1, B_2, \dots, B_K, each of length 2 or more, in this order yields the same sequence as A.
You are given an integer sequence Q of length N where each element is either an integer between 1 and N inclusive or -1, and a positive integer X.
Find the total number, modulo 998244353, of permutations P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) satisfying all of the following conditions.
- For i=1,2,\dots,N, if Q_i \neq -1 then P_i=Q_i.
- f(P)=X
Solve T test cases per input.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 10^5
- 1 \leq X \leq 10^{18}
- Q_i=-1 or 1 \leq Q_i \leq N
- If Q_i \neq -1 and Q_j \neq -1, then Q_i \neq Q_j\;(i \neq j).
- The sum of N over all test cases is at most 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_t is given in the following format:
N X Q_1 Q_2 \dots Q_N
Output
Output the answers over a total of T lines. The t-th line should contain the answer for the t-th test case.
Sample Input 1
3 3 4 2 -1 -1 4 10 1 3 4 2 9 42 -1 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 1
2 1 155520
For the first test case, the permutations P satisfying the first condition are these two: (2,1,3) and (2,3,1).
In either case, the only way to divide P into contiguous subsequences each of length 2 or more is to take P itself as a single contiguous subsequence. The total score in that case is 3+1=4, so f(P)=4.
For the second test case, dividing P=(1,3,4,2) into (1,3) and (4,2) gives a total score of 10, which cannot be exceeded, so f(P)=10.