Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N, M が与えられます.各 j=1,2,\ldots,M に対して,1\leq L_j\leq R_j\leq N を満たす整数組 (L_j,R_j) が与えられます.
長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) であって,すべての j=1,2,\ldots,M に対して次の条件を満たすものを考えます.
- A_{L_j},A_{L_j+1},\ldots,A_{R_j} はすべて相異なる.
このような正整数列 A は必ず存在することが証明できます.条件を満たす正整数列 A のうち,\max(A_1,A_2,\ldots,A_N) の値が最小となるものを 1 つ出力してください.
T 個のテストケースが与えられるので,それぞれについて答えを求めてください.
制約
- 1\leq T\leq 10^5
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 2\times 10^5
- 1\leq L_j \leq R_j\leq N (1\leq j\leq M)
- 入力される値はすべて整数.
- すべてのテストケースにわたる N の総和は 2\times 10^5 以下.
- すべてのテストケースにわたる M の総和は 2\times 10^5 以下.
入力
入力は以下の形式で標準入力から与えられます.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられます.
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
出力
テストケースごとに 1 行出力してください.各テストケースについて,条件を満たす正整数列 A のうち,\max(A_1,A_2,\ldots,A_N) の値が最小となるものについて,その要素を空白区切りで出力してください.
A_1 A_2 \ldots A_N
そのような A が複数存在する場合には,そのどれを出力しても正解となります.
入力例 1
2 5 1 1 5 5 3 1 2 2 3 3 5
出力例 1
1 2 3 4 5 3 1 2 1 3
1 番目のテストケースについて,出力が条件を満たすことは,次のように確かめられます.
- A_1, A_2, A_3, A_4, A_5 は 1, 2, 3, 4, 5 であり,これらは相異なります.
この場合 \max(A_1,A_2,\ldots,A_N) = 5 であり,これが条件を満たす正整数列に対する最小値となります.
2 番目のテストケースについて,出力が条件を満たすことは,次のように確かめられます.
- A_1, A_2 は 3, 1 であり,これらは相異なります.
- A_2, A_3 は 1, 2 であり,これらは相異なります.
- A_3, A_4, A_5 は 2, 1, 3 であり,これらは相異なります.
この場合 \max(A_1,A_2,\ldots,A_N) = 3 であり,これが条件を満たす正整数列に対する最小値となります.
Score : 300 points
Problem Statement
You are given positive integers N and M. For each j = 1, 2, \ldots, M, you are given a pair of integers (L_j, R_j) satisfying 1\leq L_j\leq R_j\leq N.
Consider a length-N positive integer sequence A = (A_1, A_2, \ldots, A_N) satisfying the following condition for all j = 1, 2, \ldots, M:
- A_{L_j}, A_{L_j+1}, \ldots, A_{R_j} are all distinct.
It can be proved that such a positive integer sequence A always exists. Among all positive integer sequences A satisfying the condition, output one that minimizes the value of \max(A_1, A_2, \ldots, A_N).
T test cases are given; solve each of them.
Constraints
- 1\leq T\leq 10^5
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 2\times 10^5
- 1\leq L_j \leq R_j\leq N (1\leq j\leq M)
- All input values are integers.
- The sum of N over all test cases is at most 2\times 10^5.
- The sum of M over all test cases is at most 2\times 10^5.
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 is given in the following format:
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
Output
Output one line per test case. For each test case, output the elements, space-separated, of a positive integer sequence A satisfying the condition that minimizes \max(A_1, A_2, \ldots, A_N).
A_1 A_2 \ldots A_N
If multiple such A exist, any of them will be accepted.
Sample Input 1
2 5 1 1 5 5 3 1 2 2 3 3 5
Sample Output 1
1 2 3 4 5 3 1 2 1 3
For the first test case, the output can be verified to satisfy the condition as follows:
- A_1, A_2, A_3, A_4, A_5 are 1, 2, 3, 4, 5, which are all distinct.
In this case, \max(A_1, A_2, \ldots, A_N) = 5, which is the minimum value for a positive integer sequence satisfying the condition.
For the second test case, the output can be verified to satisfy the condition as follows:
- A_1, A_2 are 3, 1, which are all distinct.
- A_2, A_3 are 1, 2, which are all distinct.
- A_3, A_4, A_5 are 2, 1, 3, which are all distinct.
In this case, \max(A_1, A_2, \ldots, A_N) = 3, which is the minimum value for a positive integer sequence satisfying the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
a+b+c 人の参加者によるじゃんけん大会が行われました.ここで a, b, c は非負整数で,3\leq a+b+c が成り立ちます.
大会では,まず参加者全員が円周上に並びます.その後,各参加者は「グー」「チョキ」「パー」のいずれか 1 つの手を出します.
各参加者について,自分の出した手が,左隣の参加者が出した手と右隣の参加者が出した手の両方に勝っているとき,その参加者は勝者となります.なお,勝者は 1 人とは限らず,0 人であることも 2 人以上であることもあります.
より形式的には,ある参加者が勝者となるのは次のいずれかが成り立つ場合です.
- 自身が「グー」の手を出して,左隣の参加者と右隣の参加者がどちらも「チョキ」の手を出した場合
- 自身が「チョキ」の手を出して,左隣の参加者と右隣の参加者がどちらも「パー」の手を出した場合
- 自身が「パー」の手を出して,左隣の参加者と右隣の参加者がどちらも「グー」の手を出した場合
この大会について,以下のことが分かっています.
- a 人の参加者が「グー」の手を出した.
- b 人の参加者が「チョキ」の手を出した.
- c 人の参加者が「パー」の手を出した.
この条件のもと,大会の勝者の人数としてありうる最大値を求めてください.
T 個のテストケースが与えられるので,それぞれについて解いてください.
制約
- 1\leq T\leq 5\times 10^5
- 0\leq a, b, c\leq 10^9
- 3\leq a+b+c
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられます.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられます.
a b c
出力
テストケースごとに 1 行出力してください.
各テストケースに対して,大会の勝者の人数としてありうる最大値を出力してください.
入力例 1
6 3 1 2 2 2 2 123 456 789 10 0 0 10 0 100 100 0 10
出力例 1
2 1 578 0 9 10
1 番目のテストケースについて説明します.6 人の出した手が,次の図のように表される場合を考えます.ただし図において,「グー」の手を出した参加者を A,「チョキ」の手を出した参加者を B,「パー」の手を出した参加者を C で表しています.

この図において,「グー」の手を出した参加者は 3 人,「チョキ」の手を出した参加者は 1 人,「パー」の手を出した参加者は 2 人で,入力で与えられる内容と一致しています.この場合大会の勝者は,「パー」の手を出した 2 人です.
Score : 500 points
Problem Statement
A rock-paper-scissors tournament was held with a+b+c participants. Here, a, b, c are non-negative integers with 3\leq a+b+c.
In the tournament, all participants first stand in a circle. Then, each participant plays "Rock," "Scissors," or "Paper."
For each participant, if the hand they played beats both the hand of the participant to their left and the hand of the participant to their right, that participant becomes a winner. There is not necessarily exactly one winner; there may be zero, or two or more winners.
More formally, a participant becomes a winner if one of the following holds:
- The participant played "Rock," and both the left neighbor and the right neighbor played "Scissors."
- The participant played "Scissors," and both the left neighbor and the right neighbor played "Paper."
- The participant played "Paper," and both the left neighbor and the right neighbor played "Rock."
The following is known about this tournament:
- a participants played "Rock."
- b participants played "Scissors."
- c participants played "Paper."
Under this condition, find the maximum possible number of winners in the tournament.
T test cases are given; solve each of them.
Constraints
- 1\leq T\leq 5\times 10^5
- 0\leq a, b, c\leq 10^9
- 3\leq a+b+c
- 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 is given in the following format:
a b c
Output
Output one line per test case.
For each test case, output the maximum possible number of winners in the tournament.
Sample Input 1
6 3 1 2 2 2 2 123 456 789 10 0 0 10 0 100 100 0 10
Sample Output 1
2 1 578 0 9 10
Let us explain the first test case. Consider the case where the hands played by the six participants are represented as in the following figure. In the figure, a participant who played "Rock" is denoted by A, a participant who played "Scissors" is denoted by B, and a participant who played "Paper" is denoted by C.

In this figure, there are three participants who played "Rock," one participant who played "Scissors," and two participants who played "Paper," which matches the input. In this case, the winners of the tournament are the two participants who played "Paper."
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
N\times N のマス目があります.ここで N は 2 以上の整数です.1\leq i,j\leq N に対して,上から i 行目,左から j 列目のマスを (i,j) で表します.
はじめ,すべてのマスは黒マスです.また各マス (i,j) について,マスの色を黒から白に変更するためのコスト A_{i,j} が与えられます.
このマス目とひとつの駒を使って,Alice と Bob がゲームを行います.ゲームは次のような 3 つのステップからなります.
ステップ 1
審判が,駒を最初に置くマスを指定します.
ステップ 2
Alice は次の操作を 0 回以上行います.
- 黒マス (i,j) をひとつ選び,コスト A_{i,j} を支払って,そのマスを白マスに変更する.
ステップ 3
ステップ 1 で初期位置として指定されたマスに駒を置きます.その後 Alice から始めて,Alice と Bob は交互に手番を行います.
- Alice の手番では,Alice が駒を左右のいずれかに隣接するマスへ動かす.
- Bob の手番では,Bob が駒を上下左右のいずれかに隣接するマスへ動かす.
ただし,マス目の外へ出るように動かすことはできません.
ステップ 3 は,Alice と Bob がそれぞれ 10^{10} 回ずつ手番を行った直後に終了します.
ステップ 3 が終了した時点で,駒があるマスが白マスならば Alice の勝ち,黒マスならば Bob の勝ちとなります.Alice と Bob はステップ 3 において,それぞれ自分が勝つために最適に行動します.
各マス (h,w) について,ステップ 1 で駒を最初に置くマスとして (h,w) が指定された場合に,Alice が勝つためにステップ 2 で支払う必要がある合計コストの最小値を求めてください.
T 個のテストケースが与えられるので,それぞれについて答えを求めてください.
制約
- 1\leq T\leq 10^4
- 2\leq N\leq 500
- 0\leq A_{i,j}\leq 10^9
- 入力される値はすべて整数.
- すべてのテストケースにわたる N^2 の総和は 500^2 以下.
入力
入力は以下の形式で標準入力から与えられます.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられます.
N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
出力
テストケースごとに N 行出力してください.
各テストケースにおける h 行目には,(h,1),(h,2),\ldots,(h,N) が指定された場合に,Alice が勝つためにステップ 2 で支払う必要がある合計コストの最小値を,空白区切りで出力してください.
入力例 1
3 2 1 4 3 2 3 1 2 3 8 9 4 7 6 5 4 0 0 1 0 1 0 1 1 1 1 1 0 0 1 1 1
出力例 1
3 7 7 3 25 20 25 20 25 20 25 20 25 4 3 4 3 4 4 3 5 4 3 4 3 3 4 3 5
1 番目のテストケースについて説明します.
(h,w) = (1,1) または (h,w) = (2,2) の場合には,Alice はマス (1,1), (2,2) を白マスに変更します.
この場合,Bob が手番を終えるたびに,駒は (1,1) または (2,2) にあります.したがって Alice と Bob がそれぞれ 10^{10} 回ずつ手番を行った直後には駒が (1,1) または (2,2) にあるため,どのように駒を動かしても,Alice の勝ちとなります.Alice がステップ 2 で支払うコストは,A_{1,1}+A_{2,2}=1+2=3 です.
(h,w) = (1,2) または (h,w) = (2,1) の場合にも,Alice はマス (1,2), (2,1) を白マスに変更することで勝てることが確かめられます.この場合,Alice がステップ 2 で支払うコストは,A_{1,2}+A_{2,1}=4+3=7 です.
Score : 700 points
Problem Statement
There is an N\times N grid, where N is an integer at least 2. For 1\leq i,j\leq N, the cell at row i from the top and column j from the left is denoted by (i,j).
Initially, all cells are black. For each cell (i,j), a cost A_{i,j} to change the color of the cell from black to white is given.
Alice and Bob play a game using this grid and one piece. The game consists of the following three steps.
Step 1
The referee specifies the cell where the piece is initially placed.
Step 2
Alice performs the following operation zero or more times:
- Choose a black cell (i,j), pay cost A_{i,j}, and change that cell to white.
Step 3
Place the piece on the cell specified as the initial position in Step 1. Then, starting with Alice, Alice and Bob take turns making moves.
- On Alice's turn, Alice moves the piece to an adjacent cell to the left or right.
- On Bob's turn, Bob moves the piece to an adjacent cell in any of the up, down, left, or right directions.
A move that would take the piece outside the grid is not allowed.
Step 3 ends immediately after Alice and Bob have each taken 10^{10} turns.
At the end of Step 3, if the piece is on a white cell, Alice wins; if it is on a black cell, Bob wins. In Step 3, Alice and Bob each act optimally to win.
For each cell (h,w), find the minimum total cost that Alice must pay in Step 2 to guarantee a win if (h,w) is specified as the initial cell in Step 1.
T test cases are given; solve each of them.
Constraints
- 1\leq T\leq 10^4
- 2\leq N\leq 500
- 0\leq A_{i,j}\leq 10^9
- All input values are integers.
- The sum of N^2 over all test cases is at most 500^2.
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 is given in the following format:
N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
Output
Output N lines per test case.
For each test case, the h-th line should contain the minimum total cost that Alice must pay in Step 2 to guarantee a win for each of (h,1),(h,2),\ldots,(h,N) specified as the initial cell, space-separated.
Sample Input 1
3 2 1 4 3 2 3 1 2 3 8 9 4 7 6 5 4 0 0 1 0 1 0 1 1 1 1 1 0 0 1 1 1
Sample Output 1
3 7 7 3 25 20 25 20 25 20 25 20 25 4 3 4 3 4 4 3 5 4 3 4 3 3 4 3 5
Let us explain the first test case.
For (h,w) = (1,1) or (h,w) = (2,2), Alice changes cells (1,1) and (2,2) to white.
In this case, at the end of each of Bob's turns, the piece is at (1,1) or (2,2). Therefore, immediately after Alice and Bob have each taken 10^{10} turns, the piece is at (1,1) or (2,2), so Alice wins regardless of how the piece is moved. The cost Alice pays in Step 2 is A_{1,1}+A_{2,2}=1+2=3.
For (h,w) = (1,2) or (h,w) = (2,1), it can be verified that Alice can win by changing cells (1,2) and (2,1) to white. In this case, the cost Alice pays in Step 2 is A_{1,2}+A_{2,1}=4+3=7.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
非負整数 n の桁和を,n を 10 進法で表したときの各桁の和と定義します.例えば 2026 の桁和は 2+0+2+6=10 です.
正整数列 A=(A_1,A_2,\ldots,A_N),B=(B_1,B_2,\ldots,B_N) が与えられます.
A, B を用いて,整数列 X=(X_0,X_1,X_2,\ldots,X_N) を次のようにして定めます.
- X_0 = 0 と定める.
- 各 i=1,2,\ldots,N について,X_i = 10X_{i-1} + A_i と X_i = 10X_{i-1} + B_i のどちらか一方を選んで,X_i を定める.
各 k=1,2,\ldots,N に対して,X_k の桁和としてありうる最小値を求めてください.
各 k について独立に解いてください.つまり,異なる k に対して,X_k の桁和を最小化する場合の整数列 X の定め方は同じである必要はありません.
制約
- 1\leq N\leq 20000
- 1\leq A_i \leq 10^9-1
- 1\leq B_i \leq 10^9-1
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられます.
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
k=1,2,\ldots,N に対して,X_k の桁和としてありうる最小値を,空白区切りで出力してください.
入力例 1
2 123 444 456 222
出力例 1
6 9
整数列 X=(X_0,X_1,X_2) としてありうるのは,次の 4 通りです.
- X = (0, 123, 1674)
- X = (0, 123, 1452)
- X = (0, 456, 5004)
- X = (0, 456, 4782)
X_1 の桁和としてありうる最小値は,123 の桁和である 6 です.
X_2 の桁和としてありうる最小値は,5004 の桁和である 9 です.
入力例 2
15 47640 28384 12457 87254 35032 27915 72870 32688 59091 95771 44037 44252 82222 87110 45829 41522 28795 85247 87801 45150 79509 15308 17923 75921 14992 71984 7601 42312 98436 36545
出力例 2
14 18 26 19 28 33 28 34 29 27 29 39 35 43 41
Score : 700 points
Problem Statement
Define the digit sum of a non-negative integer n as the sum of the digits when n is written in decimal notation. For example, the digit sum of 2026 is 2+0+2+6=10.
You are given positive integer sequences A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
Using A and B, define an integer sequence X=(X_0,X_1,X_2,\ldots,X_N) as follows:
- Set X_0 = 0.
- For each i=1,2,\ldots,N, choose one of X_i = 10X_{i-1} + A_i and X_i = 10X_{i-1} + B_i to determine X_i.
For each k=1,2,\ldots,N, find the minimum possible digit sum of X_k.
Solve independently for each k. That is, for different k, the way to determine the integer sequence X that minimizes the digit sum of X_k may be different.
Constraints
- 1\leq N\leq 20000
- 1\leq A_i \leq 10^9-1
- 1\leq B_i \leq 10^9-1
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
For k=1,2,\ldots,N, output the minimum possible digit sum of X_k, space-separated.
Sample Input 1
2 123 444 456 222
Sample Output 1
6 9
The possible integer sequences X=(X_0,X_1,X_2) are the following four:
- X = (0, 123, 1674)
- X = (0, 123, 1452)
- X = (0, 456, 5004)
- X = (0, 456, 4782)
The minimum possible digit sum of X_1 is 6, which is the digit sum of 123.
The minimum possible digit sum of X_2 is 9, which is the digit sum of 5004.
Sample Input 2
15 47640 28384 12457 87254 35032 27915 72870 32688 59091 95771 44037 44252 82222 87110 45829 41522 28795 85247 87801 45150 79509 15308 17923 75921 14992 71984 7601 42312 98436 36545
Sample Output 2
14 18 26 19 28 33 28 34 29 27 29 39 35 43 41
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
N 枚のカードがあります.i 番目のカード(1\leq i\leq N)には整数 A_i が書かれています.ここで,0\leq A_i \leq 2^M-1 が成り立ちます.
各整数 X=0,1,\ldots,2^M-1 に対して,次の問題の答えを f(X) と書くことにします.
カードのペアをいくつか作ることを考えます.
ただし,ペアは相異なる 2 枚のカードからなり,それら 2 枚のカードに書かれた整数のビット単位 XOR が X となる必要があります. 同じカードを複数のペアに含めることはできません.
この条件のもとで作れるペアの個数の最大値を求めてください.
次の値を出力してください.
\[ \left(\sum_{X=0}^{2^M-1} f(X) \times 10 ^ X\right) \bmod 998244353 \]
ビット単位 \mathrm{XOR} 演算とは
非負整数 A, B のビット単位 \mathrm{XOR},A \oplus B は,以下のように定義されます.
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は,A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1,そうでなければ 0 である.
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され,これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます.
制約
- 2\leq N\leq 2\times 10^5
- 1\leq M\leq 20
- 0\leq A_i \leq 2^M-1 (1\leq i\leq N)
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられます.
N M A_1 A_2 \ldots A_N
出力
次の値を出力してください.
\[ \left(\sum_{X=0}^{2^M-1} f(X) \times 10 ^ X\right) \bmod 998244353 \]
入力例 1
4 2 1 1 3 3
出力例 1
202
- X=0 の場合:(A_1,A_2),(A_3,A_4) という 2 個のペアを作ることができます.
- X=1 の場合:0 個のペアを作ることができます.
- X=2 の場合:(A_1,A_4),(A_2,A_3) という 2 個のペアを作ることができます.
- X=3 の場合:0 個のペアを作ることができます.
したがって,f(0)=2,f(1)=0,f(2)=2,f(3)=0 です.出力すべき値は 2\times 1 + 0\times 10 + 2\times 100 + 0\times 1000 = 202 です.
入力例 2
10 3 5 2 1 4 5 5 5 0 1 7
出力例 2
22332223
入力例 3
10 4 11 6 3 8 6 7 14 10 10 11
出力例 3
613668597
Score : 700 points
Problem Statement
There are N cards. The i-th card (1\leq i\leq N) has the integer A_i written on it. Here, 0\leq A_i \leq 2^M-1 holds.
For each integer X=0,1,\ldots,2^M-1, let f(X) denote the answer to the following problem:
Consider forming some pairs of cards.
Each pair consists of two distinct cards, and the bitwise XOR of the integers written on those two cards must equal X. The same card cannot be included in multiple pairs.
Find the maximum number of pairs that can be formed under these conditions.
Output the following value:
\[ \left(\sum_{X=0}^{2^M-1} f(X) \times 10 ^ X\right) \bmod 998244353 \]
What is the bitwise \mathrm{XOR} operation
The bitwise \mathrm{XOR} of non-negative integers A, B, A \oplus B, is defined as follows.
- The digit at the 2^k (k \geq 0) place of A \oplus B in binary representation is 1 if exactly one of the digits at the 2^k place of A and B in binary representation is 1, and 0 otherwise.
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.
Constraints
- 2\leq N\leq 2\times 10^5
- 1\leq M\leq 20
- 0\leq A_i \leq 2^M-1 (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Output the following value:
\[ \left(\sum_{X=0}^{2^M-1} f(X) \times 10 ^ X\right) \bmod 998244353 \]
Sample Input 1
4 2 1 1 3 3
Sample Output 1
202
- For X=0: two pairs (A_1,A_2) and (A_3,A_4) can be formed.
- For X=1: zero pairs can be formed.
- For X=2: two pairs (A_1,A_4) and (A_2,A_3) can be formed.
- For X=3: zero pairs can be formed.
Therefore, f(0)=2, f(1)=0, f(2)=2, and f(3)=0. The value to be output is 2\times 1 + 0\times 10 + 2\times 100 + 0\times 1000 = 202.
Sample Input 2
10 3 5 2 1 4 5 5 5 0 1 7
Sample Output 2
22332223
Sample Input 3
10 4 11 6 3 8 6 7 14 10 10 11
Sample Output 3
613668597
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
非負整数の 3 つ組 (x,y,z) に対する操作を考えます.操作では,(x,y,z) を次の規則に従って置き換えます.
- y+z<x のとき:(x-y-z,\ 2y,\ 2z) に置き換える.
- x+z<y のとき:(2x,\ y-x-z,\ 2z) に置き換える.
- x+y<z のとき:(2x,\ 2y,\ z-x-y) に置き換える.
- 上のいずれの条件も満たさないとき:(y+z-x,\ x+z-y,\ x+y-z) に置き換える.
なおすべての非負整数の 3 つ組は,上記の 4 つの場合のうちちょうど 1 つに該当します.
非負整数 A_1, A_2, A_3, B_1, B_2, B_3 が与えられます.3 つ組 (A_1,A_2,A_3) に対して操作を 0 回以上繰り返し行って,(B_1,B_2,B_3) にすることを考えます.そのために行う操作回数の最小値を求めてください.ただし,何回操作を繰り返しても (B_1,B_2,B_3) にできない場合は,-1 を出力してください.
T 個のテストケースが与えられるので,それぞれについて解いてください.
制約
- 1\leq T\leq 300
- 0\leq A_1, A_2, A_3, B_1, B_2, B_3\leq 10^8
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられます.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられます.
A_1 A_2 A_3 B_1 B_2 B_3
出力
テストケースごとに 1 行出力してください.
各テストケースに対して,3 つ組 (A_1,A_2,A_3) に対して操作を 0 回以上繰り返し行って,(B_1,B_2,B_3) にするために行う操作回数の最小値を出力してください.ただし,何回操作を繰り返しても (B_1,B_2,B_3) にできない場合は,-1 を出力してください.
入力例 1
6 2 3 4 2 3 4 2 3 4 5 3 1 2 3 4 1 6 2 2 3 4 4 3 2 1 0 0 0 0 1 0 0 0 0 0 0
出力例 1
0 1 2 -1 -1 0
(A_1,A_2,A_3)=(2,3,4) である場合,操作を繰り返すと 3 つ組は次のように変化します.
- (2,3,4)\to (5,3,1)\to (1,6,2)\to (2,3,4)\to (5,3,1)\to (1,6,2)\to\cdots
このことから,はじめの 3 つのテストケースの答えが 0, 1, 2 であることが分かります.
Score : 700 points
Problem Statement
Consider an operation on a triple (x,y,z) of non-negative integers. In the operation, (x,y,z) is replaced according to the following rules:
- If y+z<x: replace with (x-y-z,\ 2y,\ 2z).
- If x+z<y: replace with (2x,\ y-x-z,\ 2z).
- If x+y<z: replace with (2x,\ 2y,\ z-x-y).
- If none of the above conditions hold: replace with (y+z-x,\ x+z-y,\ x+y-z).
Note that every triple of non-negative integers falls into exactly one of the above four cases.
You are given non-negative integers A_1, A_2, A_3, B_1, B_2, B_3. Consider performing the operation on the triple (A_1,A_2,A_3) zero or more times to obtain (B_1,B_2,B_3). Find the minimum number of operations required to do so. If it is impossible to obtain (B_1,B_2,B_3) no matter how many times the operation is performed, output -1.
T test cases are given; solve each of them.
Constraints
- 1\leq T\leq 300
- 0\leq A_1, A_2, A_3, B_1, B_2, B_3\leq 10^8
- 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 is given in the following format:
A_1 A_2 A_3 B_1 B_2 B_3
Output
Output one line per test case.
For each test case, output the minimum number of operations required to obtain (B_1,B_2,B_3) by performing the operation on the triple (A_1,A_2,A_3) zero or more times. If it is impossible to obtain (B_1,B_2,B_3) no matter how many times the operation is performed, output -1.
Sample Input 1
6 2 3 4 2 3 4 2 3 4 5 3 1 2 3 4 1 6 2 2 3 4 4 3 2 1 0 0 0 0 1 0 0 0 0 0 0
Sample Output 1
0 1 2 -1 -1 0
For (A_1,A_2,A_3)=(2,3,4), the triple changes as follows when the operation is repeatedly applied:
- (2,3,4)\to (5,3,1)\to (1,6,2)\to (2,3,4)\to (5,3,1)\to (1,6,2)\to\cdots
From this, we can see that the answers to the first three test cases are 0, 1, 2.