E - Cycle Graph?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1,2,\dots,N の番号が、辺には 1,2,\dots,M の番号がつけられており、辺 i は頂点 A_i と頂点 B_i を結んでいます。

このグラフがサイクルグラフであるか判定してください。

単純無向グラフとは 単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

サイクルグラフとは 頂点に 1, 2, \dots, N の番号が付けられた N 頂点のグラフがサイクルグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
  • i = 1, 2, \dots, N-1 に対して、頂点 v_iv_{i+1} を結ぶ辺が存在する
  • 頂点 v_Nv_1 を結ぶ辺が存在する
  • それら以外の辺は存在しない

制約

  • 3\leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは単純
  • 入力は全て整数

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

与えられたグラフがサイクルグラフであるなら Yes、そうでないなら No と出力せよ。


入力例 1

4 4
2 4
3 1
4 1
2 3

出力例 1

Yes

与えられたグラフは以下の通りであり、これはサイクルグラフです。

図


入力例 2

4 6
1 2
1 3
1 4
2 3
2 4
3 4

出力例 2

No

与えられたグラフは以下の通りであり、これはサイクルグラフではありません。

図

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1,2,\dots,N and the edges are numbered 1,2,\dots,M. Edge i connects vertices A_i and B_i.

Determine whether this graph is a cycle graph.

Definition of simple undirected graph A simple undirected graph is a graph with undirected edges without self-loops or multi-edges.

Definition of cycle graph An N-vertex graph with vertices labeled 1,2,\dots,N is a cycle graph when there exists a permutation (v_1,v_2,\dots,v_N) of (1,2,\dots,N) such that:
  • For each i = 1,2,\dots,N-1, there is an edge between vertices v_i and v_{i+1}.
  • There is an edge between vertices v_N and v_1.
  • No other edges exist.

Constraints

  • 3 \le N \le 2\times 10^5
  • 0 \le M \le 2\times 10^5
  • 1 \le A_i, B_i \le N
  • The given graph is simple.
  • All input values are integers.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Output Yes if the given graph is a cycle graph; otherwise, print No.


Sample Input 1

4 4
2 4
3 1
4 1
2 3

Sample Output 1

Yes

The given graph is as follows, and this is a cycle graph.

Figure


Sample Input 2

4 6
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 2

No

The given graph is as follows, and this is not a cycle graph.

Figure

F - Palindromic in Both Bases

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

414 の十進法での表記は 414 であり、これは回文です。 また、414 の八進法での表記は 636 であり、これも回文です。 これを踏まえて、以下の問題を解いてください。

正の整数 A, N が与えられます。 1 以上 N 以下の整数のうち、十進法での表記も A 進法での表記も回文であるようなものの総和を求めてください。

なお、この問題の制約下で答えは 2^{63} 未満であることが証明できます。

制約

  • 2 \leq A \leq 9
  • 1 \leq N \le 10^{12}
  • 入力される数値は全て整数である。

入力

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

A
N

出力

答えを 1 行で出力せよ。


入力例 1

8
1000

出力例 1

2155

条件を満たす整数は 1,2,3,4,5,6,7,9,121,292,333,373,414,58514 個であり、その総和は 2155 です。


入力例 2

8
999999999999

出力例 2

914703021014

入力例 3

6
999999999999

出力例 3

283958331810

Score : 350 points

Problem Statement

The decimal representation of 414 is 414, which is a palindrome. Also, the octal representation of 414 is 636, which is also a palindrome. Based on this, solve the following problem.

You are given positive integers A and N. Find the sum of all integers between 1 and N, inclusive, whose decimal representation and base-A representation are both palindromes.

Under the constraints of this problem, it can be proved that the answer is less than 2^{63}.

Constraints

  • 2 \leq A \leq 9
  • 1 \leq N \le 10^{12}
  • All input values are integers.

Input

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

A
N

Output

Output the answer in one line.


Sample Input 1

8
1000

Sample Output 1

2155

The integers satisfying the condition are 1,2,3,4,5,6,7,9,121,292,333,373,414,585 (14 integers), and their sum is 2155.


Sample Input 2

8
999999999999

Sample Output 2

914703021014

Sample Input 3

6
999999999999

Sample Output 3

283958331810
G - String Rotation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の英小文字からなる文字列 S=S_1S_2\dots S_N が与えられます。あなたは、S に対して以下の操作をちょうど 1 回行います。

  • S の長さ 1 以上の連続部分文字列を選んで、左に 1 だけ巡回シフトする。すなわち、整数 1 \leq \ell \leq r \leq N を選んで、Sr 文字目の右に S_\ell を挿入したのち、S\ell 番目の文字を削除する。

操作後の S としてありえる文字列のうち、辞書順最小のものを求めてください。

T 個のテストケースが与えられるので、それぞれについて答えてください。

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • S は英小文字からなる長さ N の文字列
  • T,N は整数
  • 1 つの入力ファイルに含まれる N の総和は 10^5 以下

入力

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

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

各テストケース \mathrm{case}_i\;(1 \leq i \leq T) は以下の形式で与えられる。

N
S

出力

T 行出力せよ。i \; (1 \leq i \leq T) 行目には、\mathrm{case}_i に対する答えを出力せよ。


入力例 1

3
7
atcoder
1
x
5
snuke

出力例 1

acodert
x
nsuke
  • 1 番目のテストケースでは、2 文字目から 7 文字目を巡回シフトして acodert とするのが辞書順最小です。
  • 2 番目のテストケースでは、どのように操作しても x が得られます。
  • 3 番目のテストケースでは、1 文字目から 2 文字目を巡回シフトして nsuke とするのが辞書順最小です。

Score : 400 points

Problem Statement

You are given a string S=S_1S_2\dots S_N of length N consisting of lowercase English letters. You will perform the following operation on S exactly once:

  • Choose a contiguous substring of S with length at least 1 and cyclically shift it to the left by 1. That is, choose integers 1 \leq \ell \leq r \leq N, insert S_\ell to the right of the r-th character of S, and then delete the \ell-th character of S.

Find the lexicographically smallest string among all possible strings that S can become after the operation.

You are given T test cases, so solve each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • S is a string of length N consisting of lowercase English letters.
  • T and N are integers.
  • The sum of N over all test cases in a single input file is at most 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 \mathrm{case}_i\;(1 \leq i \leq T) is given in the following format:

N
S

Output

Output T lines. The i-th (1 \leq i \leq T) line should contain the answer for \mathrm{case}_i.


Sample Input 1

3
7
atcoder
1
x
5
snuke

Sample Output 1

acodert
x
nsuke
  • In the first test case, cyclically shifting from the 2nd to the 7th character gives acodert, which is lexicographically smallest.
  • In the second test case, no matter how you operate, you get x.
  • In the third test case, cyclically shifting from the 1st to the 2nd character gives nsuke, which is lexicographically smallest.
H - Tangency of Cuboids

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

3 次元空間内に N 個の直方体があります。

直方体同士は重なっていません。厳密には、相異なるどの 2 つの直方体の共通部分の体積も 0 です。

i 番目の直方体は、2(X_{i,1},Y_{i,1},Z_{i,1}), (X_{i,2},Y_{i,2},Z_{i,2}) を結ぶ線分を対角線とし、辺は全ていずれかの座標軸に平行です。

各直方体について、他のいくつの直方体と面で接しているか求めてください。
厳密には、各 i に対し、1\leq j \leq N かつ j\neq i である j のうち、i 番目の直方体の表面と j 番目の直方体の表面の共通部分の面積が正であるものの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • 直方体同士は体積が正の共通部分を持たない
  • 入力は全て整数である

入力

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

出力

答えを出力せよ。


入力例 1

4
0 0 0 1 1 1
0 0 1 1 1 2
1 1 1 2 2 2
3 3 3 4 4 4

出力例 1

1
1
0
0

1 番目の直方体と 2 番目の直方体は、2(0,0,1),(1,1,1) を結ぶ線分を対角線とする長方形を共有しています。
1 番目の直方体と 3 番目の直方体は、点 (1,1,1) を共有していますが、面で接してはいません。


入力例 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

出力例 2

2
1
1

入力例 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

出力例 3

3
3
3
3
3
3
3
3

Score : 500 points

Problem Statement

There are N rectangular cuboids in a three-dimensional space.

These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has a volume of 0.

The diagonal of the i-th cuboid is a segment that connects two points (X_{i,1},Y_{i,1},Z_{i,1}) and (X_{i,2},Y_{i,2},Z_{i,2}), and its edges are all parallel to one of the coordinate axes.

For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each i, find the number of j with 1\leq j \leq N and j\neq i such that the intersection of the surfaces of the i-th and j-th cuboids has a positive area.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • Cuboids do not have an intersection with a positive volume.
  • All input values are integers.

Input

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

Output

Print the answer.


Sample Input 1

4
0 0 0 1 1 1
0 0 1 1 1 2
1 1 1 2 2 2
3 3 3 4 4 4

Sample Output 1

1
1
0
0

The 1-st and 2-nd cuboids share a rectangle whose diagonal is the segment connecting two points (0,0,1) and (1,1,1).
The 1-st and 3-rd cuboids share a point (1,1,1), but do not share a surface.


Sample Input 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

Sample Output 2

2
1
1

Sample Input 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

Sample Output 3

3
3
3
3
3
3
3
3
I - Vacation Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

0, 1 からなる長さ N の文字列 S が与えられます。Si 文字目を S_i とします。

Q 個のクエリを与えられた順に処理してください。
クエリは 3 個の整数の組 (c, L, R) で表されて、c の値によってクエリの種類が異なります。

  • c=1 のとき : L \leq i \leq R を満たす整数 i について、S_i1 ならば 0 に、0 ならば 1 に変更する。
  • c=2 のとき : SL 文字目から R 文字目までを取り出して出来る文字列を T とする。T に含まれる連続する 1 の長さの最大値を出力する。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq Q \leq 10^5
  • S は長さ N0, 1 からなる文字列
  • c \in \lbrace 1, 2 \rbrace
  • 1 \leq L \leq R \leq N
  • N, Q, c, L, R は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは次の形式で与えられる。

c L R

出力

c=2 であるクエリの個数を k として、k 行出力せよ。
i 行目には i 番目の c=2 であるクエリに対する答えを出力せよ。


入力例 1

7 6
1101110
2 1 7
2 2 4
1 3 6
2 5 6
1 4 7
2 1 7

出力例 1

3
1
0
7

クエリを順に処理すると次のようになります。

  • はじめ、S= 1101110 です。
  • 1 番目のクエリについて、T = 1101110 です。T に含まれる連続する 1 の中で最も長いものは、T4 文字目から 6 文字目からなる 111 なので、答えは 3 になります。
  • 2 番目のクエリについて、T = 101 です。T に含まれる連続する 1 の中で最も長いものは、T1 文字目あるいは 3 文字目の 1 なので、答えは 1 になります。
  • 3 番目のクエリについて、操作後の S1110000 です。
  • 4 番目のクエリについて、T = 00 です。T1 は含まれないので答えは 0 になります。
  • 5 番目のクエリについて、操作後の S1111111 です。
  • 6 番目のクエリについて、T = 1111111 です。T に含まれる連続する 1 の中で最も長いものは、T1 文字目から 7 文字目からなる 1111111 なので、答えは 7 になります。

Score : 550 points

Problem Statement

You are given a string S of length N consisting of 0 and 1. Let S_i denote the i-th character of S.

Process Q queries in the order they are given.
Each query is represented by a tuple of three integers (c, L, R), where c represents the type of the query.

  • When c=1: For each integer i such that L \leq i \leq R, if S_i is 1, change it to 0; if it is 0, change it to 1.
  • When c=2: Let T be the string obtained by extracting the L-th through R-th characters of S. Print the maximum number of consecutive 1s in T.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq Q \leq 10^5
  • S is a string of length N consisting of 0 and 1.
  • c \in \lbrace 1, 2 \rbrace
  • 1 \leq L \leq R \leq N
  • N, Q, c, L, and R are all integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i represents the i-th query:

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

c L R

Output

Let k be the number of queries with c=2. Print k lines.
The i-th line should contain the answer to the i-th query with c=2.


Sample Input 1

7 6
1101110
2 1 7
2 2 4
1 3 6
2 5 6
1 4 7
2 1 7

Sample Output 1

3
1
0
7

The queries are processed as follows.

  • Initially, S= 1101110.
  • For the first query, T = 1101110. The longest sequence of consecutive 1s in T is 111 from the 4-th character to 6-th character, so the answer is 3.
  • For the second query, T = 101. The longest sequence of consecutive 1s in T is 1 at the 1-st or 3-rd character, so the answer is 1.
  • For the third query, the operation changes S to 1110000.
  • For the fourth query, T = 00. T does not contain 1, so the answer is 0.
  • For the fifth query, the operation changes S to 1111111.
  • For the sixth query, T = 1111111. The longest sequence of consecutive 1s in T is 1111111 from the 1-st to 7-th characters, so the answer is 7.