A - A Barricade

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 66

問題文

ついに夏休みが終わり、新学期が始まった. 京大生のあなたがいつものように大学に来ると、これから教室が何者かによってバリケード封鎖されるという噂を耳にした。 バリケードは A 時限目の始まる直前に設置され、 B 時限目の始まる直前に京大生によって破壊される見通しだそうだ。 バリケードが設置されている時間は教室に出入りできないため、その時間中の授業は中止され、出席できないだろう。 あなたは今日、 N 個の授業を履修しており、それぞれの授業は t_i (1 \leq i \leq N) 時限目にあり、同じ時限にある授業を複数履修していることはない。 あなたは今日、いくつの授業に出席することができるだろうか。

制約

  • 1 \leq N \leq 1000
  • 1 \leq A < B \leq 10^9
  • 1 \leq t_i \leq 10^9
  • t_i に同じ値が二度以上出現することはない。

入力

入力の1行目には N, A, B が空白区切りで与えられ、 i+1 行目に t_i が与えられる。

N A B
t1
:
tN

出力

出席できる授業の数を1行で出力せよ。


入力例1

5 5 9
4
3
6
9
1

出力例1

4

3 番目の授業にだけ出席できない。

入力例2

5 4 9
5
6
7
8
9

出力例2

1

5 番目の授業にだけ出席できる。

入力例3

4 3 6
9
6
8
1

出力例3

4

全ての授業に出席できる。

入力例4

2 1 2
1
2

出力例4

1

1 番目の授業には出席できないが、2 番目の授業には出席できる。

Score : 66 points

Problem Statement

Summer vacation ended at last and the second semester has begun. You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom. The barricade will be built just before the start of the A-th class and removed by Kyoto University students just before the start of the B-th class. All the classes conducted when the barricade is blocking the entrance will be cancelled and you will not be able to attend them. Today you take N classes and class i is conducted in the t_i-th period. You take at most one class in each period. Find the number of classes you can attend.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq A < B \leq 10^9
  • 1 \leq t_i \leq 10^9
  • All t_i values are distinct.

Input

N, A and B are given on the first line and t_i is given on the (i+1)-th line.

N A B
t1
:
tN

Output

Print the number of classes you can attend.


Sample Input 1

5 5 9
4
3
6
9
1

Sample Output 1

4

You can not only attend the third class.

Sample Input 2

5 4 9
5
6
7
8
9

Sample Output 2

1

You can only attend the fifth class.

Sample Input 3

4 3 6
9
6
8
1

Sample Output 3

4

You can attend all the classes.

Sample Input 4

2 1 2
1
2

Sample Output 4

1

You can not attend the first class, but can attend the second.


Source Name

KUPC2016
B - Problem Committee

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 100

問題文

京都大学プログラミングコンテストは、京都大学の学生が有志で主催するプログラミングコンテストです。 Kyoto University Programming Contest の略で KUPC などと呼ばれています。

引用元:京都大学プログラミングコンテスト ご案内

今年もKUPCを開催するために作問委員会が開かれ、そこで N 問の問題が提案された。 それぞれの問題は順に 1 から N まで番号付けられており、 i 番目の問題の名前は P_i である。 ただ、提案された問題数が多すぎたため、KUPCを複数回に分けて出題することにした。

そこで、以下の条件を満たすように問題を選んで、KUPCを複数回に分けることとした。

  • 1回のKUPCでは K 問の問題を出題する。
  • 各問題が出題される回数は、開催される全てのKUPCを通して高々1回である。
  • 1回のKUPCに出題する K 問の問題の名前の頭文字が全て異なるように出題する。

作問委員の1人であるあなたは、KUPCが開催される回数をできるだけ多くしたいと考えた。 最大で何回のKUPCを開催することができるかを求めよ。

制約

  • 1 \leq N \leq 10^4
  • 1 \leq K \leq 26
  • 1 \leq |P_i| \leq 10
  • P_i に含まれる文字の種類は半角アルファベット大文字のみ

ただし、全ての問題 i, j (1 \leq i < j \leq N) に対して P_i \neq P_j となるとは限らない。


入力

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

N K
P_1
:
P_N

出力

KUPCを最大で何回開催することができるかを1行で出力せよ。


入力例1

9 3
APPLE
ANT
ATCODER
BLOCK
BULL
BOSS
CAT
DOG
EGG

例えば、以下のように問題を選べば、 3 回KUPCを開催できる。

  • 1 回目: APPLE, BLOCK, CAT
  • 2 回目: ANT, BULL, DOG
  • 3 回目: ATCODER, BOSS, EGG

出力例1

3

入力例2

3 2
KU
KYOUDAI
KYOTOUNIV

出力例2

0

一度もKUPCを開催できない。

Score : 100 points

Problem Statement

Kyoto University Programming Contest is a programming contest voluntarily held by some Kyoto University students. This contest is abbreviated as Kyoto University Programming Contest and called KUPC.

source: Kyoto University Programming Contest Information

The problem-preparing committee met to hold this year's KUPC and N problems were proposed there. The problems are numbered from 1 to N and the name of i-th problem is P_i. However, since they proposed too many problems, they decided to divide them into some sets for several contests.

They decided to divide this year's KUPC into several KUPCs by dividing problems under the following conditions.

  • One KUPC provides K problems.
  • Each problem appears at most once among all the KUPCs.
  • All the first letters of the problem names in one KUPC must be different.

You, one of the committee members, want to hold as many KUPCs as possible. Write a program to find the maximum number of KUPCs that can be held this year.

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq K \leq 26
  • 1 \leq |P_i| \leq 10
  • All characters in P_i are capital letters.

Note that, for each i and j (1 \leq i < j \leq N), P_i \neq P_j are not necessarily satisfied.


Input

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

N K
P_1
:
P_N

Output

Print the maximum number of KUPCs that can be held on one line.


Sample Input 1

9 3
APPLE
ANT
ATCODER
BLOCK
BULL
BOSS
CAT
DOG
EGG

For example, three KUPCs can be held by dividing the problems as follows.

  • First: APPLE, BLOCK, CAT
  • Second: ANT, BULL, DOG
  • Third: ATCODER, BOSS, EGG

Sample Output 1

3

Sample Input 2

3 2
KU
KYOUDAI
KYOTOUNIV

Sample Output 2

0

No KUPC can be held.


Source Name

KUPC2016
C - Cookie Breeding Machine

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 100

問題文

クッキー好きの学生たちのために、ある教授がクッキー増殖装置を発明した。

この装置に美味しさ x のクッキー1枚を投入し、0 以上 127 以下の整数 y を入力すると、 投入したクッキーを消費して、美味しさ y と美味しさ (x XOR y) の2枚のクッキーが生成される。

ここで、XORはビットごとの排他的論理和を表す。

最初、クッキーは1枚だけあり、美味しさは D である。

以下の操作を N-1 回繰り返して生成された、ちょうど N 枚のクッキーの美味しさの合計の最大値を出力せよ。

  1. 今あるクッキーのうちいずれか1枚を選んで装置に投入する。
  2. 0 以上 127 以下の整数から自由に1つ選んで装置に入力する。

制約

  • 1 \leq T \leq 1000
  • 1 \leq N_t \leq 1000 (1 \leq t \leq T)
  • 1 \leq D_t \leq 127 (1 \leq t \leq T)

入力

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

T
N_1 D_1
:
N_T D_T

入力は複数のテストケースからなる。1 行目には、テストケースの個数を表す整数 T が与えられる。
続く T 行にテストケースが1つずつ与えられる。
t ( 1 \leq t \leq T ) 番目のテストケースでは、 半角スペース区切りで最終的なクッキーの枚数を表す整数 N_t と最初のクッキーの美味しさ D_t が与えられる。

出力

最終生成物となる N 枚クッキーの美味しさの合計の最大値を標準出力に1行で出力せよ。


入力例1

3
3 1
4 108
1 10

出力例1

255
400
10

1 つ目のテストケースでは、以下の手順で装置を使用すると、最終的に、美味しさ 61, 95, 993 枚のクッキーが生成され、美味しさの合計が最大であるため、255 と出力する。

  1. 美味しさ 1 のクッキーを投入して消費し、装置に 60 を入力すると、美味しさ 60 のクッキーと美味しさ 61 のクッキーが生成される。
  2. 美味しさ 60 のクッキーを投入して消費し、装置に 99 を入力すると、美味しさ 99 のクッキーと美味しさ 95 のクッキーが生成される。

また、 3 つ目のテストケースのように、装置を使用しないこともある。

Score : 100 points

Problem Statement

A professor invented Cookie Breeding Machine for his students who like cookies very much.

When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine, it consumes the cookie and generates two cookies with the taste of y and (x XOR y).

Here, XOR represents Bitwise Exclusive OR.

At first, there is only one cookie and the taste of it is D .

Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times.

  1. Put one of the cookies into the machine.
  2. Input a non-negative integer less than or equal to 127 on the machine.

Constraints

  • 1 \leq T \leq 1000
  • 1 \leq N_t \leq 1000 (1 \leq t \leq T)
  • 1 \leq D_t \leq 127 (1 \leq t \leq T)

Input

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

T
N_1 D_1
:
N_T D_T

The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1.
Each test case is given on the next T lines.
In the t-th test case ( 1 \leq t \leq T ), N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space.

Output

For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line.


Sample Input 1

3
3 1
4 108
1 10

Sample Output 1

255
400
10

On the 1st test case, if the machine is used as follows, three cookies with the taste of 61, 95 and 99 are generated. Since the sum of these values is maximum among all possible ways, the answer is 255.

  1. Put the cookie with the taste of 1 and input an integer 60 on the machine, lose the cookie and get two cookies with the taste of 60 and 61.
  2. Put the cookie with the taste of 60 and input an integer 99 on the machine, lose the cookie and get two cookies with the taste of 99 and 95.

On the 3rd test case, the machine may not be used.


Source Name

KUPC2016
D - Long Blackboard

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 150

問題文

京都大学のとある教室には縦に2行、横に N 列の黒板が設置されている。 あまりに長く連なっているため、どの黒板がすでに使用されていて、 どの黒板が未使用なのかを人間が全て把握するのは困難である。

そこで最近、黒板検索装置が導入された。 W を任意の整数として、 この装置に縦2行、横 W 列の黒板の使用状況を検索クエリとして入力すると、 そのような使用状況が部分黒板列として黒板の中に存在しているかが判定される。 ここで、使用状況に対して、ある i < j が存在して 黒板の i 列から j 列までが使用状況と一致するとき。 その使用状況は、その黒板の部分黒板列であるという。

この教室での発表を控えたあなたは滞りなく発表が進むよう、 この黒板検索装置を使って黒板全体の使用状況を特定するプログラムを書くことにした。 黒板検索装置の検索には多少時間がかかるので検索回数はなるべく少なくしたい。

黒板全体の使用状況は最初に決まっており、途中で変化することはない。


入出力

初めの入力は以下で与えられる。

N

N (1 \leq N \leq 100) は黒板の長さを表す整数である。

この入力の後に、解答プログラムは黒板検索装置への検索クエリを出力せよ。 検索クエリは次の形式で表される。

s_1
s_2

ここで s_1 は部分黒板列の上の段、 s_2 は部分黒板列の下の段を表す文字列である。 ただし s_1,s_2 は、すでに使われている黒板を # 、未使用の黒板を . で表した文字列である。 文字列 s_1,s_2 の長さは任意だが一致していなければならない。 末尾には改行を出力せよ。

次に、黒板検索装置の検索結果を表す文字列が以下の形式で与えられる。

r

rT または F である。 意味は次の通りである。

  • T のとき、直前の検索クエリの部分黒板列が黒板に存在する。
  • F のとき、直前の検索クエリの部分黒板列が黒板に存在しない。

直前の検索クエリが黒板全体となっていた場合や 検索回数の上限を超えた場合は r の代わりに文字列 end が与えられる。 この文字列を受け取った場合,即座に解答プログラムを終了せよ。 検索回数の上限以内で,黒板全体を検索クエリとして出力した場合, Accepted と判定される. ただし,そのときの検索クエリも検索回数に含めて数える.

各出力の後には,出力をフラッシュする必要があることに注意せよ。例えば C/C++ では 検索クエリ s1, s2

printf("%s\n%s\n", s1, s2); fflush(stdout);

と出力すればよい。 黒板検索装置から与えられる入力は最後まで受け取ること。受け取らなかった場合、Time Limit Exceeded の結果が得られることがあるため、注意すること。


検索回数の上限

検索回数の上限は 420 回である。 420 回以内に黒板全体となる検索クエリが与えられなかった場合は Query Limit Exceeded となる。


入出力例

以下では N=3 で黒板が

.#.
...

である場合を示している。実際には黒板の状態は解答プログラム側からは分からないことに注意せよ。

解答プログラムの出力 解答プログラムへの入力 説明
3 黒板の長さが与えられる
..
##
検索クエリを出力
F 先ほどの部分黒板列は黒板の中に存在しない
.
.
検索クエリを出力
T 先ほどの部分黒板列は黒板の中に存在する
..
..
検索クエリを出力
F 先ほどの部分黒板列は黒板の中に存在しない
.#
..
検索クエリを出力
T 先ほどの部分黒板列は黒板の中に存在する
.#.
...
検索クエリを出力
end 先ほどの部分黒板列は黒板全体となっていたので終了

Score : 150 points

Problem Statement

There is a long blackboard with 2 rows and N columns in a classroom of Kyoto University. This blackboard is so long that it is impossible to tell which cells are already used and which unused.

Recently, a blackboard retrieval device was installed at the classroom. To use this device, you type a search query that forms a rectangle with 2 rows and any length of columns, where each cell is used or unused. When you input a query, the decive answers whether the rectangle that corresponds to the query exists in the blackboard. Here, for a rectangle that corresponds to a search query, if two integer i, j ( i < j ) exist and the rectangle equals to the partial blackboard between column i and j , the rectangle is called a sub-blackboard of the blackboard.

You are currently preparing for a presentation at this classroom. To make the presentation go well, you decided to write a program to detect the status of the whole blackboard using the retrieval device. Since it takes time to use the device, you want to use it as few times as possible.

The status of the whole blackboard is already determined at the beginning and does not change while you are using the device.


Input

The first input is given in the following format:

N

N (1 \leq N \leq 100) is an integer that represents the length of the blackboard.

After this input value, your program must print search queries. A search query has the following format.

s_1
s_2

Here, s_1 represents the upper part of the blackboard and s_2 represents the lower. # in s_1 and s_2 represents the cell is already used and . represents the cell is still unused. The lengths of s_1 and s_2 are arbitrary, but they must be the same. Make sure to insert a line break at the end of the lines.

Every time your program prints a search query, a string that represents the search result of the device is returned in the followin format.

r

r is either T or F . The meaning of each character is as follows.

  • T represents that the sub-blackboard that corresponds to the search query exists in the blackboard.
  • F represents that the sub-blackboard that corresponds to the search query does not exist in the blackboard.

If the search query equals to the whole blackboard or the number of the search queries exceeds the limit, string end is given instead of r . Once you receive this string, exit your program immediately. If your program prints the whole blackboard as a search query before exceedin the limit, it is judged as Accepted. Note that the search query that represents the whole blackboard is also counted as the number of search queries.

Note that the output needs to be flushed every time the output is printed. For example, In C/C++, search query s1, s2 can be printed as follows.

printf("%s\n%s\n", s1, s2); fflush(stdout);

Make sure your program receive all the input from the device. Otherwise, the result may be Time Limit Exceeded .


Query Limit

The maximun number of search queries is 420. If the number of queries exceeds the limit, the result will be Query Limit Exceeded .


Sample Input and Output

The following is an example where N=3 and the blackboard is as follows.

.#.
...

Note that your program does not know the state of the blackboard.

Output of your program Input to your program Explanation
3 The length of the blackboard is given
..
##
Output a search query
F The sub-blackboard does not exist
.
.
Output a search query
T The sub-blackboard exists
..
..
Output a search query
F The sub-blackboard does not exist
.#
..
Output a search query
T The sub-blackboard exists
.#.
...
Output a search query
end Exit your program because the above sub-blackboard equals to the whole blackboard.

Source Name

KUPC2016
E - Fences

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 150

問題文

H マス横 W マスのグリッドのいくつかのマスにヤギがいる。 ふわりは、ヤギのいないいくつかのマスに柵を設置して、どのヤギも、移動をくりかえしてグリッドの外に出ることができないようにしたい。 ヤギは上下左右の隣接する柵のないマスに移動することができる。 ヤギがグリッドの端の行や列にいるときは、グリッドの外に移動することができる。 柵の設置が終わるまでヤギは動かないとする。 目的を達成するのに必要な柵の最小個数を求めよ。

制約

  • 1 \leq H \leq 100
  • 1 \leq W \leq 100
  • グリッドの少なくとも 1 マスにはヤギが存在する。

入力

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

H W
S_1
:
S_H

S_i (1 \leq i \leq H)j (1 \leq j \leq W) 文字目はグリッドの上から i 行目、左から j 列目の状態を表す。 . のとき、このマスには何もないことを表す。 X のとき、このマスにヤギがいることを表す。

出力

設置する必要のある柵の最小個数を一行で出力せよ。いくつ柵を設置してもグリッドの外に出ることができるヤギが存在するときは -1 を出力せよ。


入力例1

4 5
.....
.....
..X..
.....

出力例1

4

3 行目の 2 列目と 2 行目の 3 列目と 3 行目の 4 列目と 4 行目の 3 列目に設置すればよい。

入力例2

2 2
..
.X

出力例2

-1

この例ではどのようにしてもヤギはグリッドの外へ出ることができる。

入力例3

6 6
......
......
..X...
.X..X.
..X...
......

出力例3

10

Score : 150 points

Problem Statement

There are some goats on a grid with H rows and W columns. Alice wants to put some fences at some cells where goats do not exist so that no goat can get outside the grid. Goats can move in the four directions, that is, up, down, right and left. Goats can not move onto the cells where fences are placed. If a goat exists at one of the outermost cells in the grid, it can move outside. Goats do not move until all fences are placed. Find the minimum number of fences to be placed.

Constraints

  • 1 \leq H \leq 100
  • 1 \leq W \leq 100
  • There is at least one goat on the given grid.

Input

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

H W
S_1
:
S_H

The j-th (1 \leq j \leq W) character in S_i (1 \leq i \leq H) represents whether a goat exists at the cell located at row i and column j. Character . represents there is no goat, and X represents there is one goat at the cell.

Output

Print the minimun number of fences to be placed. If there is no way to keep all the goats inside the grid, print -1.


Sample Input 1

4 5
.....
.....
..X..
.....

Sample Output 1

4

The optimum answer is to put fences at the cells located at row 3 and column 2, row 2 and column 3, row 3 and column 4, and row 4 and column 3.

Sample Input 2

2 2
..
.X

Sample Output 2

-1

In this case, there is no way to keep the goats inside the grid.

Sample Input 3

6 6
......
......
..X...
.X..X.
..X...
......

Sample Output 3

10

Source Name

KUPC2016
F - Speed Solving

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 150

問題文

京都大学で飼育されているゴリラ達は数学が得意である。 彼らは今、 2 つの関数 _^ を含む式の値を求める問題を解いている。 これらの関数は 2 入力関数であり、 _ 関数は入力の小さい方の値を、 ^ 関数は大きい方の値を出力する。 ゴリラ達は、式の中に現れる整数が 0 以上 99 以下であることは知っているが、式の長さは終端を表す記号 ? を読むまでわからない。 それぞれの式に含まれる文字数は 1000 文字以下であるが、そのことも知らない。 優等生ゴリラのアイちゃんは、 式を全て読まなくても式の値がわかることがあることに気づいた。

例えば、

^(41,3)?

という式を左から順に読んでいくと、 6 文字目まで読んだ時点、 つまり

^(41,3

まで読んだ時点で、関数の2番目の入力が3 もしくは 30 以上 39 以下であることがわかるため、式の値は 41 であることが確定する。

アイちゃんは他のゴリラより早く問題を解きたいので、先頭から1文字ずつ読んでいき、式の値が分かった時点でその問題を読むのをやめることにした。 それぞれの式について、式の値とアイちゃんが読む必要のある文字数の最小値を求めよ。

制約

  • 1 \leq Q \leq 200
  • それぞれの式に含まれる文字数は 1000 以下である。

入力

入力は複数のテストケースからなり、以下の形式で標準入力から与えられる。

Q
statement_1
...
statement_Q

statement_i (1 \leq i \leq Q) は、以下の BNF の形式で与えられる。

<statement> ::= <expression> ?
<expression> ::= (^ | _)  ( <expression> , <expression> ) | <number> 
<number> :: = 0 | 1 | 2 | ... | 98 | 99

出力

出力は Q 行からなる。 i (1 \leq i \leq Q) 行目には i 番目のテストケースにおける式の値と、読む必要のある文字数の最小値を空白区切りで出力せよ。


入力例1

4
_(4,51)?
^(99,_(3,67))?
_(0,87)?
3?

出力例1

4 5
99 4
0 3
3 2
  • 1 番目の例では、 5 文字目を読んだ時点、つまり _(4,5 の時点で式の値が 4 であるとわかる。
  • 2 番目の例では、 4 文字目を読んだ時点、つまり ^(99 の時点で式の値が 99 であるとわかる。
  • 3 番目の例では、 3 文字目を読んだ時点、つまり _(0 の時点で式の値が 0 であるとわかる。
  • 4 番目の例では、 終端記号を読み込むまで式の値が 3 であるとわからない。

入力例2

7
_(23,^(_(22,40),4))?
_(0,99)?
^(99,_(^(19,2),5))?
_(^(43,20),^(30,29))?
^(_(20,3),_(50,41))?
^(_(20,3),_(3,41))?
^(_(20,3),_(4,41))?

出力例2

22 18
0 3
99 4
30 17
41 17
3 14
4 15

Score : 150 points

Problem Statement

Gorillas in Kyoto University are good at math. They are currently trying to solve problems to find the value of an expression that contains two functions, _ , ^. Each of these functions takes two input values. _ function returns the smaller of the two input values and ^ function returns the larger. Gorillas know that integers in the expression are non-negative and less than or equal to 99, but can not find out the length of the expression until they read a terminal symbol ? that represents the end of the expression. The number of characters included in each expression is less than or equal to 1000, but they do not even know this fact. Ai, a smart gorilla, noticed that she may be able to know the value of the expression even if they don't read the whole expression.

For example,

Assume you read the following sentence from the left.

^(41,3)?

When you read the sixth character, that is, when you read the following expression,

^(41,3

you can tell the second input value of the funcion is whether 3 or an integer between 30 and 39, and the value turns out 41.

Since Ai wants to solve problems earlier than other gorillas, she decided to solve the problems such that she reads as fewer characters as possible from the left. For each expression, Find the value of the expression and the minimum number of characters Ai needs to read to know the value.

Constraints

  • 1 \leq Q \leq 200
  • The number of characters each expression contains is less than or equal to 1000.

Input

The input consists of multiple test cases and is given from Standard Input in the following format:

Q
statement_1
...
statement_Q

Each statement_i (1 \leq i \leq Q) is given in the following BNF format.

<statement> ::= <expression> ?
<expression> ::= (^ | _)  ( <expression> , <expression> ) | <number> 
<number> :: = 0 | 1 | 2 | ... | 98 | 99

Output

Output consists of Q lines. On line i (1 \leq i \leq Q), print the value of the expression and the number of character Ai needs to read for the test case i separated by space.


Sample Input 1

4
_(4,51)?
^(99,_(3,67))?
_(0,87)?
3?

Sample Output 1

4 5
99 4
0 3
3 2
  • For the first test case, when you read the fifth character, that is, when you read _(4,5, you will know the value is 4.
  • For the second test case, when you read the fourth character, that is, when you read ^(99, you will know the value is 99.
  • For the third test case, when you read the third character, that is, when you read _(0, you will know the value is 0.
  • For the fourth test case, you will not know the value is 3 untill you read the terminal symbol.

Sample Input 2

7
_(23,^(_(22,40),4))?
_(0,99)?
^(99,_(^(19,2),5))?
_(^(43,20),^(30,29))?
^(_(20,3),_(50,41))?
^(_(20,3),_(3,41))?
^(_(20,3),_(4,41))?

Sample Output 2

22 18
0 3
99 4
30 17
41 17
3 14
4 15

Source Name

KUPC2016
G - Exam

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

京都大学の一般入試を翌日に控えているあなたは, 試験に出題されそうな文字列集合 S を覚えることにした. しかし,S を覚えるのは面倒なので,代わりに語呂合わせとして文字列 T を覚えることにして, ST について以下の条件を満たすことを確認した.

  • 任意の S の要素は T連続する部分文字列(1) である.
  • 任意の S の要素の組 x, y (x \neq y) について, xy の部分列(2) でない.

ただし,(1) は「連続する部分列」であり,(2) は「部分列」であることに注意されたい.

翌日,試験が開始して答案用紙を開くと,どうやら S さえ覚えていれば満点を取れそうだということがわかった. しかし,あなたは文字列 T から S への復元のしかたを忘れてしまった. 文字列 T の他に上の条件を満たすことだけは覚えているので, まずは S の要素数としてありえる最大値を求めることにした.

制約

  • 1 \leq |T| \leq 10^5
  • 含まれる文字の種類は半角アルファベット小文字のみ

部分点

  • 1 \leq |T| \leq 50 を満たす全てのテストケースに正解した場合,部分点として 30 点が与えられる.
  • 1 \leq |T| \leq 10^3 を満たす全てのテストケースに正解した場合,部分点としてさらに 30 点が与えられる.

入力

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

T

入力は文字列 T の 1 行のみからなる.

出力

S の要素数の最大値を出力せよ.


入力例1

abcabc

出力例1

3
このときは, abcabc と復元した場合が最大となる例の1つである

入力例2

abracadabra

出力例2

7

入力例3

abcbabbcabbc

出力例3

8

入力例4

bbcacbcbcabbabacccbbcacbaaababbacabaaccbccabcaabba

出力例4

44

Score : 200 points

Problem Statement

You are going to take the entrance examination of Kyoto University tomorrow and have decided to memorize a set of strings S that is expected to appear in the examination. Since it is really tough to memorize S as it is, you have decided to memorize a single string T that efficiently contains all the strings in S.

You have already confirmed the following conditions for S and T are satisfied.
  • Every string in S is a consecutive subsequence(1) in T .
  • For every pair of strings x, y (x \neq y) in S , x is not a subsequence(2) of y.

Note that (1) is ''consecutive subsequence'', while (2) is ''subsequence''.

The next day, you opened the problem booklet at the examination and found that you can get a full score only if you remember S. However, You have forgot how to reconstruct S from T . Since all you remember is that T satisfies the above conditions, first you have decided to find the maximum possible number of elements in S .

Constraints

  • 1 \leq |T| \leq 10^5
  • T consists of only lowercase letters.

Partial points

  • 30 points will be awarded for passing the test set satisfying the condition: 1 \leq |T| \leq 50 .
  • Another 30 points will be awarded for passing the test set satisfying the condition: 1 \leq |T| \leq 10^3.

Input

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

T

The input only consists of T on one line.

Output

Print the maximum number of elements inS .


Sample Input 1

abcabc

Sample Output 1

3
In this case, abca and bc are an example of the optimum answers.

Sample Input 2

abracadabra

Sample Output 2

7

Sample Input 3

abcbabbcabbc

Sample Output 3

8

Sample Input 4

bbcacbcbcabbabacccbbcacbaaababbacabaaccbccabcaabba

Sample Output 4

44

Source Name

KUPC2016
H - WAAAAAAAAAAAAALL

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

京都大学は毎晩西から攻撃してくるゴリラから大学を守るために大学の西側に一直線の壁を築き上げることにした。 また,攻撃が激しい位置は壁だけでは防ぎきれないため,補強材を使って対処している。 補強材の数には限りがあるが,最先端の技術により,次回に攻撃してくるゴリラの数と位置を計算できるので,それに合わせて毎日補強材を移動させている。 その効率化のために,情報学科のエリートであるあなたが呼ばれた。

一直線の壁に沿って補強材を使う位置が等間隔に N 箇所あり,左から順に 12,...,N の番号が付いている。 前回の攻撃に対する備えで,今,各位置 i (1 \leq i \leq N) には A_i 個の補強材が使われている。 これらの補強材のうちいくつかを移動させ,各位置 i (1 \leq i \leq N) に B_i 個以上の補強材が使われている状態にしなければならない。 ただし,位置 i から位置 j に補強材を 1 個移動させるのにコストが |i - j| かかる。 この移動を繰り返し,条件を満たすために要する合計コストの最小値を出力せよ。 また,次々回以降の攻撃のことは考えない。

制約

  • 1 \leq N \leq 10^5
  • A_i \geq 1
  • B_i \geq 1
  • A_1 + A_2 + ... + A_N \leq 10^{12}
  • B_1 + B_2 + ... + B_N \leq A_1 + A_2 + ... + A_N
  • 条件を満たす移動方法は必ず存在する

入力

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

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

出力

条件を満たすために要する合計コストの最小値を標準出力に1行で出力せよ。

部分点

以下の追加制約を満たすデータセットに正解した場合は 30 点の部分点が与えられる。

  • N \leq 100
  • A_1 + A_2 + ... + A_N \leq 400

以下の追加制約を満たすデータセットに正解した場合は更に 30 点の部分点が与えられる。

  • N \leq 10^3

追加制約のないデータセットに正解した場合は更に 140 点が与えられ,合計で 200 点が得られる。


入力例1

2
1 5
3 1

出力例1

2

位置 2 から位置 12 個補強材を移動させる場合の合計コストが最小である。

入力例2

5
1 2 3 4 5
3 3 1 1 1

出力例2

6

入力例3

27
46 3 4 2 10 2 5 2 6 7 20 13 9 49 3 8 4 3 19 9 3 5 4 13 9 5 7
10 2 5 6 2 6 3 2 2 5 3 11 13 2 2 7 7 3 9 5 13 4 17 2 2 2 4

出力例3

48

このケースの入力は部分点の追加制約の1つ目,2つ目の両方を満たす。

入力例4

18
3878348 423911 8031742 1035156 24256 10344593 19379 3867285 4481365 1475384 1959412 1383457 164869 4633165 6674637 9732852 10459147 2810788
1236501 770807 4003004 131688 1965412 266841 3980782 565060 816313 192940 541896 250801 217586 3806049 1220252 1161079 31168 2008961

出力例4

6302172

このケースの入力は部分点の追加制約の2つ目を満たす。

入力例5

2
1 99999999999
1234567891 1

出力例5

1234567890

入力値および出力値は32bit整数型に収まらない場合がある。

Score : 200 points

Problem Statement

Kyoto University decided to build a straight wall on the west side of the university to protect against gorillas that attack the university from the west every night. Since it is difficult to protect the university at some points along the wall where gorillas attack violently, reinforcement materials are also built at those points. Although the number of the materials is limited, the state-of-the-art technology can make a prediction about the points where gorillas will attack next and the number of gorillas that will attack at each point. The materials are moved along the wall everyday according to the prediction. You, a smart student majoring in computer science, are called to find the way to move the materials more efficiently.

Theare are N points where reinforcement materials can be build along the straight wall. They are numbered 1 through N. Because of the protection against the last attack of gorillas, A_i materials are build at point i (1 \leq i \leq N). For the next attack, the materials need to be rearranged such that at least B_i materials are built at point i (1 \leq i \leq N). It costs |i - j| to move 1 material from point i to point j. Find the minimum total cost required to satisfy the condition by moving materials. You do not need to consider the attack after the next.

Constraints

  • 1 \leq N \leq 10^5
  • A_i \geq 1
  • B_i \geq 1
  • A_1 + A_2 + ... + A_N \leq 10^{12}
  • B_1 + B_2 + ... + B_N \leq A_1 + A_2 + ... + A_N
  • There is at least one way to satisfy the condition.

Input

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

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Print the minimum total cost required to satisfy the condition.

Partial Scores

30 points will be awarded for passing the test set satisfying the following:

  • N \leq 100
  • A_1 + A_2 + ... + A_N \leq 400

Another 30 points will be awarded for passing the test set satisfying the following:

  • N \leq 10^3

Another 140 points will be awarded for passing the test set without addtional constraints and you can get 200 points in total.


Sample Input 1

2
1 5
3 1

Sample Output 1

2

It costs least to move 2 materials from point 2 to point 1.

Sample Input 2

5
1 2 3 4 5
3 3 1 1 1

Sample Output 2

6

Sample Input 3

27
46 3 4 2 10 2 5 2 6 7 20 13 9 49 3 8 4 3 19 9 3 5 4 13 9 5 7
10 2 5 6 2 6 3 2 2 5 3 11 13 2 2 7 7 3 9 5 13 4 17 2 2 2 4

Sample Output 3

48

The input of this test case satisfies both the first and second additional constraints.

Sample Input 4

18
3878348 423911 8031742 1035156 24256 10344593 19379 3867285 4481365 1475384 1959412 1383457 164869 4633165 6674637 9732852 10459147 2810788
1236501 770807 4003004 131688 1965412 266841 3980782 565060 816313 192940 541896 250801 217586 3806049 1220252 1161079 31168 2008961

Sample Output 4

6302172

The input of this test case satisfies the second additional constraint.

Sample Input 5

2
1 99999999999
1234567891 1

Sample Output 5

1234567890

The input and output values may exceed the range of 32-bit integer.


Source Name

KUPC2016
I - Handing out leaflets

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 300

問題文

Eli- 1 さんは仕事時間が N 秒のティッシュ配りのバイトをすることにした. Eli- 1 さんは特殊能力である分身を利用してなるべく多くのティッシュを配ることにした. Eli- gen さんができる行動は次の 2 種類である.

  • gen \times C ( C は分身にかかる時間の係数) 秒をかけてEli- gen さんとEli- (gen + 1) さんの 2 人に分身する.
  • 世代( = gen )に関わらず 1 秒をかけてちょうど 1 個のティッシュを配る.

配りながら分身するということはできない. N , C が与えられたとき, Eli- 1 さんが分身と合計で最大何個のティッシュを配ることができるかを求めよ. ただし, 配れるティッシュの数は非常に大きくなることがあるため 1000000007 (= 10^9 + 7) で割った余りを解答として出力せよ.

制約

  • 1 \leq Q \leq 100000 = 10^5
  • 1 \leq N_q \leq 100000 = 10^5
  • 1 \leq C_q \leq 20000 = 2 \times 10^4

入力

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

Q
N_1 C_1
:
N_Q C_Q

入力は複数のクエリからなる. 1 行目には, クエリの個数を表す整数 Q が与えられる.

続く Q 行に, それぞれ 1 個のクエリが与えられる. q ( 1 \leq q \leq Q ) 番目のクエリでは, N_qC_q が半角スペース区切りで与えられる. N_qC_qq 番目のクエリにおける, 仕事時間と分身にかかる時間の係数をそれぞれ表している.

出力

各クエリに対して, Eli- 1 さんが分身と合計で最大何個のティッシュを配ることができるかを 1 行で出力せよ. ただし, 配れるティッシュの数は非常に大きくなることがあるため 1000000007 ( = 10^9 + 7 ) で割った余りを出力せよ.

部分点

Q = 1 を満たすデータセットに正解した場合は 30 点の部分点が与えられる.

追加制約のないデータセットに正解した場合は追加で 270 点が与えられ,合計で 300 点が得られる.


入力例 1

2
20 8
20 12

出力例 1

24
20

1 つめのクエリでは, 分身しないと 20 個しか配れないところを, 分身後 2 人が 12 個ずつ配ることで 24 個配ることができ, これが最大である.

2 つめのクエリでは, 分身しても 2 人がそれぞれ 8 個ずつしか配れないため, 分身せずに 20 個配るほうがよい.

入力例 2

1
20 3

出力例 2

67

以下の図のようにすればよい. 黒線は分身を表し, 赤線はティッシュを配ることを表している.

このケースは部分点の追加制約を満たす.

入力例 3

1
200 1

出力例 3

148322100

1000000007 ( 10^9 + 7 ) で割った余りを出力する必要があることに注意せよ.

このケースは部分点の追加制約を満たす.

Score : 300 points

Problem Statement

Eli- 1 started a part-time job handing out leaflets for N seconds. Eli- 1 wants to hand out as many leaflets as possible with her special ability, Cloning. Eli- gen can perform two kinds of actions below.

  • Clone herself and generate Eli- (gen + 1) . (one Eli- gen (cloning) and one Eli- (gen + 1) (cloned) exist as a result of Eli-gen 's cloning.) This action takes gen \times C ( C is a coefficient related to cloning. ) seconds.
  • Hand out one leaflet. This action takes one second regardress of the generation ( =gen ).

They can not hand out leaflets while cloning. Given N and C , find the maximum number of leaflets Eli- 1 and her clones can hand out in total modulo 1000000007 (= 10^9 + 7).

Constraints

  • 1 \leq Q \leq 100000 = 10^5
  • 1 \leq N_q \leq 100000 = 10^5
  • 1 \leq C_q \leq 20000 = 2 \times 10^4

Input

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

Q
N_1 C_1
:
N_Q C_Q

The input consists of multiple test cases. On line 1 , Q that represents the number of test cases is given. Each test case is given on the next Q lines. For the test case q ( 1 \leq q \leq Q ) , N_q and C_q are given separated by a single space. N_q and C_q represent the working time and the coefficient related to cloning for test case q respectively.

Output

For each test case, Print the maximum number of leaflets Eli- 1 and her clones can hand out modulo 1000000007 ( = 10^9 + 7 ).

Partial Scores

30 points will be awarded for passing the test set satisfying the condition: Q = 1 .

Another 270 points will be awarded for passing the test set without addtional constraints and you can get 300 points in total.


Sample Input 1

2
20 8
20 12

Sample Output 1

24
20

For the first test case, while only 20 leaflets can be handed out without cloning, 24 leaflets can be handed out by cloning first and two people handing out12 leaflets each.

For the second test case, since two people can only hand out 8 leaflets each if Eli- 1 clones, she should hand out 20 leaflets without cloning.

Sample Input 2

1
20 3

Sample Output 2

67

One way of handing out 67 leaflets is like the following image. Each black line means cloning, and each red line means handing out.

This case satisfies the constraint of the partial score.

Sample Input 3

1
200 1

Sample Output 3

148322100

Note that the value modulo 1000000007 ( 10^9 + 7 ) must be printed.

This case satisfies the constraint of the partial score.


Source Name

KUPC2016
J - Coloring

Time Limit: 4 sec / Memory Limit: 256 MB

配点 : 300

問題文

もうすぐみかんの誕生日である。あろまはグラフが好きなみかんのために今年も無向グラフをプレゼントすることにした。

あろまは n 頂点 n 辺からなる連結な無向グラフを購入した。このグラフの頂点には 1, 2, ..., n の番号がついており、各 i(1 \leq i \leq n) について頂点 i と頂点 a_i の間に辺がある。あろまは既製品を贈るだけでは芸がないと考え k 色で色を塗ることに決め、手始めに色の塗り方が何通りあるかを数えることにした。ただし、頂点番号を並び替えて一致する色の塗り方は同一であるとし、重複して数えないものとする。購入したグラフの頂点に k 色で塗る塗り方の個数を 10^9 + 7 で割った余りを求めよ。

制約

  • 1 \leq n \leq 10^5
  • 1 \leq k \leq 10^5
  • 1 \leq a_i \leq n (1 \leq i \leq n)
  • グラフは連結である。
  • グラフに自己辺や多重辺は存在しない。

入力

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

n k
a_1
:
a_n

出力

グラフの頂点を k 色で塗る塗り方の個数を 10^9 + 7 で割った余りを一行で出力せよ。


入力例1

4 2
2
3
1
1

出力例1

12

入力例2

4 4
2
3
4
1

出力例2

55

入力例3

10 5
2
3
4
1
1
1
2
3
3
4

出力例3

926250

Score : 300 points

Problem Statement

Mikan's birthday is coming soon. Since Mikan likes graphs very much, Aroma decided to give her a undirected graph this year too.

Aroma bought a connected undirected graph, which consists of n vertices and n edges. The vertices are numbered from 1 to n and for each i(1 \leq i \leq n), vertex i and vartex a_i are connected with an undirected edge. Aroma thinks it is not interesting only to give the ready-made graph and decided to paint it. Count the number of ways to paint the vertices of the purchased graph in k colors modulo 10^9 + 7. Two ways are considered to be the same if the graph painted in one way can be identical to the graph painted in the other way by permutating the numbers of the vertices.

Constraints

  • 1 \leq n \leq 10^5
  • 1 \leq k \leq 10^5
  • 1 \leq a_i \leq n (1 \leq i \leq n)
  • The given graph is connected
  • The given graph contains no self-loops or multiple edges.

Input

Each data set is given in the following format from the standard input.

n k
a_1
:
a_n

Output

Output the number of ways to paint the vertices of the given graph in k colors modulo 10^9 + 7 in one line.


Sample Input 1

4 2
2
3
1
1

Sample Output 1

12

Sample Input 2

4 4
2
3
4
1

Sample Output 2

55

Sample Input 3

10 5
2
3
4
1
1
1
2
3
3
4

Sample Output 3

926250

Source Name

KUPC2016
K - Hundred Eyes Monster

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 300

あらすじ

私は召喚術を使う魔術師である。スケッチブックに描いた魔物の絵を実体化させることができる。

この度、京都大学霊長類研究所から百目おばけを召喚してほしいという依頼が届いた。

京都大学の霊長類学の発展に寄与できるなど何たる譽れであろうか!私は腕によりをかけて、飛び切り高品質な百目を作ることにした。

私はまず額と口を描いた。次はこの額と口の間になるべく多くの目を描こう。ふふ、百目の完成が楽しみだ。

問題文

2 次元平面上に 2 つの円 A, B がある。 円 A の中心の座標は (x_A, y_A), 半径は r_A であり、 円 B の中心の座標は (x_B, y_B), 半径は r_B である。 この 2 つの円の内部や外周は共通部分を持たない。 ここで、以下の条件を満たす円集合 S を考える。

  • S に含まれる任意の円は、A, B に接し、内部に共通部分を持たない。
  • S に含まれる任意の異なる円 C_1, C_2 は、内部に共通部分を持たない。

集合 S の要素数の最大値を求めよ。

制約

  • 1 \leq T \leq 50000
  • -10^5 \leq x_A \leq 10^5
  • -10^5 \leq y_A \leq 10^5
  • -10^5 \leq x_B \leq 10^5
  • -10^5 \leq y_B \leq 10^5
  • 1 \leq r_A \leq 10^5
  • 1 \leq r_B \leq 10^5
  • {r_A}^2 + {r_B}^2 < (x_A - x_B)^2 + (y_A - y_B)^2
  • 入力は全て整数である。
  • A, B の半径を 0.0005 だけ変化させても答えが変わらないことが保証されている。

入力

入力は複数のテストケースからなり、以下の形式で標準入力から与えられる。

T
testcase_1
:
testcase_T
テストケース 1 つは以下の形式で与えられる。
x_A y_A r_A x_B y_B r_B

出力

出力は T 行からなる。 i (1 \leq i \leq T) 行目には i 番目のテストケースにおける S の要素数の最大値を出力せよ。


入力例1

4
0 -3 2 0 3 2
0 0 9 8 8 2
0 0 9 10 10 5
0 0 707 1000 1000 707

出力例1

3
10
21
180
1 つ目のテストケース、2 つ目のテストケースでは、例えば以下のように配置すればよい。

Score : 300 points

Story

I am a Summoner who can invoke monsters. I can embody the pictures of monsters drawn on sketchbooks.

I recently have received a request from Primate Research Institute of Kyoto University to invoke "Hundred Eyes Monster".

What an great honor it is to contribute to the prosperity of the primate research of Kyoto University! I decided to paint an extremely high-quarity monster to the best of my ability.

I have already painted the forehead and mouth and this time I am going to paint as many eyes as possible between the forehead and mouth. Well...I cannot wait to see the complete painting.

Problem Statement

Two circles A, B are given on a two-dimensional plane. The coordinate of the center and radius of circle A is (x_A, y_A) and r_A respectively. The coordinate of the center and radius of circle B is (x_B, y_B) and r_B respectively. These two circles have no intersection inside. Here, we consider a set of circles S that satisfies the following conditions.

  • Each circle in S touches both A and B, and does not have common points with them inside the circle.
  • Any two different circles in S have no common points inside them.

Write a program that finds the maximum number of elements in S.

Constraints

  • 1 \leq T \leq 50000
  • -10^5 \leq x_A \leq 10^5
  • -10^5 \leq y_A \leq 10^5
  • -10^5 \leq x_B \leq 10^5
  • -10^5 \leq y_B \leq 10^5
  • 1 \leq r_A \leq 10^5
  • 1 \leq r_B \leq 10^5
  • {r_A}^2 + {r_B}^2 < (x_A - x_B)^2 + (y_A - y_B)^2
  • All values given as input are integers.
  • It is guaranteed that, when the radiuses of A and B change by 0.0005, the answer does not change.

Input

The input consists of multiple test cases and is given from Standard Input in the following format:

T
testcase_1
:
testcase_T
Each test case is given with the following format.
x_A y_A r_A x_B y_B r_B

Output

The output consists of T lines. On line i (1 \leq i \leq T), putput the maximum number of elements in S in i-th test case.


Sample Input 1

4
0 -3 2 0 3 2
0 0 9 8 8 2
0 0 9 10 10 5
0 0 707 1000 1000 707

Sample Output 1

3
10
21
180
You can arrange circles ,for example, like the following images in Sample 1, 2.

Source Name

KUPC2016