Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
二つの文字 x と y は以下の 3 つの条件のうちどれか 1 つを満たすとき、似た文字と呼ばれます。
- x と y は同じ文字
- x と y の片方が
1で、もう片方がl - x と y の片方が
0で、もう片方がo
また、長さ N の文字列 S と T は以下の条件を満たすとき、似た文字列と呼ばれます。
- 任意の i\ (1\leq i\leq N) について、 S の i 番目の文字と T の i 番目の文字は似た文字
英小文字及び数字からなる長さ N の文字列 S,T が与えられます。 S と T が似た文字列か判定してください。
制約
- N は 1 以上 100 以下の整数
- S,T は英小文字及び数字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
S と T が似た文字列の場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
3 l0w 1ow
出力例 1
Yes
S の 1 文字目は lで、T の 1 文字目は 1です。これらは似た文字です。
S の 2 文字目は 0で、T の 2 文字目は oです。これらは似た文字です。
S の 3 文字目は wで、T の 3 文字目は wです。これらは似た文字です。
よって S と T は似た文字列です。
入力例 2
3 abc arc
出力例 2
No
S の 2 文字目は bで、T の 2 文字目は rです。これらは似た文字ではありません。
よって S と T は似た文字列ではありません。
入力例 3
4 nok0 n0ko
出力例 3
Yes
Score : 100 points
Problem Statement
Two characters x and y are called similar characters if and only if one of the following conditions is satisfied:
- x and y are the same character.
- One of x and y is
1and the other isl. - One of x and y is
0and the other iso.
Two strings S and T, each of length N, are called similar strings if and only if:
- for all i\ (1\leq i\leq N), the i-th character of S and the i-th character of T are similar characters.
Given two length-N strings S and T consisting of lowercase English letters and digits, determine if S and T are similar strings.
Constraints
- N is an integer between 1 and 100.
- Each of S and T is a string of length N consisting of lowercase English letters and digits.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print Yes if S and T are similar strings, and No otherwise.
Sample Input 1
3 l0w 1ow
Sample Output 1
Yes
The 1-st character of S is l, and the 1-st character of T is 1. These are similar characters.
The 2-nd character of S is 0, and the 2-nd character of T is o. These are similar characters.
The 3-rd character of S is w, and the 3-rd character of T is w. These are similar characters.
Thus, S and T are similar strings.
Sample Input 2
3 abc arc
Sample Output 2
No
The 2-nd character of S is b, and the 2-nd character of T is r. These are not similar characters.
Thus, S and T are not similar strings.
Sample Input 3
4 nok0 n0ko
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字・英小文字からなる空でない文字列 S が与えられます。以下の条件が満たされているか判定してください。
- S の先頭の文字は大文字であり、それ以外の文字はすべて小文字である。
制約
- 1 \leq |S| \leq 100(|S| は文字列 S の長さ)
- S の各文字は英大文字または英小文字である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
条件が満たされていれば Yes、そうでなければ No を出力せよ。
入力例 1
Capitalized
出力例 1
Yes
Capitalized の先頭の文字 C は大文字であり、それ以外の文字 apitalized はすべて小文字であるため、Yes を出力します。
入力例 2
AtCoder
出力例 2
No
AtCoder は先頭以外にも大文字 C を含むため、No を出力します。
入力例 3
yes
出力例 3
No
yes の先頭の文字 y は大文字でないため、No を出力します。
入力例 4
A
出力例 4
Yes
Score: 100 points
Problem Statement
You are given a non-empty string S consisting of uppercase and lowercase English letters. Determine whether the following condition is satisfied:
- The first character of S is uppercase, and all other characters are lowercase.
Constraints
- 1 \leq |S| \leq 100 (|S| is the length of the string S.)
- Each character of S is an uppercase or lowercase English letter.
Input
The input is given from Standard Input in the following format:
S
Output
If the condition is satisfied, print Yes; otherwise, print No.
Sample Input 1
Capitalized
Sample Output 1
Yes
The first character C of Capitalized is uppercase, and all other characters apitalized are lowercase, so you should print Yes.
Sample Input 2
AtCoder
Sample Output 2
No
AtCoder contains an uppercase letter C that is not at the beginning, so you should print No.
Sample Input 3
yes
Sample Output 3
No
The first character y of yes is not uppercase, so you should print No.
Sample Input 4
A
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号がついた N 人の選手がサッカーの試合をします。
選手が反則を犯したとき、その選手には イエローカード と レッドカード のどちらかが提示されます。
以下の条件のうち一方を満たした選手は 退場処分 と呼ばれるペナルティを受けます。
- イエローカードを累計 2 回提示される。
- レッドカードを提示される。
なお、退場処分を受けた選手にそれ以降カードが提示されることはありません。
あなたはこの試合を観戦します。はじめ、すべての選手はカードを 1 回も提示されていません。
Q 個のイベントが発生するので、イベントで聞かれる質問に正しく答えてください。
イベントは 3 種類あり、c x (c は 1, 2, 3 のいずれか) という形式で入力から与えられます。イベントの説明は次の通りです。
1 x: 選手 x にイエローカードが提示される。2 x: 選手 x にレッドカードが提示される。3 x: あなたは選手 x が退場処分を受けたかを質問される。選手 x が退場処分を受けていればYesと、そうでなければNoと答える。
制約
- 1 \leq N \leq 100
- 1 \leq Q \leq 100
- 全てのイベントにおいて 1 \leq x \leq N
- 3 種類目のイベントは少なくとも 1 個以上存在する
- すでに退場処分を受けた選手にカードが提示されることはない
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。ただし、\text{event}_i は i 番目に発生するイベントを意味する。
N Q
\text{event}_1
\text{event}_2
\vdots
\text{event}_Q
イベントは次の 3 つのいずれかの形式で入力される。
1 x
2 x
3 x
出力
入力で与えられる 3 種類目のイベントの個数を X として、X 行出力せよ。
i 行目には、3 種類目のイベントのうち i 番目のもので聞かれる質問について、選手 x が退場処分を受けていれば Yes を、そうでなければ No を出力せよ。
入力例 1
3 9 3 1 3 2 1 2 2 1 3 1 3 2 1 2 3 2 3 3
出力例 1
No No Yes No Yes No
イベントを時系列順にすべて説明すると次の通りです。
1 番目のイベントでは、あなたは選手 1 が退場処分を受けたかを質問されます。選手 1 は退場処分を受けていないので No を出力します。
2 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けていないので No を出力します。
3 番目のイベントでは、選手 2 にイエローカードが提示されます。
4 番目のイベントでは、選手 1 にレッドカードが提示されます。選手 1 は退場処分を受けます。
5 番目のイベントでは、あなたは選手 1 が退場処分を受けたかを質問されます。選手 1 は退場処分を受けたので Yes を出力します。
6 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けていないので No を出力します。
7 番目のイベントでは、選手 2 にイエローカードが提示されます。選手 2 は退場処分を受けます。
8 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けたので Yes を出力します。
9 番目のイベントでは、あなたは選手 3 が退場処分を受けたかを質問されます。選手 3 は退場処分を受けていないので No を出力します。
Score : 200 points
Problem Statement
N players numbered 1 to N will play a soccer game.
When a player commits an offense, that player will receive a yellow card or a red card.
A player who satisfies one of the following conditions will be removed from the game.
- Accumulates two yellow cards.
- Receives a red card.
Once a player is removed, that player will no longer receive any cards.
You will watch this game. Initially, the players have not received any cards.
There will be Q events. Correctly answer the questions asked in the events.
There are three kinds of events, which are given in the format c x from the input, where c is 1, 2, or 3. The events are as follows.
1 x: Player x receives a yellow card.2 x: Player x receives a red card.3 x: You are asked whether player x has been removed from the game. AnswerYesorNo.
Constraints
- 1 \leq N \leq 100
- 1 \leq Q \leq 100
- 1 \leq x \leq N in all events.
- There is at least one event of the third kind.
- A player who has been removed will no longer receive any cards.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \text{event}_i denotes the i-th event.
N Q
\text{event}_1
\text{event}_2
\vdots
\text{event}_Q
Each event is in one of the following formats:
1 x
2 x
3 x
Output
Print X lines, where X is the number of events of the third kind in the input.
The i-th line should contain Yes if, for the i-th event of the third kind, player x has been removed from the game, and No otherwise.
Sample Input 1
3 9 3 1 3 2 1 2 2 1 3 1 3 2 1 2 3 2 3 3
Sample Output 1
No No Yes No Yes No
Here are all the events in chronological order.
In the 1-st event, you are asked whether player 1 has been removed from the game. Player 1 has not been removed, so you should print No.
In the 2-nd event, you are asked whether player 2 has been removed from the game. Player 2 has not been removed, so you should print No.
In the 3-rd event, player 2 receives a yellow card.
In the 4-th event, player 1 receives a red card and is removed from the game.
In the 5-th event, you are asked whether player 1 has been removed from the game. Player 1 has been removed, so you should print Yes.
In the 6-th event, you are asked whether player 2 has been removed from the game. Player 2 has not been removed, so you should print No.
In the 7-th event, player 2 receives a yellow card and is removed from the game.
In the 8-th event, you are asked whether player 2 has been removed from the game. Player 2 has been removed, so you should print Yes.
In the 9-th event, you are asked whether player 3 has been removed from the game. Player 3 has not been removed, so you should print No.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号が付いた N 人のプレイヤーが総当たり戦をしました。この総当たり戦で行われた試合全てについて、二人の一方が勝ち、もう一方が負けました。
総当たり戦の結果は N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって以下の形式で与えられます。
-
i\neq j のとき、S_i の j 文字目は
o,xのいずれかであり、oのときプレイヤー i がプレイヤー j に勝ったことを、xのときプレイヤー i がプレイヤー j に負けたことを意味する。 -
i=j のとき、S_i の j 文字目は
-である。
総当たり戦で勝った試合数が多いほうが順位が上であり、勝った試合数が同じ場合は、プレイヤーの番号が小さいほうが順位が上となります。 N 人のプレイヤーの番号を順位が高い順に答えてください。
制約
- 2\leq N\leq 100
- N は整数
- S_i は
o,x,-からなる長さ N の文字列 - S_1,\ldots,S_N は問題文中の形式を満たす
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 人のプレイヤーの番号を、順位が高い順に空白区切りで出力せよ。
入力例 1
3 -xx o-x oo-
出力例 1
3 2 1
プレイヤー 1 は 0 勝、プレイヤー 2 は 1 勝、プレイヤー 3 は 2 勝なので、プレイヤーの番号は順位が高い順に 3,2,1 です。
入力例 2
7 -oxoxox x-xxxox oo-xoox xoo-ooo ooxx-ox xxxxx-x oooxoo-
出力例 2
4 7 3 1 5 2 6
プレイヤー 4 とプレイヤー 7 はどちらも 5 勝ですが、プレイヤー番号が小さいプレイヤー 4 のほうが順位が上になります。
Score : 200 points
Problem Statement
There are N players numbered 1 to N, who have played a round-robin tournament. For every match in this tournament, one player won and the other lost.
The results of the matches are given as N strings S_1,S_2,\ldots,S_N of length N each, in the following format:
-
If i\neq j, the j-th character of S_i is
oorx.omeans that player i won against player j, andxmeans that player i lost to player j. -
If i=j, the j-th character of S_i is
-.
The player with more wins ranks higher. If two players have the same number of wins, the player with the smaller player number ranks higher. Report the player numbers of the N players in descending order of rank.
Constraints
- 2\leq N\leq 100
- N is an integer.
- S_i is a string of length N consisting of
o,x, and-. - S_1,\ldots,S_N conform to the format described in the problem statement.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the player numbers of the N players in descending order of rank.
Sample Input 1
3 -xx o-x oo-
Sample Output 1
3 2 1
Player 1 has 0 wins, player 2 has 1 win, and player 3 has 2 wins. Thus, the player numbers in descending order of rank are 3,2,1.
Sample Input 2
7 -oxoxox x-xxxox oo-xoox xoo-ooo ooxx-ox xxxxx-x oooxoo-
Sample Output 2
4 7 3 1 5 2 6
Both players 4 and 7 have 5 wins, but player 4 ranks higher because their player number is smaller.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 xの形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc なので 2 文字目の b を出力します。
2 個目のクエリのとき、S は abc から cab に変わります。
3 個目のクエリのとき、S は cab なので 2 文字目の a を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc, so the 2-nd character b should be printed.
In the 2-nd query, S is changed from abc to cab.
In the 3-rd query, S is cab, so the 2-nd character a should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。
- 魔法 A :箱の中にボールを 1 つ増やす
- 魔法 B :箱の中のボールの数を 2 倍にする
合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。
魔法以外の方法でボールの数を変化させることはできません。
制約
- 1 \leq N \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
A , B のみからなる文字列 S を出力せよ。
S の i 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。
S の長さは \mathbf{120} 以下でなければならない。
入力例 1
5
出力例 1
AABA
ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。
入力例 2
14
出力例 2
BBABBAAAB
ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。
Score : 300 points
Problem Statement
We have an empty box.
Takahashi can cast the following two spells any number of times in any order.
- Spell A: puts one new ball into the box.
- Spell B: doubles the number of balls in the box.
Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.
There is no way other than spells to alter the number of balls in the box.
Constraints
- 1 \leq N \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print a string S consisting of A and B.
The i-th character of S should represent the spell for the i-th cast.
S must have at most \mathbf{120} characters.
Sample Input 1
5
Sample Output 1
AABA
This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.
Sample Input 2
14
Sample Output 2
BBABBAAAB
This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14.
It is not required to minimize the length of S.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
高橋君はゲームをプレイしています。
ゲームは 1,2,\ldots,N の番号がついた N 個のステージからなり、現在はステージ 1 のみを遊ぶことができます。
各ステージ i ( 1\leq i \leq N-1 )が遊べるとき、ステージ i では以下の 2 つのどちらかの行動を行えます。
- A_i 秒掛けてステージ i をクリアする。ステージ i+1 を遊べるようになる。
- B_i 秒掛けてステージ i をクリアする。ステージ X_i を遊べるようになる。
各ステージをクリアするためにかかる時間以外は無視できるとき、ステージ N を遊べるようになるのは最短で何秒後ですか?
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i, B_i \leq 10^9
- 1 \leq X_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_{N-1} B_{N-1} X_{N-1}
出力
答えを出力せよ。
入力例 1
5 100 200 3 50 10 1 100 200 5 150 1 2
出力例 1
350
次のように行動することで、350 秒でステージ 5 を遊べるようになります。
- 100 秒掛けてステージ 1 をクリアし、ステージ 2 を遊べるようになる。
- 50 秒掛けてステージ 2 をクリアし、ステージ 3 を遊べるようになる。
- 200 秒掛けてステージ 3 をクリアし、ステージ 5 を遊べるようになる。
入力例 2
10 1000 10 9 1000 10 10 1000 10 2 1000 10 3 1000 10 4 1000 10 5 1000 10 6 1000 10 7 1000 10 8
出力例 2
90
入力例 3
6 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1
出力例 3
5000000000
Score: 425 points
Problem Statement
Takahashi is playing a game.
The game consists of N stages numbered 1,2,\ldots,N. Initially, only stage 1 can be played.
For each stage i ( 1\leq i \leq N-1 ) that can be played, you can perform one of the following two actions at stage i:
- Spend A_i seconds to clear stage i. This allows you to play stage i+1.
- Spend B_i seconds to clear stage i. This allows you to play stage X_i.
Ignoring the times other than the time spent to clear the stages, how many seconds will it take at the minimum to be able to play stage N?
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i, B_i \leq 10^9
- 1 \leq X_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_{N-1} B_{N-1} X_{N-1}
Output
Print the answer.
Sample Input 1
5 100 200 3 50 10 1 100 200 5 150 1 2
Sample Output 1
350
By acting as follows, you will be allowed to play stage 5 in 350 seconds.
- Spend 100 seconds to clear stage 1, which allows you to play stage 2.
- Spend 50 seconds to clear stage 2, which allows you to play stage 3.
- Spend 200 seconds to clear stage 3, which allows you to play stage 5.
Sample Input 2
10 1000 10 9 1000 10 10 1000 10 2 1000 10 3 1000 10 4 1000 10 5 1000 10 6 1000 10 7 1000 10 8
Sample Output 2
90
Sample Input 3
6 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1 1000000000 1000000000 1
Sample Output 3
5000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) と N 以下の正整数 K が与えられます。
i=1,2,\dots,N について次の問題を解いてください。
- A_i を含むように K 個の要素を A から選ぶ時、それらの最大公約数 (GCD) としてあり得る最大値を求めてください。
制約
- 1 \leq K \leq N \leq 1.2 \times 10^6
- 1 \leq A_i \leq 10^6
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
N 行出力せよ。j 行目には i=j の時の答えを出力せよ。
入力例 1
5 2 3 4 6 7 12
出力例 1
3 4 6 1 6
i=1 の時は A_1 と A_3 を選ぶと最大公約数が \gcd(\lbrace 3, 6 \rbrace) = 3 となり、これが最大です。
i=2 の時は A_2 と A_5 を選ぶと最大公約数が \gcd(\lbrace 4, 12 \rbrace) = 4 となり、これが最大です。
i=3 の時は A_3 と A_5 を選ぶと最大公約数が \gcd(\lbrace 6, 12 \rbrace) = 6 となり、これが最大です。
i=4 の時は A_4 と A_2 を選ぶと最大公約数が \gcd(\lbrace 7, 4 \rbrace) = 1 となり、これが最大です。
i=5 の時は A_5 と A_3 を選ぶと最大公約数が \gcd(\lbrace 12, 6 \rbrace) = 6 となり、これが最大です。
入力例 2
3 3 6 10 15
出力例 2
1 1 1
入力例 3
10 3 414003 854320 485570 52740 833292 625990 909680 885153 435420 221663
出力例 3
59 590 590 879 879 590 20 879 590 59
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and a positive integer K (at most N).
For each i = 1, 2, \dots, N, solve the following problem:
- When you choose K elements from A that include A_i, find the maximum possible GCD (greatest common divisor) of those chosen elements.
Constraints
- 1 \leq K \leq N \leq 1.2 \times 10^6
- 1 \leq A_i \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print N lines. The j-th line should contain the answer for i=j.
Sample Input 1
5 2 3 4 6 7 12
Sample Output 1
3 4 6 1 6
For i=1, choosing A_1 and A_3 yields \gcd(\lbrace 3,6 \rbrace) = 3, which is the maximum.
For i=2, choosing A_2 and A_5 yields \gcd(\lbrace 4,12 \rbrace) = 4, which is the maximum.
For i=3, choosing A_3 and A_5 yields \gcd(\lbrace 6,12 \rbrace) = 6, which is the maximum.
For i=4, choosing A_4 and A_2 yields \gcd(\lbrace 7,4 \rbrace) = 1, which is the maximum.
For i=5, choosing A_5 and A_3 yields \gcd(\lbrace 12,6 \rbrace) = 6, which is the maximum.
Sample Input 2
3 3 6 10 15
Sample Output 2
1 1 1
Sample Input 3
10 3 414003 854320 485570 52740 833292 625990 909680 885153 435420 221663
Sample Output 3
59 590 590 879 879 590 20 879 590 59
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。
N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。
条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
出力
答えを出力せよ。
入力例 1
3 1 2 3 2 1 3
出力例 1
3
例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。
条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\} の 3 通りです。
入力例 2
5 2 3 5 4 1 4 2 1 3 5
出力例 2
12
入力例 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
出力例 3
1
Score : 500 points
Problem Statement
There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).
How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.
Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P and Q are permutations of (1, 2, \dots, N).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
Output
Print the answer.
Sample Input 1
3 1 2 3 2 1 3
Sample Output 1
3
For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.
There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.
Sample Input 2
5 2 3 5 4 1 4 2 1 3 5
Sample Output 2
12
Sample Input 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Sample Output 3
1