実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。
1 以上 N 以下の整数 i 、1 以上 M 以下の整数 j について、S_i の j 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_i の j 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。
このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。
より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。
制約
- N は 2 以上 30 以下の整数
- M は 1 以上 30 以下の整数
- S_i は
o,xからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
出力例 1
5
参加者 1 と 2 のペア、参加者 1 と 3 のペア、参加者 1 と 4 のペア、参加者 1 と 5 のペア、参加者 2 と 3 のペアの 5 個のペアが条件を満たします。
例えば参加者 2 と 4 のペアは、問題 4 が解けないので条件を満たしません。
入力例 2
3 2 ox xo xx
出力例 2
1
入力例 3
2 4 xxxx oxox
出力例 3
0
Score : 200 points
Problem Statement
N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.
For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.
The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.
More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.
Constraints
- N is an integer between 2 and 30, inclusive.
- M is an integer between 1 and 30, inclusive.
- S_i is a string of length M consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
Sample Output 1
5
The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.
On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.
Sample Input 2
3 2 ox xo xx
Sample Output 2
1
Sample Input 3
2 4 xxxx oxox
Sample Output 3
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人が 1 列に並んでおり、前から i 番目に並んでいる人は人 P_i です。
Q 個のクエリを処理して下さい。i 番目のクエリは以下のものです。
- 整数 A_i,B_i が与えられる。人 A_i と人 B_i のうち、より前に並んでいる人の番号を出力せよ。
制約
- 入力は全て整数
- 1\leq N\leq 100
- 1\leq P_i\leq N
- P_i \neq P_j\ (i\neq j)
- 1\leq Q \leq 100
- 1\leq A_i<B_i\leq N
入力
入力は以下の形式で標準入力から与えられる。
N P_1 \ldots P_N Q A_1 B_1 \vdots A_Q B_Q
出力
Q 行出力せよ。i 行目には、i 番目のクエリの答えを出力せよ。
入力例 1
3 2 1 3 3 2 3 1 2 1 3
出力例 1
2 2 1
1 番目のクエリでは、人 2 は前から 1 番目、人 3 は前から 3 番目なので、人 2 がより前にいます。
2 番目のクエリでは、人 1 は前から 2 番目、人 2 は前から 1 番目なので、人 2 がより前にいます。
3 番目のクエリでは、人 1 は前から 2 番目、人 3 は前から 3 番目なので、人 1 がより前にいます。
入力例 2
7 3 7 2 1 6 5 4 13 2 3 1 2 1 3 3 6 3 7 2 4 3 7 1 3 4 7 1 6 2 4 1 3 1 3
出力例 2
3 2 3 3 3 2 3 3 7 1 2 3 3
Score: 200 points
Problem Statement
There are N people standing in a line. The person standing at the i-th position from the front is person P_i.
Process Q queries. The i-th query is as follows:
- You are given integers A_i and B_i. Between person A_i and person B_i, print the person number of the person standing further to the front.
Constraints
- All inputs are integers.
- 1 \leq N \leq 100
- 1 \leq P_i \leq N
- P_i \neq P_j\ (i \neq j)
- 1 \leq Q \leq 100
- 1 \leq A_i < B_i \leq N
Input
The input is given from Standard Input in the following format:
N P_1 \ldots P_N Q A_1 B_1 \vdots A_Q B_Q
Output
Print Q lines. The i-th line should contain the response for the i-th query.
Sample Input 1
3 2 1 3 3 2 3 1 2 1 3
Sample Output 1
2 2 1
In the first query, person 2 is at the first position from the front, and person 3 is at the third position, so person 2 is further to the front.
In the second query, person 1 is at the second position from the front, and person 2 is at the first position, so person 2 is further to the front.
In the third query, person 1 is at the second position from the front, and person 3 is at the third position, so person 1 is further to the front.
Sample Input 2
7 3 7 2 1 6 5 4 13 2 3 1 2 1 3 3 6 3 7 2 4 3 7 1 3 4 7 1 6 2 4 1 3 1 3
Sample Output 2
3 2 3 3 3 2 3 3 7 1 2 3 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
S の空でない部分文字列であって、1 種類の文字のみからなるものの数を求めてください。 ただし、文字列として等しい部分文字列同士は、取り出し方が異なっても区別しません。
なお、S の空でない部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のうち、長さが 1 以上であるもののことをいいます。
例えば、ab や abc は abc の空でない部分文字列ですが、ac や空文字列は abc の空でない部分文字列ではありません。
制約
- 1 \leq N \leq 2\times 10^5
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の空でない部分文字列であって、1 種類の文字のみからなるものの数を出力せよ。
入力例 1
6 aaabaa
出力例 1
4
S の空でない部分文字列であって、1 種類の文字のみからなるものは a, aa, aaa, b の 4 つです。
S から a や aa を取り出す方法は 1 通りではありませんが、それぞれ 1 回ずつしか数えないことに注意してください。
入力例 2
1 x
出力例 2
1
入力例 3
12 ssskkyskkkky
出力例 3
8
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Find the number of non-empty substrings of S that are repetitions of one character. Here, two substrings that are equal as strings are not distinguished even if they are obtained differently.
A non-empty substring of S is a string of length at least one obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab and abc are non-empty substrings of abc, while ac and the empty string are not.
Constraints
- 1 \leq N \leq 2\times 10^5
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the number of non-empty substrings of S that are repetitions of one character.
Sample Input 1
6 aaabaa
Sample Output 1
4
The non-empty substrings of S that are repetitions of one character are a, aa, aaa, and b; there are four of them. Note that there are multiple ways to obtain a or aa from S, but each should only be counted once.
Sample Input 2
1 x
Sample Output 2
1
Sample Input 3
12 ssskkyskkkky
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
黒板に整数 N が 1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。
- 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
- 黒板から x を 1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
- この一連の操作を行うために高橋君は x 円払う必要がある。
ここで \lfloor a \rfloor は a 以下の整数のうち最大のものを、\lceil a \rceil は a 以上の整数のうち最小のものを意味します。
操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。
制約
- 2 \leq N \leq 10^{17}
入力
入力は以下の形式で標準入力から与えられる。
N
出力
高橋君が払った金額の総和が何円であるかを出力せよ。
入力例 1
3
出力例 1
5
高橋君が行う操作の一例を挙げると次のようになります。
- はじめ、黒板には 3 が 1 個書かれている。
- 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 3 を 1 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
- 黒板には 2 が 1 個と 1 が 1 個書かれている。
- 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 2 を 1 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
- 黒板には 1 が 3 個書かれている。
- 黒板から 2 以上の整数が全て無くなったので操作を終了する。
操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。
入力例 2
340
出力例 2
2888
入力例 3
100000000000000000
出力例 3
5655884811924144128
Score: 300 points
Problem Statement
There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:
- Choose one integer x not less than 2 written on the blackboard.
- Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
- Takahashi must pay x yen to perform this series of operations.
Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.
What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.
Constraints
- 2 \leq N \leq 10^{17}
Input
The input is given from Standard Input in the following format:
N
Output
Print the total amount of money Takahashi will have paid, in yen.
Sample Input 1
3
Sample Output 1
5
Here is an example of how Takahashi performs the operations:
- Initially, there is one 3 written on the blackboard.
- He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
- There is one 2 and one 1 written on the blackboard.
- He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
- There are three 1s written on the blackboard.
- Since all integers not less than 2 have been removed from the blackboard, the process is finished.
Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.
Sample Input 2
340
Sample Output 2
2888
Sample Input 3
100000000000000000
Sample Output 3
5655884811924144128
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
非負整数 X を 10 進表記(先行ゼロ無し)で表した文字列が回文である時、X を回文数と呼びます。
例えば 363, 12344321, 0 はいずれも回文数です。
小さい方から N 番目の回文数を求めてください。
制約
- 1 \leq N \leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
小さい方から N 番目の回文数を出力せよ。
入力例 1
46
出力例 1
363
小さい方から 46 番目の回文数は 363 です。
入力例 2
1
出力例 2
0
入力例 3
1000000000000000000
出力例 3
90000000000000000000000000000000009
Score : 350 points
Problem Statement
A non-negative integer X is called a palindrome number if its decimal representation (without leading zeros) is a palindrome.
For example, 363, 12344321, and 0 are all palindrome numbers.
Find the N-th smallest palindrome number.
Constraints
- 1 \leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the N-th smallest palindrome number.
Sample Input 1
46
Sample Output 1
363
The 46th smallest palindrome number is 363.
Sample Input 2
1
Sample Output 2
0
Sample Input 3
1000000000000000000
Sample Output 3
90000000000000000000000000000000009