A - Two Arithmetic Progressions

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

正整数 N,A,B,C,D が与えられます.

\displaystyle \sum_{i=1}^N \gcd (Ai+B,Ci+D)998244353 で割った余りを求めてください.ただし,\gcd(x,y)xy の最大公約数を表します.

T 個のテストケースが与えられるので,それぞれについて答えを求めてください.

制約

  • 1\leq T\leq 200
  • 1\leq N\leq 10^9
  • 1\leq A,B,C,D\leq 10^4
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる.

N A B C D

出力

各テストケースに対する答えを順に改行区切りで出力せよ.


入力例 1

3
4 1 3 4 2
100000000 1 1 1 2
100000000 1 1 1 1

出力例 1

10
100000000
822404071

1 番目のテストケースについて,答えは \gcd(4,6)+\gcd(5,10)+\gcd(6,14)+\gcd(7,18)=2+5+2+1=10 です.

Score : 600 points

Problem Statement

You are given positive integers N,A,B,C,D.

Find \displaystyle \sum_{i=1}^N \gcd (Ai+B,Ci+D), modulo 998244353. Here, \gcd(x,y) denotes the greatest common divisor of x and y.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T\leq 200
  • 1\leq N\leq 10^9
  • 1\leq A,B,C,D\leq 10^4
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N A B C D

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

3
4 1 3 4 2
100000000 1 1 1 2
100000000 1 1 1 1

Sample Output 1

10
100000000
822404071

For the first test case, the answer is \gcd(4,6)+\gcd(5,10)+\gcd(6,14)+\gcd(7,18)=2+5+2+1=10.

B - Two-Powered Sum

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 800

問題文

正整数 N と素数 P が与えられます.

すべての要素が 0 である長さ N の数列 A=(A_1,A_2,\ldots,A_N) があります.

あなたは以下の操作を 0 回以上好きな回数繰り返すことができます.

  • 非空な \lbrace 1,2,\ldots,N\rbrace の部分集合 S を選ぶ.\displaystyle x=\sum_{i\in S}2^{i-1} として,各 i\in S に対して A_ix で置き換える.

操作を繰り返すことで最終的に得られる A としてあり得るものの個数を P で割った余りを求めてください.

制約

  • 1\leq N\leq 700
  • P10^8\lt P\lt 10^9 を満たす素数
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

N P

出力

答えを出力せよ.


入力例 1

1 998244353

出力例 1

2

得られる A(0),(1)2 通りです.


入力例 2

2 998244353

出力例 2

7

得られる A(0,0),(0,2),(1,0),(1,2),(1,3),(3,2),(3,3)7 通りです.


入力例 3

3 998244353

出力例 3

57

入力例 4

4 998244353

出力例 4

1208

入力例 5

77 777777773

出力例 5

381787647

Score : 800 points

Problem Statement

You are given a positive integer N and a prime P.

There is a length-N sequence A=(A_1,A_2,\ldots,A_N) where all elements are 0.

You can repeat the following operation any number of times, possibly zero:

  • Choose a non-empty subset S of \lbrace 1,2,\ldots,N\rbrace. Let \displaystyle x=\sum_{i\in S}2^{i-1}. For each i\in S, replace A_i with x.

Find the number, modulo P, of possible sequences A after repeating the operations.

Constraints

  • 1\leq N\leq 700
  • P is a prime satisfying 10^8\lt P\lt 10^9.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N P

Output

Output the answer.


Sample Input 1

1 998244353

Sample Output 1

2

The possible sequences A are (0),(1), giving two possibilities.


Sample Input 2

2 998244353

Sample Output 2

7

The possible sequences A are (0,0),(0,2),(1,0),(1,2),(1,3),(3,2),(3,3), giving seven possibilities.


Sample Input 3

3 998244353

Sample Output 3

57

Sample Input 4

4 998244353

Sample Output 4

1208

Sample Input 5

77 777777773

Sample Output 5

381787647
C - Two Deques Sorting

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 800

問題文

正整数 N と,長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます.

また,長さ 0 の正整数列 B があります.

あなたは,以下の操作を 0 回以上 N 回以下行うことができます.

  • A の先頭または末尾の要素を削除し,その要素を B の先頭または末尾に追加する.

操作終了後に B が狭義単調増加列である必要があるとき,最大で何回操作を行うことができますか.

T 個のテストケースが与えられるので,それぞれについて答えを求めてください.

制約

  • 1\le T\le 3\times 10^5
  • 1\le N\le 3\times 10^5
  • 1\le A_i \le N (1\leq i\leq N)
  • 全てのテストケースにおける N の総和は 3\times 10^5 以下
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる.

N
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ.


入力例 1

3
5
3 1 4 1 5
3
2 2 1
16
3 4 5 7 12 15 1 15 16 8 14 2 16 14 9 2

出力例 1

4
2
11

1 番目のテストケースについて,以下のように操作することを考えます.

  • A の先頭の要素を削除し,その要素を B の末尾に追加する.A=(1,4,1,5),B=(3) となる.
  • A の先頭の要素を削除し,その要素を B の先頭に追加する.A=(4,1,5),B=(1,3) となる.
  • A の先頭の要素を削除し,その要素を B の末尾に追加する.A=(1,5),B=(1,3,4) となる.
  • A の末尾の要素を削除し,その要素を B の末尾に追加する.A=(1),B=(1,3,4,5) となる.

このとき最終的な B=(1,3,4,5) は狭義単調増加列であり,操作回数は 4 回です.条件を満たすように 5 回操作することはできないので,答えは 4 です.

2 番目のテストケースについて,B=(1,2,2) は狭義単調増加列でないことに注意してください.

Score : 800 points

Problem Statement

You are given a positive integer N and a length-N sequence of positive integers A=(A_1,A_2,\dots,A_N).

There is also a length-0 sequence of positive integers B.

You can perform the following operation between 0 and N times, inclusive.

  • Remove the first or last element of A, and append that element to the beginning or end of B.

What is the maximum number of operations you can perform, given that B must be a strictly increasing sequence after the operations?

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 3\times 10^5
  • 1\le N\le 3\times 10^5
  • 1\le A_i \le N (1\leq i\leq N)
  • The sum of N over all test cases is at most 3\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

3
5
3 1 4 1 5
3
2 2 1
16
3 4 5 7 12 15 1 15 16 8 14 2 16 14 9 2

Sample Output 1

4
2
11

For the first test case, consider performing the operations as follows.

  • Remove the first element of A and append it to the end of B. Now A=(1,4,1,5),B=(3).
  • Remove the first element of A and append it to the beginning of B. Now A=(4,1,5),B=(1,3).
  • Remove the first element of A and append it to the end of B. Now A=(1,5),B=(1,3,4).
  • Remove the last element of A and append it to the end of B. Now A=(1),B=(1,3,4,5).

The final B=(1,3,4,5) is a strictly increasing sequence, and the number of operations is 4. It is impossible to perform five operations while satisfying the condition, so the answer is 4.

For the second test case, note that B=(1,2,2) is not a strictly increasing sequence.

D - Two Balanced Subtrees

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 800

問題文

正整数 N が与えられます.

頂点数が 2^N-1 の完全二分木があります.頂点には 1 から 2^N-1 までの番号が付いています.

頂点 1 が根であり,各 i\ (1\leq i\lt 2^{N-1}) について,頂点 i は頂点 2i と頂点 2i+1 を子として持ちます.

各頂点に 1 以上 2^N-1 以下の整数を書き込む(ただし,書き込む 2^N-1 個の整数は相異なるようにする)方法であって,以下の条件を満たすようなものを一つ求めてください.

  • i\ (1\leq i\lt 2^{N-1}) について,頂点 2i を根とする部分木の各頂点に書かれた整数の総和と,頂点 2i+1 を根とする部分木の各頂点に書かれた整数の総和の差の絶対値は 1 である.

この問題の制約下で条件を満たす整数の書き込み方が必ず存在することが証明できます.

制約

  • 2\leq N\leq 18
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

N

出力

頂点 i に書き込む整数を P_i として

P_1 P_2 \ldots P_{2^N-1}

と出力せよ.(P_1,P_2,\ldots,P_{2^N-1})(1,2,\ldots,2^N-1) の順列である必要がある.

条件を満たす書き込み方が複数存在する場合には,そのどれを出力しても正解と見なされる.


入力例 1

3

出力例 1

1 7 2 3 4 5 6

この書き込み方が条件を満たすことは,以下のようにして確かめられます.

  • i=1:頂点 2 を根とする部分木の各頂点に書かれた整数の総和は 14,頂点 3 を根とする部分木の各頂点に書かれた整数の総和は 13 であり,|14-13|=1 である.
  • i=2:頂点 4 を根とする部分木の各頂点に書かれた整数の総和は 3,頂点 5 を根とする部分木の各頂点に書かれた整数の総和は 4 であり,|3-4|=1 である.
  • i=3:頂点 6 を根とする部分木の各頂点に書かれた整数の総和は 5,頂点 7 を根とする部分木の各頂点に書かれた整数の総和は 6 であり,|5-6|=1 である.

Score : 800 points

Problem Statement

You are given a positive integer N.

There is a complete binary tree with 2^N-1 vertices. The vertices are numbered 1 through 2^N-1.

Vertex 1 is the root, and for each i\ (1\leq i\lt 2^{N-1}), vertex i has vertices 2i and 2i+1 as its children.

Find one way to write an integer between 1 and 2^N-1, inclusive, on the vertices (where all 2^N-1 written integers are distinct) such that the following condition is satisfied.

  • For each i\ (1\leq i\lt 2^{N-1}), the absolute difference between the sum of the integers written on the vertices of the subtree rooted at vertex 2i and the sum of the integers written on the vertices of the subtree rooted at vertex 2i+1 is 1.

It can be proved that there always exists a way to write the integers satisfying the condition under the constraints of this problem.

Constraints

  • 2\leq N\leq 18
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N

Output

Let P_i be the integer written on vertex i. Output:

P_1 P_2 \ldots P_{2^N-1}

(P_1,P_2,\ldots,P_{2^N-1}) must be a permutation of (1,2,\ldots,2^N-1).

If there are multiple valid ways to write the integers, any of them will be accepted.


Sample Input 1

3

Sample Output 1

1 7 2 3 4 5 6

We can verify that this assignment satisfies the condition, as follows.

  • i=1: The sum of the integers written on the vertices of the subtree rooted at vertex 2 is 14, the sum of the integers written on the vertices of the subtree rooted at vertex 3 is 13, and |14-13|=1.
  • i=2: The sum of the integers written on the vertices of the subtree rooted at vertex 4 is 3, the sum of the integers written on the vertices of the subtree rooted at vertex 5 is 4, and |3-4|=1.
  • i=3: The sum of the integers written on the vertices of the subtree rooted at vertex 6 is 5, the sum of the integers written on the vertices of the subtree rooted at vertex 7 is 6, and |5-6|=1.
E - Two Increasing Sequences

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 1000

問題文

正整数 N と正整数 X(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます.X のみ二進法で与えられ,NP の要素は十進法で与えられます.

以下の条件を全て満たすような非負整数列 A=(A_1,A_2,\ldots,A_N)B=(B_1,B_2,\ldots,B_N) が存在するか判定してください.

  • A,B は共に狭義単調増加列である.
  • i=1,2,\ldots,N に対し A_{P_i}=B_i \oplus X が成り立つ.

ただし,二項演算子 \oplus は非負整数同士の bit 毎の排他的論理和を表します.

T 個のテストケースが与えられるので,それぞれについて答えを求めてください.

制約

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le X < 2^{10^6}
  • P(1,2,\ldots,N) の順列
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下
  • 全てのテストケースにおける X の二進法における桁数の総和は 10^6 以下
  • T,N,P_i は十進法で与えられる
  • X は leading zero がない二進法で与えられる
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる.

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる.

N
X
P_1 P_2 \ldots P_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ.

各テストケースについて,条件を全て満たす AB が存在する場合は Yes を,存在しない場合は No を出力せよ.


入力例 1

3
4
101
3 4 2 1
4
100
4 3 2 1
8
1101011
3 5 4 1 2 6 8 7

出力例 1

Yes
No
Yes

1 番目のテストケースについて,例えば A=(2,3,5,7)B=(0,2,6,7) が条件を満たします.

Score : 1000 points

Problem Statement

You are given a positive integer N, a positive integer X, and a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N). Only X is given in binary, while N and the elements of P are given in decimal.

Determine whether there exist sequences of non-negative integers A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N) satisfying all of the following conditions.

  • Both A and B are strictly increasing sequences.
  • A_{P_i}=B_i \oplus X holds for i=1,2,\ldots,N.

Here, the binary operator \oplus denotes the bitwise XOR of non-negative integers.

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le X < 2^{10^6}
  • P is a permutation of (1,2,\ldots,N).
  • The sum of N over all test cases is at most 2\times 10^5.
  • The sum of the number of digits of X in binary over all test cases is at most 10^6.
  • T,N,P_i are given in decimal.
  • X is given in binary without leading zeros.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N
X
P_1 P_2 \ldots P_N

Output

Output the answers for the test cases in order, separated by newlines.

For each test case, output Yes if there exist A and B satisfying all the conditions, and No otherwise.


Sample Input 1

3
4
101
3 4 2 1
4
100
4 3 2 1
8
1101011
3 5 4 1 2 6 8 7

Sample Output 1

Yes
No
Yes

For the first test case, for example, A=(2,3,5,7) and B=(0,2,6,7) satisfy the conditions.