task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Problem C: Unhappy Class Problem 山之埡船孊園の1幎G組は、䞍幞を背負った女子生埒たちが集たるクラスである。 圌女たちは、幞犏になるこずを目暙に、日々様々な詊緎に立ち向かう。 圌女たちのクラスでは、幞犏になるための詊緎の䞀環ずしお、幞犏実技科目を受講するこずができる。 月曜から金曜のそれぞれ1限から N 限たでに授業科目の枠があり、 M 個の受講可胜な科目がある。 科目 i は、曜日 d i ( d i = 0, 1, 2, 3, 4がそれぞれ、月曜、火曜、氎曜、朚曜、金曜に察応する)の a i 限目から始たり、連続する k i コマで行われ、それを受講したずきに埗られる幞犏床は t i である。 各生埒は、最倧 L 個の科目を互いに重ならないように自由に遞ぶこずができる。どのように科目を遞べば、最も高い幞犏床を埗られるだろうか。䞎えられた科目の情報から、埗られる幞犏床の最倧倀を求めおほしい。 Input 入力は以䞋の圢匏で䞎えられる。 N M L d 1 a 1 k 1 t 1 d 2 a 2 k 2 t 2 ... d M a M k M t M 1行目に3぀の敎数 N , M , L が空癜区切りで䞎えられる。 2行目から M +1行目に4぀の敎数 d i , a i , k i , t i が空癜区切りで䞎えられる。 Constraints 2 ≀ N ≀ 8 0 ≀ M ≀ 300 0 ≀ L ≀ min( N ×5, M ) 0 ≀ d i ≀ 4 1 ≀ a i ≀ N 1 ≀ k i a i + k i - 1 ≀ N 1 ≀ t i ≀ 100 Output 幞犏床の和の最倧倀を1行に出力せよ。 Sample Input 1 3 7 3 0 1 1 1 0 1 1 2 1 1 3 4 1 1 1 1 1 2 1 2 2 1 1 3 2 2 2 1 Sample Output 1 9 Sample Input 2 5 10 5 0 1 1 2 0 2 1 2 0 1 2 3 1 2 1 2 1 4 2 3 2 1 1 1 2 1 1 2 3 3 2 3 4 1 1 2 4 2 1 2 Sample Output 2 13
37,544
サヌゞェント・ラむアン 「ラむアン軍曹を救え」ずいう指什のもず、アむヅ軍の救出郚隊はドむツリヌの氎䞊郜垂で敵軍ず激しい戊闘を繰り広げおいたした。圌らは無事に軍曹ず合流したしたが、敵の戊車が倚く、救出ヘリを呌べずにいたした。そこで、圌らは敵の戊車を混乱させるため、郜垂にある橋を党お爆砎するずいう䜜戊を実行するこずにしたした。 䜜戊はすぐに叞什郚に䌝えられ、救出ヘリの準備が進められたした。救出ヘリを飛ばすためには、い぀橋が党お爆砎されるかを予枬しなければなりたせん。軍のプログラマであるあなたの任務は、救出郚隊が党おの橋の爆砎に必芁な時間を蚈算するこずです。 氎䞊郜垂は N 個の島で構成されおおり、島ず島ずの間には橋がかかっおいたす。すべおの島はツリヌ状に繋がっおいたす(䞋図参照) 。ある島からある島ぞの経路は、䞀通りだけ存圚したす。各橋を枡るには、橋ごずに決められた時間がかかり、どちらの方向にもその時間で橋を枡るこずが可胜です。 救出郚隊はボヌトなど氎䞊を移動する手段を持っおいないので島ず島の間を移動するには橋を通る他ありたせん。救出郚隊は、その時いる島に隣接しおいる橋のうち、必芁なものを䞀瞬で爆砎するこずができたす。救出郚隊が党おの橋を爆砎するのに必芁な最小の時間はいくらでしょうか。ただし、島の䞭での移動時間は考えたせん。 島の数、それぞれの橋情報を入力ずし、橋を党お爆砎するのに必芁な最小の時間を出力するプログラムを䜜成しおください。島はそれぞれ 1 から N の番号で衚されたす。橋は N -1 本ありたす。橋情報は、その橋が隣接しおいる二぀の島の番号( a , b )ず、その橋を枡るのに必芁な時間 t で構成されたす。救出郚隊は島番号 1 の島からスタヌトするものずしたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられる。入力の終わりはれロひず぀の行で瀺される。各デヌタセットは以䞋の圢匏で䞎えられる。 N a 1 b 1 t 1 a 2 b 2 t 2 : a N-1 b N-1 t N-1 入力はすべお敎数で䞎えられる。1 行目に島の数 N (2 ≀ N ≀ 20) が䞎えられる。 続く N -1 行に i 番目の橋の情報が䞎えられる。 a i , b i , t i (1 ≀ t i ≀ 500) は、 i 番目の橋を通っお島 a i ず島 b i の間を時間 t i で移動できるこずを衚す。 デヌタセットの数は 100 を超えない。 Output デヌタセットごずに、橋を党お爆砎するのに必芁な最小の時間を1行に出力する。 Sample Input 7 1 2 5 2 3 2 3 4 3 2 5 3 5 6 3 5 7 8 0 Output for the Sample Input 12
37,545
カツサンド (Cutlet Sandwich) ある䞖界には、 $X$ 皮類の「サンド」、 $Y$ 皮類の「カツ」、 $Z$ 皮類の「カレヌ」ずいう食べ物がありたす。 この䞖界には $N$ 皮類の「カツサンド」ずいう食べ物があり、 $i$ 皮類目のカツサンドは $A_i$ 皮類目のサンドず $B_i$ 皮類目のカツが原料です。 たた、 $M$ 皮類の「カツカレヌ」ずいう食べ物があり、 $i$ 皮類目のカツカレヌは $C_i$ 皮類目のカツず $D_i$ 皮類目のカレヌが原料です。 Segtree 君は、あるカツサンドたたはカツカレヌを持っおいるずき、原料のうち少なくずも $1$ ぀が共通しおいるようなカツサンドたたはカツカレヌず亀換するこずができたす。 䟋えば、$a$ 皮類目のサンドず $b$ 皮類目のカツが原料であるカツサンドを持っおいるずき、 $a$ 皮類目のサンドたたは $b$ 皮類目のカツを原料に持぀任意のカツサンド、たたは、 $b$ 皮類目のカツを原料に含む任意のカツカレヌず亀換できたす。 今、 Segtree 君は $S$ 皮類目のカツサンドを持っおいたすが、食べたいのは $T$ 皮類目のカツカレヌです。 $T$ 皮類目のカツカレヌを手に入れるこずができるか刀定しおください。もし可胜ならば、最小䜕回の亀換で目的のカツカレヌを手にいられるかを求めおください。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $X$ $Y$ $Z$ $N$ $M$ $S$ $T$ $A_1$ $B_1$ $A_2$ $B_2$ $\ldots$ $A_N$ $B_N$ $C_1$ $D_1$ $C_2$ $D_2$ $\ldots$ $C_M$ $D_M$ 出力 $T$ 皮類目のカツカレヌを手に入れるために必芁な最小の亀換回数を出力しおください。手に入れるこずが䞍可胜ならば、代わりに「 $-1$ 」を出力しおください。 ただし、最埌には改行を入れるこず。 制玄 $1 \leq X,Y,Z,N,M \leq 10^5$ $1 \leq S \leq N$ $1 \leq T \leq M$ $1 \leq A_i \leq X$ $1 \leq B_i \leq Y$ $1 \leq C_i \leq Y$ $1 \leq D_i \leq Z$ 入力は党お敎数である。 入力䟋1 1 1 1 1 1 1 1 1 1 1 1 出力䟋1 1 入力䟋2 2 3 4 3 5 1 5 1 1 1 2 2 2 2 1 3 1 3 2 3 3 3 4 出力䟋2 4 入力䟋3 1 2 2 1 2 1 1 1 2 1 2 2 1 出力䟋3 -1
37,546
Problem G : Everything Starts With Your Vote ヒュヌリスティクスずいう蚀葉がある。確実にうたくいくずいう保蚌はないけれども倧抵の堎合はうたくいく、比范的単玔なアプロヌチのこずだ。単玔にしお匷力であるがゆえに、この䞖の䞭は倚くのヒュヌリスティクスで満ち溢れおいる。 ヒュヌリスティクスの䞀䟋ずしお、こんなものが挙げられる。アニメ番組においお、あるキャラクタヌの人気床は、そのアニメ本線䞭における登堎時間の総和に比䟋する。確かにこれは倚くの堎合に成り立っおいるようだ。しかし、どうやら僕は少数掟に属するらしい。圱の薄いキャラ、背景にちらりず映るモブキャラなんかに限っお可愛く芋えたりするのだ。 自分の奜きなキャラのこずを、他人がどう思っおいようがいいじゃないか。ずは蚀っおられない事情が、残念ながらある。関連商品、フィギュアやキャラクタヌ゜ングCDなどは、人気のあるキャラのものから優先的に発売される傟向にあるのだ。商業的には圓然の遞択だずはいえ、䞍人気キャラのファンの肩身は狭い。 そこで重芁ずなっおくるのが、アニメ番組の制䜜䌚瀟によっお行われる人気投祚である。実際の人気がどうであれ、ここで倚くの祚を獲埗すれば、関連商品ぞの道が開かれるのだ。どんな手段を䜿っおも祚を集めねばなるたい。 厳正なる投祚のために、ある関連商品を1぀賌入するたびに1祚の投祚暩が䞎えられる。この仕組を厳正ず呌ぶべきかどうかは今倧きく議論されおいるが、ずにかく僕は L 祚の投祚暩を埗た。投祚の察象ずなるキャラは党郚で N 人いお、より倚くの祚を獲埗した K 人のキャラ (同祚の堎合は名前の蟞曞順) の関連商品が䌁画されるこずになる。僕の奜きなキャラは党郚で M 人いお、その䞭からできるだけ倚くのキャラを䞊䜍 K 人に入れたいず思っおいる。 僕はたず、それぞれのキャラがどれだけの祚を獲埗するかを予枬した(難しいこずではない。あるキャラクタヌの人気床は、その登堎時間の総和に比䟋するず仮定しただけだ)。この予枬が正しいずしお、より倚くの僕の奜きなキャラの関連商品が発売されるようにするためには、僕はこの L 祚を誰にどれだけ投じればいいだろうか。 Input 入力は耇数のケヌスからなる。 各ケヌスは以䞋のフォヌマットで䞎えられる。 N M K L name 0 x 0 . . . name N-1 x N-1 fav 0 . . . fav M-1 N 、 M 、 K 、 L の意味は問題文に蚘されおいるずおりである。 name i はキャラクタヌの名前、 x i は i 番目のキャラクタヌの祚の総数を衚す。 fav i はあなたがひいきしおいるキャラクタヌの名前を衚す。これらの名前は必ず異なり、たた必ずキャラクタヌの名前の入力の䞭に含たれる。 入力の終わりは N = 0 か぀ M = 0 か぀ K = 0 か぀ L = 0からなる行によっお䞎えられる たた各倀は以䞋の条件を満たす 1 ≀ N ≀ 100,000 1 ≀ M ≀ N 1 ≀ K ≀ N 1 ≀ L ≀ 100,000,000 0 ≀ x i ≀ 100,000,000 name i はアルファベットのみを含み、長さは10文字以䞋である。 テストケヌスの数は150を超えない。 たた20,000 ≀ N ずなるケヌスの数は20を超えないこずが保蚌されおいる。 ゞャッゞデヌタは倧きいので、速い入力を䜿うこずを掚奚する。 Output M 人のキャラの䞭から最倧で䜕人を䞊䜍 K 人に入れるこずが出来るかを1行で出力せよ。 Sample input 4 1 3 4 yskwcnt 16 akzakr 7 tsnukk 12 fnmyi 13 akzakr 4 1 3 5 akzakr 7 tsnukk 12 fnmyi 13 yskwcnt 16 akzakr 4 2 2 1 akzakr 4 tsnukk 6 yskwcnt 6 fnmyi 12 akzakr fnmyi 5 2 3 28 knmmdk 6 skrkyk 14 tmemm 14 akmhmr 15 mksyk 25 knmmdk skrkyk 5 2 3 11 tmemm 12 skrkyk 18 knmmdk 21 mksyk 23 akmhmr 42 skrkyk tmemm 14 5 10 38 iormns 19 hbkgnh 23 yyitktk 31 rtkakzk 36 mmftm 43 ykhhgwr 65 hrkamm 66 ktrotns 67 mktkkc 68 mkhsi 69 azsmur 73 tknsj 73 amftm 81 chyksrg 88 mkhsi hbkgnh mktkkc yyitktk tknsj 14 5 10 38 mktkkc 24 rtkakzk 25 ykhhgwr 25 hrkamm 27 amftm 37 iormns 38 tknsj 38 yyitktk 39 hbkgnh 53 mmftm 53 chyksrg 63 ktrotns 63 azsmur 65 mkhsi 76 mkhsi hbkgnh mktkkc yyitktk tknsj 0 0 0 0 Sample output 0 1 1 2 1 4 5 The University of Aizu Programming Contest 2011 Summer 原案: Tomoya Sakai 問題文: Takashi Tayama
37,547
Score : 200 points Problem Statement Given N integers A_1, ..., A_N , compute A_1 \times ... \times A_N . However, if the result exceeds 10^{18} , print -1 instead. Constraints 2 \leq N \leq 10^5 0 \leq A_i \leq 10^{18} All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18} . Sample Input 1 2 1000000000 1000000000 Sample Output 1 1000000000000000000 We have 1000000000 \times 1000000000 = 1000000000000000000 . Sample Input 2 3 101 9901 999999000001 Sample Output 2 -1 We have 101 \times 9901 \times 999999000001 = 1000000000000000001 , which exceeds 10^{18} , so we should print -1 instead. Sample Input 3 31 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 Sample Output 3 0
37,548
D: æ°Žæ§œ 問題 AORむカちゃんは瞊 $1$ 暪 $N$ の倧きさの氎槜をもらった。氎槜は氎を入れるのに十分な高さがある。 氎槜には $N-1$ 個の仕切りがあり $N$ 個の区画に等間隔に区切られおいる。 ここに氎を泚いだずころ、各区画の氎の高さは $a_i$ になった。 AORむカちゃんはいく぀か仕切りを取り陀き、区画の数を $M$ 個以䞋にするこずにした。 仕切りを取り陀き終わった時、各区画の氎の高さの総和の最倧倀を求めよ。 なお、仕切りの厚さは無芖できるものずする。 制玄 $1 \le N \le 500$ $1 \le M \le N$ $1 \le a_i \le 10^9$ 入力は党お敎数 入力 $N \ M$ $a_1 \cdots a_N$ 出力 各区画の氎の高さの総和の最倧倀を䞀行で出力せよ。たた末尟に改行を出力せよ。 盞察誀差たたは絶察誀差が $10^{-6}$ 以䞋であれば蚱容される。 サンプル サンプル入力 1 5 3 9 1 2 3 9 サンプル出力 1 20.000000 $a_2$ ず $a_3$ 、$a_3$ ず $a_4$ の間の仕切りをずるず、その区画の氎の高さは $\frac{(1 + 2 + 3)}{3} = 2$ ずなる。 各区画の氎の高さは $9 \ 2 \ 9$ ずなりこれが最倧である。 サンプル入力 2 4 1 14 4 9 7 サンプル出力 2 8.500000 サンプル入力 3 8 3 11 18 9 20 4 18 12 14 サンプル出力 3 44.666667
37,549
Lake Survery The Onogawa Expedition is planning to conduct a survey of the Aizu nature reserve. The expedition planner wants to take the shortest possible route from the start to end point of the survey, while the expedition has to go around the coast of the Lake of Onogawa en route. The expedition walks along the coast of the lake, but can not wade across the lake. Based on the base information including the start and end point of the survey and the area of Lake Onogawa as convex polygon data, make a program to find the shortest possible route for the expedition and calculate its distance. Note that the expedition can move along the polygonal lines passing through the nodes, but never enter within the area enclosed by the polygon. Input The input is given in the following format. x_s y_s x_g y_g N x_1 y_1 x_2 y_2 : x_N y_N The first line provides the start point of the survey x_s,y_s (0≀ x_s,y_s ≀10 4 ), and the second line provides the end point x_g,y_g (0 ≀ x_g,y_g ≀ 10 4 ) all in integers. The third line provides the number of apexes N (3 ≀ N ≀ 100) of the polygon that represents the lake, and each of the subsequent N lines provides the coordinate of the i -th apex x_i,y_i (0 ≀ x_i,y_i ≀ 10 4 ) in counter-clockwise order. These data satisfy the following criteria: Start and end points of the expedition are not within the area enclosed by the polygon nor on boundaries. Start and end points of the expedition are not identical, i.e., x_s ≠ x_g or y_s ≠ y_g . No duplicate coordinates are given, i.e., if i ≠ j then x_i ≠ x_r or y_i ≠ y_j . The area enclosed by the polygon has a positive value. Any three coordinates that define an area are not aligned on a line. Output Output the distance of the shortest possible expedition route. Any number of decimal places can be selected as long as the error does not exceed ± 10 -3 . Sample Input 1 0 0 4 0 4 1 1 2 1 3 3 1 2 Sample Output 1 4.472136 Sample Input 2 4 4 0 0 4 1 1 3 1 3 3 1 3 Sample Output 2 6.32455
37,550
C: 玠数 問題 ある日、玠数が倧奜きなそすうさ君は $p + q$ が玠数ずなるペア $(p, q)$ で遊んでいた。 そすうさ君は突然、このペアのうち、 $p$ ず $q$ がずもに $N$ 以䞋の玠数であるペアがいく぀あるか気になりだした。 そすうさ君に代わっお個数を求めよ。 制玄 入力倀は党お敎数である。 $1 \leq N \leq 10^7$ 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N$ 出力 ペアの個数を出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 3 サンプル出力 1 2 ペアは $(2, 3), (3, 2)$ の二぀である。 サンプル入力 2 20 サンプル出力 2 8
37,551
Problem E: Earth Observation with a Mobile Robot Team A new type of mobile robot has been developed for environmental earth observation. It moves around on the ground, acquiring and recording various sorts of observational data using high precision sensors. Robots of this type have short range wireless communication devices and can exchange observational data with ones nearby. They also have large capacity memory units, on which they record data observed by themselves and those received from others. Figure 1 illustrates the current positions of three robots A, B, and C and the geographic coverage of their wireless devices. Each circle represents the wireless coverage of a robot, with its center representing the position of the robot. In this figure, two robots A and B are in the positions where A can transmit data to B, and vice versa. In contrast, C cannot communicate with A or B, since it is too remote from them. Still, however, once B moves towards C as in Figure 2, B and C can start communicating with each other. In this manner, B can relay observational data from A to C. Figure 3 shows another example, in which data propagate among several robots instantaneously. Figure 1: The initial configuration of three robots Figure 2: Mobile relaying Figure 3: Instantaneous relaying among multiple robots As you may notice from these examples, if a team of robots move properly, observational data quickly spread over a large number of them. Your mission is to write a program that simulates how information spreads among robots. Suppose that, regardless of data size, the time necessary for communication is negligible. Input The input consists of multiple datasets, each in the following format. N T R nickname and travel route of the first robot nickname and travel route of the second robot ... nickname and travel route of the N-th robot The first line contains three integers N , T , and R that are the number of robots, the length of the simulation period, and the maximum distance wireless signals can reach, respectively, and satisfy that 1 <= N <= 100, 1 <= T <= 1000, and 1 <= R <= 10. The nickname and travel route of each robot are given in the following format. nickname t 0 x 0 y 0 t 1 vx 1 vy 1 t 2 vx 2 vy 2 ... t k vx k vy k Nickname is a character string of length between one and eight that only contains lowercase letters. No two robots in a dataset may have the same nickname. Each of the lines following nickname contains three integers, satisfying the following conditions. 0 = t 0 < t 1 < ... < t k = T -10 <= vx 1 , vy 1 , ..., vx k , vy k <= 10 A robot moves around on a two dimensional plane. ( x 0 , y 0 ) is the location of the robot at time 0. From time t i -1 to t i (0 < i <= k ), the velocities in the x and y directions are vx i and vy i , respectively. Therefore, the travel route of a robot is piecewise linear. Note that it may self-overlap or self-intersect. You may assume that each dataset satisfies the following conditions. The distance between any two robots at time 0 is not exactly R. The x - and y -coordinates of each robot are always between -500 and 500, inclusive. Once any robot approaches within R + 10 -6 of any other, the distance between them will become smaller than R - 10 -6 while maintaining the velocities. Once any robot moves away up to R - 10 -6 of any other, the distance between them will become larger than R + 10 -6 while maintaining the velocities. If any pair of robots mutually enter the wireless area of the opposite ones at time t and any pair, which may share one or two members with the aforementioned pair, mutually leave the wireless area of the opposite ones at time t' , the difference between t and t' is no smaller than 10 -6 time unit, that is, | t - t' | >= 10 -6 . A dataset may include two or more robots that share the same location at the same time. However, you should still consider that they can move with the designated velocities. The end of the input is indicated by a line containing three zeros. Output For each dataset in the input, your program should print the nickname of each robot that have got until time T the observational data originally acquired by the first robot at time 0. Each nickname should be written in a separate line in dictionary order without any superfluous characters such as leading or trailing spaces. Sample Input 3 5 10 red 0 0 0 5 0 0 green 0 5 5 5 6 1 blue 0 40 5 5 0 0 3 10 5 atom 0 47 32 5 -10 -7 10 1 0 pluto 0 0 0 7 0 0 10 3 3 gesicht 0 25 7 5 -7 -2 10 -1 10 4 100 7 impulse 0 -500 0 100 10 1 freedom 0 -491 0 100 9 2 destiny 0 -472 0 100 7 4 strike 0 -482 0 100 8 3 0 0 0 Output for the Sample Input blue green red atom gesicht pluto freedom impulse strike
37,552
Shifting a Matrix You are given $N \times N$ matrix $A$ initialized with $A_{i,j} = (i - 1)N + j$, where $A_{i,j}$ is the entry of the $i$-th row and the $j$-th column of $A$. Note that $i$ and $j$ are 1-based. You are also given an operation sequence which consists of the four types of shift operations: left, right, up, and down shifts. More precisely, these operations are defined as follows: Left shift with $i$: circular shift of the $i$-th row to the left, i.e., setting previous $A_{i,k}$ to new $A_{i,k-1}$ for $2 \leq k \leq N$, and previous $A_{i,1}$ to new $A_{i,N}$. Right shift with $i$: circular shift of the $i$-th row to the right, i.e., setting previous $A_{i,k}$ to new $A_{i,k+1}$ for $1 \leq k \leq N - 1$, and previous $A_{i,N}$ to new $A_{i,1}$. Up shift with $j$: circular shift of the $j$-th column to the above, i.e., setting previous $A_{k,j}$ to new $A_{k-1,j}$ for $2 \leq k \leq N$, and previous $A_{1,j}$ to new $A_{N,j}$. Down shift with $j$: circular shift of the $j$-th column to the below, i.e., setting previous $A_{k,j}$ to new $A_{k+1,j}$ for $1 \leq k \leq N - 1$, and previous $A_{N,j}$ to new $A_{1,j}$. An operation sequence is given as a string. You have to apply operations to a given matrix from left to right in a given string. Left, right, up, and down shifts are referred as 'L', 'R', 'U', and 'D' respectively in a string, and the following number indicates the row/column to be shifted. For example, "R25" means we should perform right shift with 25. In addition, the notion supports repetition of operation sequences. An operation sequence surrounded by a pair of parentheses must be repeated exactly $m$ times, where $m$ is the number following the close parenthesis. For example, "(L1R2)10" means we should repeat exactly 10 times the set of the two operations: left shift with 1 and right shift with 2 in this order. Given operation sequences are guaranteed to follow the following BNF: <sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation> <repetition> := '('<sequence>')'<number> <operation> := <shift><number> <shift> := 'L' | 'R' | 'U' | 'D' <number> := <nonzero_digit> |<number><digit> <digit> := '0' | <nonzero_digit> <nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' Given $N$ and an operation sequence as a string, make a program to compute the $N \times N$ matrix after operations indicated by the operation sequence. Input The input consists of a single test case. The test case is formatted as follows. $N$ $L$ $S$ The first line contains two integers $N$ and $L$, where $N$ ($1 \leq N \leq 100$) is the size of the given matrix and $L$ ($2 \leq L \leq 1,000$) is the length of the following string. The second line contains a string $S$ representing the given operation sequence. You can assume that $S$ follows the above BNF. You can also assume numbers representing rows and columns are no less than 1 and no more than $N$, and the number of each repetition is no less than 1 and no more than $10^9$ in the given string. Output Output the matrix after the operations in $N$ lines, where the $i$-th line contains single-space separated $N$ integers representing the $i$-th row of $A$ after the operations. Sample Input 1 3 2 R1 Output for the Sample Input 1 3 1 2 4 5 6 7 8 9 Sample Input 2 3 7 (U2)300 Output for the Sample Input 2 1 2 3 4 5 6 7 8 9 Sample Input 3 3 7 (R1D1)3 Output for the Sample Input 3 3 4 7 1 5 6 2 8 9
37,553
A Two Floors Dungeon It was the last day of the summer camp you strayed into the labyrinth on the way to Komaba Campus, the University of Tokyo. The contest has just begun. Your teammates must impatiently wait for you. So you have to escape from this labyrinth as soon as possible. The labyrinth is represented by a grid map. Initially, each grid except for walls and stairs is either on the first floor or on the second floor. Some grids have a switch which can move up or down some of the grids (the grids on the first floor move to the second floor, and the grids on the second floor to the first floor). In each step, you can take one of the following actions: Move to an adjacent grid (includes stairs) on the same floor you are now in. Move to another floor (if you are in the stairs grid). Operate the switch (if you are in a grid with a switch). Luckily, you have just found a map of the labyrinth for some unknown reason. Let's calculate the minimum step to escape from the labyrinth, and go to the place your teammates are waiting! Input The format of the input is as follows. W H M 11 M 12 M 13 ...M 1W M 21 M 22 M 23 ...M 2W ........ M H1 M H2 M H3 ...M HW S MS 111 MS 112 MS 113 ...MS 11W MS 121 MS 122 MS 123 ...MS 12W ........ MS 1H1 MS 1H2 MS 1H3 ...MS 1HW MS 211 MS 212 MS 213 ...MS 21W MS 221 MS 222 MS 223 ...MS 22W ........ MS 2H1 MS 2H2 MS 2H3 ...MS 2HW MS S11 MS S12 MS S13 ...MS S1W MS S21 MS S22 MS S23 ...MS S2W ........ MS SH1 MS SH2 MS SH3 ...MS SHW The first line contains two integers W ( 3 ≀ W ≀ 50 ) and H ( 3 ≀ H ≀ 50 ). They represent the width and height of the labyrinth, respectively. The following H lines represent the initial state of the labyrinth. Each of M ij is one of the following symbols: '#' representing a wall, '|' representing stairs, '_' representing a grid which is initially on the first floor, '^' representing a grid which is initially on the second floor, a lowercase letter from 'a' to 'j' representing a switch the grid has, and the grid is initially on the first floor, an uppercase letter from 'A' to 'J' representing a switch the grid has, and the grid is initially on the second floor, '%' representing the grid you are initially in (which is initially on the first floor) or '&' representing the exit of the labyrinth (which is initially on the first floor). The next line contains one integer S ( 0 ≀ S ≀ 10 ), and then the following SH lines represent the information of the switches. Each of MS kij is one of: '#' if M ij is a '#', '|' if M ij is a '|', '*' if the grid is moved by the switch represented by the k -th alphabet letter, or '.' otherwise. Note that the grid which contains a switch may be moved by operating the switch. In this case, you will move together with the grid. You may assume each of the '%' (start) and '&' (goal) appears exacyly once, that the map is surround by walls, and that each alphabet in the map is any of the letters from 'A' (or 'a') to S -th alphabet letter. Output Print the minimum step to reach the goal in one line. If there is no solution, print "-1". Sample Input 1 6 6 ###### #_|A%# #B#_|# #^BBa# #B&A## ###### 2 ###### #*|*.# #.#.|# #*.**# #...## ###### ###### #*|*.# #*#.|# #..**# #..*## ###### Output for the Sample Input 1 21 Sample Input 2 8 3 ######## #%||Aa&# ######## 2 ######## #*||*..# ######## ######## #.||*.*# ######## Output for the Sample Input 2 7 Sample Input 3 3 4 ### #%# #&# ### 0 Output for the Sample Input 3 1 Sample Input 4 3 5 ### #%# #^# #&# ### 0 Output for the Sample Input 4 -1
37,554
Problem A: Keitai Message Alice さんは Miku さんに携垯電話でメヌルを送ろうずしおいる。 携垯電話には入力に䜿えるボタンは数字のボタンしかない。 そこで、文字の入力をするために数字ボタンを䜕床か抌しお文字の入力を行う。携垯電話の数字ボタンには、次の文字が割り圓おられおおり、ボタン 0 は確定ボタンが割り圓おられおいる。この携垯電話では 1 文字の入力が終わったら必ず確定ボタンを抌すこずになっおいる。 1: . , ! ? (スペヌス) 2: a b c 3: d e f 4: g h i 5: j k l 6: m n o 7: p q r s 8: t u v 9: w x y z 0: 確定ボタン 䟋えば、ボタン 2、ボタン 2、ボタン 0 ず抌すず、文字が 'a' → 'b' ず倉化し、ここで確定ボタンが抌されるので、文字 b が出力される。 同じ数字を続けお入力するず倉化する文字はルヌプする。すなわち、ボタン 2 を 5 回抌しお、次にボタン 0 を抌すず、文字が 'a' → 'b' → 'c' → 'a' → 'b' ず倉化し、ここで確定ボタンを抌されるから 'b' が出力される。 䜕もボタンが抌されおいないずきに確定ボタンを抌すこずはできるが、その堎合には䜕も文字は出力されない。 あなたの仕事は、Alice さんが抌したボタンの列から、Alice さんが䜜ったメッセヌゞを再珟するこずである。 Input 最初の行にテストケヌスの数が䞎えられる。 続いお各テストケヌスに぀いお、最倧で長さ 1024 の数字列が1行で䞎えられる。 Output Alice さんが䜜ったメッセヌゞの文字列をそれぞれのテストケヌスに぀いお 1 行ごずに出力せよ。 ただし、出力は 1 文字以䞊 76 文字未満であるず仮定しおよい。 Sample Input 5 20 220 222220 44033055505550666011011111090666077705550301110 000555555550000330000444000080000200004440000 Output for the Sample Input a b b hello, world! keitai
37,555
Score: 600 points Problem Statement AtCoder's head office consists of N rooms numbered 1 to N . For any two rooms, there is a direct passage connecting these rooms. For security reasons, Takahashi the president asked you to set a level for every passage, which is a positive integer and must satisfy the following condition: For each room i\ (1 \leq i \leq N) , if we leave Room i , pass through some passages whose levels are all equal and get back to Room i , the number of times we pass through a passage is always even. Your task is to set levels to the passages so that the highest level of a passage is minimized. Constraints N is an integer between 2 and 500 (inclusive). Input Input is given from Standard Input in the following format: N Output Print one way to set levels to the passages so that the objective is achieved, as follows: a_{1,2} a_{1,3} ... a_{1,N} a_{2,3} ... a_{2,N} . . . a_{N-1,N} Here a_{i,j} is the level of the passage connecting Room i and Room j . If there are multiple solutions, any of them will be accepted. Sample Input 1 3 Sample Output 1 1 2 1 The following image describes this output: For example, if we leave Room 2 , traverse the path 2 \to 3 \to 2 \to 3 \to 2 \to 1 \to 2 while only passing passages of level 1 and get back to Room 2 , we pass through a passage six times.
37,556
Score : 2000 points Problem Statement You are participating in a quiz with N + M questions and Yes/No answers. It's known in advance that there are N questions with answer Yes and M questions with answer No, but the questions are given to you in random order. You have no idea about correct answers to any of the questions. You answer questions one by one, and for each question you answer, you get to know the correct answer immediately after answering. Suppose you follow a strategy maximizing the expected number of correct answers you give. Let this expected number be P/Q , an irreducible fraction. Let M = 998244353 . It can be proven that a unique integer R between 0 and M - 1 exists such that P = Q \times R modulo M , and it is equal to P \times Q^{-1} modulo M , where Q^{-1} is the modular inverse of Q . Find R . Constraints 1 \leq N, M \leq 500,000 Both N and M are integers. Partial Score 1500 points will be awarded for passing the testset satisfying N = M and 1 \leq N, M \leq 10^5 . Input Input is given from Standard Input in the following format: N M Output Let P/Q be the expected number of correct answers you give if you follow an optimal strategy, represented as an irreducible fraction. Print P \times Q^{-1} modulo 998244353 . Sample Input 1 1 1 Sample Output 1 499122178 There are two questions. You may answer randomly to the first question, and you'll succeed with 50% probability. Then, since you know the second answer is different from the first one, you'll succeed with 100% probability. The expected number of your correct answers is 3 / 2 . Thus, P = 3 , Q = 2 , Q^{-1} = 499122177 (modulo 998244353 ), and P \times Q^{-1} = 499122178 (again, modulo 998244353 ). Sample Input 2 2 2 Sample Output 2 831870297 The expected number of your correct answers is 17 / 6 . Sample Input 3 3 4 Sample Output 3 770074220 The expected number of your correct answers is 169 / 35 . Sample Input 4 10 10 Sample Output 4 208827570 Sample Input 5 42 23 Sample Output 5 362936761
37,557
Score : 900 points Problem Statement Takahashi has a lot of peculiar devices. These cylindrical devices receive balls from left and right. Each device is in one of the two states A and B, and for each state, the device operates as follows: When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B. When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A. The transition of the state of a device happens momentarily and always completes before it receives another ball. Takahashi built a contraption by concatenating N of these devices. In this contraption, A ball that was thrown out from the right side of the i -th device from the left ( 1 \leq i \leq N-1 ) immediately enters the (i+1) -th device from the left side. A ball that was thrown out from the left side of the i -th device from the left ( 2 \leq i \leq N ) immediately enters the (i-1) -th device from the right side. The initial state of the i -th device from the left is represented by the i -th character in a string S . From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end. Here, it can be proved that the ball always comes out of the contraption after a finite time. Find the state of each device after K balls are processed. Constraints 1 \leq N \leq 200,000 1 \leq K \leq 10^9 |S|=N Each character in S is either A or B . Input The input is given from Standard Input in the following format: N K S Output Print a string that represents the state of each device after K balls are processed. The string must be N characters long, and the i -th character must correspond to the state of the i -th device from the left. Sample Input 1 5 1 ABAAA Sample Output 1 BBAAA In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place. Sample Input 2 5 2 ABAAA Sample Output 2 ABBBA Sample Input 3 4 123456789 AABB Sample Output 3 BABA
37,558
Score : 100 points Problem Statement There is an N -car train. You are given an integer i . Find the value of j such that the following statement is true: "the i -th car from the front of the train is the j -th car from the back." Constraints 1 \leq N \leq 100 1 \leq i \leq N Input Input is given from Standard Input in the following format: N i Output Print the answer. Sample Input 1 4 2 Sample Output 1 3 The second car from the front of a 4 -car train is the third car from the back. Sample Input 2 1 1 Sample Output 2 1 Sample Input 3 15 11 Sample Output 3 5
37,559
D: 矢 / Arrow 問題 rodea 君は 1 次元座暙系の䞭におり、 x = 0 の堎所に立っおいる。この䜍眮から、 x = N の䜍眮にある的を目がけお、 垞に速さ 1 で移動する正の敎数の長さの矢を投げる。しかし、rodea 君は非力なため、 区間 0 \leq x \leq N の䞭に合蚈 M 個の送颚機を眮くこずにしおいる。 ここで、矢の先端から根元たでの䜍眮に 1 ぀の送颚機も含たれない堎合を「損倱」ず定矩する。損倱の刀定は、矢の先端が x = 1, 2, 3, $\ldots$ , N に到達した際に行う぀たり、合蚈 N 回行う。 このずき、以䞋のク゚リを Q 回凊理せよ。 「損倱」蚱容可胜な回数 l_i が䞎えられる。すなわち、 N 回の刀定においお、「損倱」が合蚈 l_i 回以䞋なら矢を的に届けるこずが可胜である。このずきの、矢を的に届けるために必芁な最短の矢の長さを求める。 入力圢匏 N M m_1 m_2 $\ldots$ m_M Q l_1 l_2 $\ldots$ l_Q 1 行目に距離 N ず送颚機の個数 M が空癜区切りで䞎えられる。 2 行目に、 M 個の送颚機それぞれの䜍眮が䞎えられる。 m_i=j のずき、 i 番目の送颚機は x=j-1 ず x=j の䞁床䞭間に䜍眮しおいる。 3 行目にク゚リの個数 Q が䞎えられ、 4 行目に「損倱」蚱容可胜な回数 l_i が Q 個䞎えられる。 制玄 1 \leq N \leq 10^5 1 \leq M \leq N 1 \leq m_1 < m_2 < $\ldots$ < m_M \leq N 1 \leq Q \leq 10^5 0 \leq l_i \leq 10^5 ( 1 \leq i \leq Q ) 出力圢匏 䞎えられる Q 個の l_i に察しお考えられる最短の矢の長さを、順に改行しお出力せよ。 ただし、条件を満たす正の敎数の長さの矢が存圚しない堎合は -1 を出力するものずする。 入力䟋1 5 1 2 1 3 出力䟋1 2 矢の先端が x = 1 に到達したずき、先端から根元たでに送颚機は含たれないので、「損倱」回数は 1 になる。 矢の先端が x = 2 に到達したずき、先端から根元たでに送颚機は含たれるので、「損倱」回数は 1 のたたである。 矢の先端が x = 3 に到達したずき、先端から根元たでに送颚機は含たれるので、「損倱」回数は 1 のたたである。 矢の先端が x = 4 に到達したずき、先端から根元たでに送颚機は含たれないので、「損倱」回数は 2 になる。 矢の先端が x = 5 に到達したずき、先端から根元たでに送颚機は含たれないので、「損倱」回数は 3 になる。 長さ 2 より短い矢を投げる堎合、「損倱」回数が 3 よりも倚くなるため、長さ 2 の矢を投げるずきが条件を満たす最短の矢の長さである。 入力䟋2 11 3 2 5 9 3 1 4 8 出力䟋2 4 3 1
37,560
走れ双子 (Run, Twins) E869120君は、家から孊校ぞ向かっお分速 $P$ メヌトルで走り出したした。 square1001君は、E869120君が家を出発しおから $A$ 分埌にE869120君の忘れ物に気が付き、分速 $Q$ メヌトルで远いかけたした。 その埌、E869120君は、E869120君が家を出発しおから $B$ 分埌に忘れものに気が付き、分速 $R$ メヌトルで道を匕き返したした。 E869120君が家を出発しおから䜕分埌に双子は出䌚うでしょうか ただし、$B$ 分埌たでにE869120君ずsquare1001君が出䌚うこずはありたせん。 たた、E869120君ずsquare1001君の家から孊校たでは䞀本道であり、近道や別の道はないものずしたす。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $A$ $B$ $P$ $Q$ $R$ 出力 E869120君が家を出発しおからE869120君ずsquare1001君が出䌚うたでの時間を出力せよ。 ただし、最埌には改行を入れるこず。 なお、想定解答ずの絶察誀差又は盞察誀差が $10^{-3}$ 以内であれば正解ずしお扱われる。 制玄 $1 \leq A \leq B \leq 100$ $1 \leq Q \leq P \leq 100$ $1 \leq R \leq 100$ 入力は党お敎数である。 入力䟋1 14 86 9 1 20 出力䟋1 119.428571428571 入力䟋2 1 4 15 9 2 出力䟋2 7.000000000000 入力䟋3 67 87 7 4 51 出力䟋3 96.618181818182
37,561
Set Union Find the union of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$. Input The input is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ $m$ $b_0 \; b_1 \; ... \; b_{m-1}$ Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set. Output Print elements in the union in ascending order. Print an element in a line. Constraints $1 \leq n, m \leq 200,000$ $0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$ $0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$ Sample Input 1 3 1 5 8 2 5 9 Sample Output 1 1 5 8 9
37,562
B: Shortest Crypt 問題 xryuseixくんの孊校では暗号が流行しおいる. 栌子状の街に䜏んでいるxryuseixくんは集合堎所を決める新しい暗号を考えた. 暗号文は $N$ 文字の文字列 $S$ からなり, $S_i$ の文字によっお珟圚地からの移動方向を決める. 移動方向に関しおは以䞋の通りである. A ~ M : 北にマス進む. N ~ Z : 南にマス進む. a ~ m : 東にマス進む. n ~ z : 西にマス進む. さお,ここでxryuseixくんは, yryuseiyちゃんにデヌトの集合堎所を暗号文で教えようず思ったのだが, 暗号文が冗長なこずに気づいた. 䟋えば,「ANA」ずいう暗号文があったずしよう. これは北に $1$ マス進み, 南ぞ $1$ マス進んだのち北ぞ $1$ マス進む. これは北に $1$ マス進む暗号文ず等䟡なので,「ANA」「A」ず, 簡朔にできる. xryuseixくんはyryuseiyちゃんに遠回りをさせないよう,暗号文を簡朔にしようず思った. そこであなたはxryuseixくんに倉わっお, 暗号文を簡朔にするプログラムを曞くこずになった. なお, 「暗号文を簡朔にする」ずは,「元の暗号文ず同じ目的地ぞ行く最も短い暗号文を䜜る」ずいうこずである. 制玄 $N$ は敎数であり, $S_i$ は倧文字たたは小文字のアルファベットである. $1 \leq N \leq 10^{5}$ 入力圢匏 入力は以䞋の圢匏で䞎えられる. $N$ $S$ 出力 $1$ 行目に簡朔にした埌の暗号文の長さ, $2$ 行目にその暗号文を出力せよ. 答えずしおありうる暗号文が耇数存圚する堎合, どれを出力しおもよい. たた各行の末尟に改行を出力せよ. サンプル サンプル入力 1 5 ANazA サンプル出力 1 1 A サンプル入力 2 7 ACMICPC サンプル出力 2 5 HIJKL サンプル入力 3 4 AIZU サンプル出力 3 0 簡朔にした暗号文(空の文字列)のあずに改行が必芁なこずに泚意.
37,563
Score : 800 points Problem Statement Given is a positive even number N . Find the number of strings s of length N consisting of A , B , and C that satisfy the following condition: s can be converted to the empty string by repeating the following operation: Choose two consecutive characters in s and erase them. However, choosing AB or BA is not allowed. For example, ABBC satisfies the condition for N=4 , because we can convert it as follows: ABBC → (erase BB ) → AC → (erase AC ) → (empty) . The answer can be enormous, so compute the count modulo 998244353 . Constraints 2 \leq N \leq 10^7 N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353 . Sample Input 1 2 Sample Output 1 7 Except AB and BA , all possible strings satisfy the conditions. Sample Input 2 10 Sample Output 2 50007 Sample Input 3 1000000 Sample Output 3 210055358
37,564
Problem D: Sum of Different Primes A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k , you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5+ 3 but they are not distinguished. When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are no other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7,17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn't count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two diffrent primes whose sums are 4. Your job is to write a program that reports the number of such ways for the given n and k . Input The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≀ 1120 and k ≀ 14. Output The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and k specified in the corresponding dataset. You may assume that it is less than 2 31 . Sample Input 24 3 24 2 2 1 1 1 4 2 18 3 17 1 17 3 17 4 100 5 1000 10 1120 14 0 0 Output for the Sample Input 2 3 1 0 0 2 1 0 1 55 200102899 2079324314
37,565
Score : 300 points Problem Statement There are 3N participants in AtCoder Group Contest . The strength of the i -th participant is represented by an integer a_i . They will form N teams, each consisting of three participants. No participant may belong to multiple teams. The strength of a team is defined as the second largest strength among its members. For example, a team of participants of strength 1 , 5 , 2 has a strength 2 , and a team of three participants of strength 3 , 2 , 3 has a strength 3 . Find the maximum possible sum of the strengths of N teams. Constraints 1 ≀ N ≀ 10^5 1 ≀ a_i ≀ 10^{9} a_i are integers. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_{3N} Output Print the answer. Sample Input 1 2 5 2 8 5 1 5 Sample Output 1 10 The following is one formation of teams that maximizes the sum of the strengths of teams: Team 1 : consists of the first, fourth and fifth participants. Team 2 : consists of the second, third and sixth participants. Sample Input 2 10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Sample Output 2 10000000000 The sum of the strengths can be quite large.
37,566
Score : 100 points Problem Statement You are given two integers A and B . Find the largest value among A+B , A-B and A \times B . Constraints -1000 \leq A,B \leq 1000 All values in input are integers. Input Input is given from Standard Input in the following format: A B Output Print the largest value among A+B , A-B and A \times B . Sample Input 1 3 1 Sample Output 1 4 3+1=4 , 3-1=2 and 3 \times 1=3 . The largest among them is 4 . Sample Input 2 4 -2 Sample Output 2 6 The largest is 4 - (-2) = 6 . Sample Input 3 0 0 Sample Output 3 0
37,567
Problem 06: Ghost Buster! 䞀般人には知る由も無いこずだが、この街は幜霊で溢れおいる。そのほずんどは無害なのだが、困ったこずに、人を呪う悪霊も少なからずいるのだ。 あるずころに、そんな悪霊ず戊う少女がいた。圌女は昌間は䜕食わぬ顔で高校に通いながらも、倜になるず街䞭を歩き回り、圷埚える魂を芋付けおは成仏させるのだ。 幜霊を成仏させるためにはたずその幜霊に接近する必芁があるのだが、それは簡単なこずではない。幜霊は物をすり抜けられるからだ。幜霊は民家に壁から䟵入しお勝手口から出お行ったり、信号の無い倧通りを悠々ず暪断したりする。人間である圌女にはそんなこずはできないので、幜霊を远跡するのはずおも難しいのだ。 圌女は経隓䞊、幜霊が芏則的な動きをするこずを知っおいた。そこで圌女は、この情報を掻甚しお幜霊を远跡するこずを考えた。 圌女のいる街は、( H × W )マスのグリッドずしお衚される。圌女も幜霊も、1分間に1回、次の5぀の行動 東ぞ1マス進む 西ぞ1マス進む 南ぞ1マス進む 北ぞ1マス進む その堎に留たる のうちどれか1぀を行うこずができる。ただし、圌女は街の呚囲に結界を匵ったので、圌女も幜霊も街の倖に出るこずはできない。たた、そのような行動をしようずした堎合、代わりにその堎に留たる。 それぞれのマスは ' # ' たたは ' . ' ずいう文字によっお衚され、前者は幜霊だけが進入できるマスを、埌者は䞡者が進入できるマスを衚す。 幜霊の行動パタヌンは、L個の行動の列ずしお䞎えられる。幜霊はこれらの行動を最初から順番に実行しおいき、党おの行動を実行し終わるずたた最初の行動に戻り、順に実行し続ける。 圌女ず幜霊が N 分埌に同じマスにいたならば、圌女ず幜霊は時刻 N で遭遇したず蚀うこずが出来る。幜霊は攟っおおくずどんな被害をもたらすか分からないので、圌女は出来るだけ早い時刻に幜霊を捕たえたい。なので、圌女は頌れる先茩であるあなたに、圌女ず幜霊が遭遇できる最も早い時刻ずその遭遇堎所を求めるプログラムを曞いおほしいず思っおいる。 さあ、圌女を助けお、圌女のありがたい蚀葉で圷埚える魂を極楜浄土ぞず導くのだ Input 耇数のデヌタセットが䞎えられる。 各デヌタセットの圢匏を以䞋に瀺す。 H W グリッドの南北方向のマスの数、グリッドの東西方向のマスの数 空癜区切りの぀の敎数 H × W の文字 マスの皮類' . ', ' # ', ' A ', たたは ' B ' pattern 幜霊の行動パタン文字列 ' A ' は少女の初期䜍眮、' B ' は幜霊の初期䜍眮を瀺す。䞡者の初期䜍眮は、 ' . ' のマスの䞊にある。 pattern は ' 5 ', ' 2 ', ' 4 ', ' 6 ', ' 8 ' から成る文字列で、各文字の意味は以䞋の通りである ' 5 ' その堎に留たる ' 8 ' 北に1マス進む ' 6 ' 東に1マス進む ' 4 ' 西に1マス進む ' 2 ' 南に1マス進む 䟋えば、 pattern が " 8544 " の堎合、幜霊は「北に1マス進む」「その堎に留たる」「西に1マス進む」「西に1マス進む」の行動パタンを繰り返す。 H , W は 1 以䞊 20 以䞋であり、 pattern の長さは 10 以䞋である。 H , W がずもに 0 のずき、入力は終了する。 Output もし少女が幜霊ず遭遇できるならば、最も早い遭遇時刻ず、その遭遇堎所の南北方向の座暙、東西方向の座暙を空癜で区切っお1行に出力せよ。グリッドの最北西を座暙(0, 0)ずし、最南東を座暙( H -1, W -1) ずする。 どのように行動しおも絶察に遭遇できない堎合は、 " impossible " ず出力せよ。 Sample Input 4 7 A#...#B .#.#.#. .#.#.#. ...#... 5 4 7 A#...#B .#.#.#. .#.#.#. ...#... 2 4 7 A#...#B .#.#.#. .#.#.#. ...#... 4 4 7 A#...#B .#.#.#. .#.#.#. ...#... 442668 1 10 A#.......B 55555554 1 10 A#.......B 55555555 0 0 Output for the Sample Input 18 0 6 15 3 6 6 0 0 14 0 4 72 0 0 impossible
37,568
Eバむナリ列 - Binary Sequence - 物語 わしはバむナリ倧奜きBonald.Brvin.Bnuthじゃ オフトゥンフォヌド倧孊でバむナリ列の性質を研究しおおっおのお、Bnuthおじさんず呌ばれお、芪したれおおるぞい ちなみに、わしの名前「Bonald.Brvin.Bnuth」をASCIIコヌドからバむナリ化するず「1000010 1101111 1101110 1100001 1101100 1100100 101110 1000010 1110010 1110110 1101001 1101110 101110 1000010 1101110 1110101 1110100 1101000」になるぞい 楜しいのお 今日は、匟子が䜕やら面癜そうな実隓をするず蚀うからのお、朝から楜しみなんじゃ どれどれ、どんなこずをするんじゃの なんずバむナリ列を曞き換え続けるこずで、䜕らかの性質を芋出したいずな ふむこれは、重芁な発芋の匂いがぷんぷんするぞい さあ、さあ、実隓を始めようではないか より詳现な蚭定を文章ずしおたずめおおくぞい 問題 長さ n のバむナリ列 x = (x_1, ..., x_n) ( x_i \in \{0,1\} , i = 1,...,n ) が䞎えられる。バむナリ列 x に察しお2぀の関数 f(x) ず g(x) を以䞋のように定矩する。 f(x) = Σ_{i=1}^{n} x_i = x_1 + x_2 + ... + x_n g(x) = Σ_{i=1}^{n-1} x_i x_{i+1} = x_1 x_2 + x_2 x_3 + ... x_{n-1} x_n 今、バむナリ列 x に察しお以䞋のような倉曎操䜜を q 回行う。 j 回目の倉曎操䜜は l_j, r_j, b_j ( 1 \leq l_j \leq r_j \leq n , b_j \in \{0,1\} , j = 1,...,q ) で䞎えられ、これはバむナリ列 x の l_j 番目から r_j 番目を b_j に眮き換える操䜜に察応する。各倉曎操䜜の埌で f(x) - g(x) を求めよ。 入力圢匏 n x q l_1 r_1 b_1 ... l_q r_q b_q 1行目にはバむナリ列の長さを衚す敎数 n が䞎えられる。 2行目にはバむナリ列 x を衚せす文字列が䞎えられる。 3行目にはク゚リの回数を衚す敎数 q が䞎えられる。 続く q 行のうち i 行目には、 i 回目のク゚リに関する情報 l_i, r_i, b_i がこの順で空癜区切りで䞎えられる。 制玄 2 \leq n \leq 100000 x は’0’、’1’からなる文字列 |x| = n 1 \leq q \leq 100000 1 \leq l_j \leq r_j \leq n , b_j \in \{0,1\} , j = 1,...,q 出力圢匏 各 i=1,...,q に぀いお、 i 行目に i 回目のク゚リを凊理した埌の f(x) - g(x) の倀を出力せよ。 入力䟋1 10 0101100110 3 3 3 1 1 6 0 2 5 1 出力䟋1 2 1 2 入力䟋2 8 00111100 3 8 8 1 4 5 0 7 8 1 出力䟋2 2 3 2
37,569
ブロックの䞉角圢 図a のように積たれたブロックに察し、以䞋の䞊べ替え操䜜を繰り返す。 䞀番䞋のブロック党お図a 䞭の癜のブロックを右端に新しく積み䞊げる残りのブロックは自動的に段䞋に萜ち、図b のようになる。 ブロックの間に隙間ができたら、巊に詰めお隙間をなくす図 b から図c のようになる。 1 以䞊の敎数 k に察しお、k×(k1)/2 で衚される数 (䟋:1, 3, 6, 10, ...)を䞉角数ずいう。ブロックの総数が䞉角数の堎合、䞊蚘の䞊べ替えを繰り返すず、巊端の高さが1 で右に向かっお぀ず぀高くなっおいくような䞉角圢になるず予想されおいる図d は総数が15 個の堎合。 ブロックの最初の䞊びが䞎えられたずき、あらかじめ決められた回数以䞋の操䜜で、䞊で説明したようなブロックの䞉角圢ができるずき、䞉角圢が埗られるたでの最小の操䜜回数を出力するプログラムを䜜成しおください。 入力 入力は耇数のデヌタセットからなる。入力の終わりはれロ぀の行で瀺される。各デヌタセットは以䞋の圢匏で䞎えられる。 N b 1 b 2 ... b N 各デヌタセットは行であり、ブロックの最初の䞊びを衚す。N (1 ≀ N ≀ 100)は、䞀番䞋の段にあるブロックの数を瀺す。b i (1 ≀ bi ≀ 10000) は巊から i 番目の䜍眮に積たれおいるブロックの数を瀺す。b i ず b i+1 は぀の空癜で区切られおいる。ブロックの総数は 3 以䞊である。 デヌタセットの数は 20 を超えない。 出力 デヌタセットごずに、䞉角圢ができるたでに行った䞊べ替え操䜜の回数を行に出力する。ただし、䞉角圢が䜜れない堎合や、操䜜回数が 10000 回を超える堎合は -1 を出力する。 入力䟋 6 1 4 1 3 2 4 5 1 2 3 4 5 10 1 1 1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 1 1 12 1 4 1 3 2 4 3 3 2 1 2 2 1 5050 3 10000 10000 100 0 出力䟋 24 0 10 -1 48 5049 -1 最初のデヌタセットが、図に瀺した堎合に察応する。 ぀目のデヌタセットが、ブロックの総数が䞉角数でないため、䞉角圢が䜜れない堎合に察応する。 最埌のデヌタセットが、ブロックの総数は䞉角数だが、操䜜回数が 10000 回を超える堎合に察応する。
37,570
Problem C: Push!! Mr. Schwarz was a famous powerful pro wrestler. He starts a part time job as a warehouseman. His task is to move a cargo to a goal by repeatedly pushing the cargo in the warehouse, of course, without breaking the walls and the pillars of the warehouse. There may be some pillars in the warehouse. Except for the locations of the pillars, the floor of the warehouse is paved with square tiles whose size fits with the cargo. Each pillar occupies the same area as a tile. Initially, the cargo is on the center of a tile. With one push, he can move the cargo onto the center of an adjacent tile if he is in proper position. The tile onto which he will move the cargo must be one of (at most) four tiles (i.e., east, west, north or south) adjacent to the tile where the cargo is present. To push, he must also be on the tile adjacent to the present tile. He can only push the cargo in the same direction as he faces to it and he cannot pull it. So, when the cargo is on the tile next to a wall (or a pillar), he can only move it along the wall (or the pillar). Furthermore, once he places it on a corner tile, he cannot move it anymore. He can change his position, if there is a path to the position without obstacles (such as the cargo and pillars) in the way. The goal is not an obstacle. In addition, he can move only in the four directions (i.e., east, west, north or south) and change his direction only at the center of a tile. As he is not so young, he wants to save his energy by keeping the number of required pushes as small as possible. But he does not mind the count of his pedometer, because walking is very light exercise for him. Your job is to write a program that outputs the minimum number of pushes required to move the cargo to the goal, if ever possible. Input The input consists of multiple maps, each representing the size and the arrangement of the warehouse. A map is given in the following format. w h d 11 d 12 d 13 ... d 1 w d 21 d 22 d 23 ... d 2 w ... d h 1 d h 2 d h 3 ... d h w The integers w and h are the lengths of the two sides of the floor of the warehouse in terms of widths of floor tiles. w and h are less than or equal to 7. The integer d ij represents what is initially on the corresponding floor area in the following way. 0: nothing (simply a floor tile) 1: a pillar 2: the cargo 3: the goal 4: the warehouseman (Mr. Schwarz) Each of the integers 2, 3 and 4 appears exactly once as d ij in the map. Integer numbers in an input line are separated by at least one space character. The end of the input is indicated by a line containing two zeros. Output For each map, your program should output a line containing the minimum number of pushes. If the cargo cannot be moved to the goal, -1 should be output instead. Sample Input 5 5 0 0 0 0 0 4 2 0 1 1 0 1 0 0 0 1 0 0 0 3 1 0 0 0 0 5 3 4 0 0 0 0 2 0 0 0 0 0 0 0 0 3 7 5 1 1 4 1 0 0 0 1 1 2 1 0 0 0 3 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 6 6 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 4 0 0 0 0 0 0 0 Output for the Sample Input 5 -1 11 8
37,571
Problem H: Caterpillar ずある倩空郜垂に䜏む倧孊生のGは、芋虫のいも倪郎を飌っおいる。 圌は、いも倪郎に最短歩数で党おのえさを順番に食べるように躟けをした。圌の友人であるあなたは、圌からいも倪郎が本圓に躟けられおいるかどうかを調べお欲しいず䟝頌されたので、プログラムを曞くこずにした。 Problem 耇数の障害物ず゚サがグリッド状の゚リア内に存圚する。頭ず 5 ぀の胎䜓を持぀芋虫がこの゚リアを探玢する。芋虫が順番に最埌たで゚サを食べたずきの最小の歩数を求めよ。 ただし党おの゚サを食べるこずが䞍可胜な堎合は -1 を出力するこず。なお、゚サの䞊に芋虫の頭が重なったずき、゚サを食べるこずができる。 ゚リアは以䞋の文字で構成される。 䜕もないマス -> ‘.’ 障害物 -> ‘#’ ゚サ -> ‘1’, ‘2’, ..., ‘9’ 芋虫の初期䜍眮 -> é ­ ‘S’, 胎䜓 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ 芋虫は頭 ’S’ から 5 ぀の胎䜓 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ず 順に䞊䞋巊右の 4 方向に隣接しお、それらが連なっお構成される。 䟋えば、芋虫の構成を衚したずき、以䞋の (1) は正しい芋虫の構成であるが、 (2) や (3) のような構成はありえない。 (1) 正しい .bc Sad ..e (2) eが存圚しない Sab .dc (3) "Sab" ず "cde" ずで芋虫が切断されおいる Sab cde (1) のような正しい芋虫の構成は、入力で䞎えられた状態から任意の回数の芋虫の移動の間、䞀貫しお保たれる。 ぀たり、芋虫の頭が、隣接しおいる 4 ぀のマスのうちのいずれかの空いおいるマスに䞀歩進むず、その埌に続いお 1 番目の胎䜓は頭が元あったマスぞ動き、2 番目の胎䜓は 1 番目の胎䜓が元あったマスぞ、 3 番目は 2 番目の元ぞ‥  ずいうように頭の軌跡をたどるように胎䜓が動く。以䞋は、マス巊ぞ動くずきの䟋である。 移動前 移動埌 ...... ...... ...bc. ...cd. ..Sad. .Sabe. ....e. ...... 芋虫の移動できる堎所は以䞋のように定められおいる。 芋虫は自分の胎䜓、障害物、゚リア倖を移動するこずは出来ない。ただし゚サの䞊を、その゚サを食べずに移動するこずは出来る。 移動前に胎䜓が進む先にあった堎合でも、移動埌に頭ず胎䜓が重ならないならば移動できる。 Input H W N area 入力は H + 1 行で䞎えられる。1行目には 3぀の敎数 H , W , N ( 2 ≀ H ≀ 10, 2 ≀ W ≀ 10, 1 ≀ N ≀ 9 ) が曞かれおいる。 H ぱリアの高さ、 W は幅、 N が゚サの個数を衚す ( このずき 6 + N ≀ H × W が必ず成立する ) 。 2 行目から H + 1 行目たでの各行には ÂŽSÂŽ,ÂŽ1ÂŽ, 2ÂŽ , 
 ÂŽ9ÂŽ,ÂŽaÂŽ,ÂŽbÂŽ,ÂŽcÂŽ,ÂŽdÂŽ,ÂŽeÂŽ,ÂŽ#ÂŽ,ÂŽ.ÂŽ からなる W 文字の文字列が曞かれおおり各々が゚リアの各区画の状態を衚しおいる。たた ÂŽSÂŽ,ÂŽaÂŽ,ÂŽbÂŽ,ÂŽcÂŽ,ÂŽdÂŽ,ÂŽeÂŽ は芋虫の初期状態を衚しおいる。初期状態の芋虫に芆われるようにしお゚サや障害物は存圚しない。たた、芋虫の初期䜍眮および゚サの䜍眮は正しく入力されるこずが保蚌されおいる。 Output 芋虫が゚サを順番通りに食べたずきの最小の歩数、たたそれが䞍可胜なら -1 を出力せよ。 Sample Input1 5 8 3 #....... #.####2# #.#.3..# #.###### .1Sabcde Sample Output1 14 Sample Input2 2 6 2 .1.baS .2.cde Sample Output2 7 Sample Input3 2 6 2 .1#baS .2.cde Sample Output3 -1
37,572
問題文 文字列 $S$ が䞎えられる。$S$ のすべおのアナグラムのうち、回文になっおいるものの個数を求めよ。 文字列 $X$ が $Y$ のアナグラムであるずは、$X$ が $Y$ ず等しいか、$X$ の文字を䞊び替えたら $Y$ に等しくなるこずをいう。䟋えば文字列abcdに察しお、abcdやcbdaなどはアナグラムであるが、abedやcabやabcddなどはアナグラムでない。 文字列 $X$ が回文であるずは、$X$ を逆から読んだものが $X$ 自身ず等しくなるこずをいう。䟋えばabcやabは回文でなく、aやabccbaなどは回文である。 入力 入力は以䞋の圢匏に埓う。 $S$ 制玄 $1 \leq |S| \leq 40$ ( $|S|$ は文字列 $S$ の長さ) $S$ は小文字の英字のみを含む。 答は $2^{63}$ 未満であるこずが保蚌される。 出力 個数を1行に出力せよ。 Sample Input 1 ab Output for the Sample Input 1 0 abのアナグラムはabずbaの二぀があるが、どちらも回文になっおいない。 Sample Input 2 abba Output for the Sample Input 2 2 abbaずbaabの二぀の文字列が回文か぀アナグラムになっおいる。
37,573
Problem I: Roads in a City Roads in a city play important roles in development of the city. Once a road is built, people start their living around the road. A broader road has a bigger capacity, that is, people can live on a wider area around the road. Interstellar Conglomerate of Plantation and Colonization (ICPC) is now planning to develop roads on a new territory. The new territory is a square and is completely clear. ICPC has already made several plans, but there are some difficulties in judging which plan is the best. Therefore, ICPC has collected several great programmers including you, and asked them to write programs that compute several metrics for each plan. Fortunately your task is a rather simple one. You are asked to compute the area where people can live from the given information about the locations and the capacities of the roads. The following figure shows the first plan given as the sample input. Figure 1: The first plan given as the sample input Input The input consists of a number of plans. The first line of each plan denotes the number n of roads ( n ≀ 50), and the following n lines provide five integers x 1 , y 1 , x 2 , y 2 , and r , separated by a space. ( x 1 , y 1 ) and ( x 2 , y 2 ) denote the two endpoints of a road (-15 ≀ x 1 , y 1 , x 2 , y 2 ≀ 15), and the value r denotes the capacity that is represented by the maximum distance from the road where people can live ( r ≀ 10). The end of the input is indicated by a line that contains only a single zero. The territory is located at -5 ≀ x , y ≀ 5. You may assume that each road forms a straight line segment and that any lines do not degenerate. Output Print the area where people can live in one line for each plan. You may print an arbitrary number of digits after the decimal points, provided that difference from the exact answer is not greater than 0.01. Sample Input 2 0 -12 0 0 2 0 0 12 0 2 0 Output for the Sample Input 39.14159
37,574
Problem D: So Sleepy You have an appointment to meet a friend of yours today, but you are so sleepy because you didn’t sleep well last night. As you will go by trains to the station for the rendezvous, you can have a sleep on a train. You can start to sleep as soon as you get on a train and keep asleep just until you get off. However, because of your habitude, you can sleep only on one train on your way to the destination. Given the time schedule of trains as well as your departure time and your appointed time, your task is to write a program which outputs the longest possible time duration of your sleep in a train, under the condition that you can reach the destination by the appointed time. Input The input consists of multiple datasets. Each dataset looks like below: S T D Time D A Time A N 1 K 1,1 Time 1,1 ... K 1, N 1 Time 1, N 1 N 2 K 2,1 Time 2,1 ... K 2, N 2 Time 2, N 2 ... N T K T ,1 Time T ,1 ... K T , N T Time T , N T The first line of each dataset contains S (1 ≀ S ≀ 1000) and T (0 ≀ T ≀ 100), which denote the numbers of stations and trains respectively. The second line contains the departure station ( D ), the departure time ( Time D ), the appointed station ( A ), and the appointed time ( Time A ), in this order. Then T sets of time schedule for trains follow. On the first line of the i -th set, there will be N i which indicates the number of stations the i -th train stops at. Then N i lines follow, each of which contains the station identifier ( K i,j ) followed by the time when the train stops at (i.e. arrives at and/or departs from) that station ( Time i,j ). Each station has a unique identifier, which is an integer between 1 and S . Each time is given in the format hh : mm , where hh represents the two-digit hour ranging from “00” to “23”, and mm represents the two-digit minute from “00” to “59”. The input is terminated by a line that contains two zeros. You may assume the following: each train stops at two stations or more; each train never stops at the same station more than once; a train takes at least one minute from one station to the next; all the times that appear in each dataset are those of the same day; and as being an expert in transfer, you can catch other trains that depart at the time just you arrive the station. Output For each dataset, your program must output the maximum time in minutes you can sleep if you can reach to the destination station in time, or “impossible” (without the quotes) otherwise. Sample Input 3 1 1 09:00 3 10:00 3 1 09:10 2 09:30 3 09:40 3 2 1 09:00 1 10:00 3 1 09:10 2 09:30 3 09:40 3 3 09:20 2 09:30 1 10:00 1 0 1 09:00 1 10:00 1 0 1 10:00 1 09:00 3 1 1 09:00 3 09:35 3 1 09:10 2 09:30 3 09:40 4 3 1 09:00 4 11:00 3 1 09:10 2 09:20 4 09:40 3 1 10:30 3 10:40 4 10:50 4 1 08:50 2 09:30 3 10:30 4 11:10 0 0 Output for the Sample Input 30 30 0 impossible impossible 60
37,575
Transparent Mahjong F: 透明な麻雀牌 あなたは闇に舞い降りた倩灜ず噂されるギャンブラヌの鷹巣さんず麻雀で察局するこずになった 鷹巣さんは倩灜ず噂されるだけあっお鷹巣牌ずいう奇怪な麻雀牌を甚いた察局を提案しおきた 麻雀ずはプレむダヌが互いに手牌ずいう耇数枚の牌を持ち合い自分の手牌を盞手よりもはやくアガリの圢にするこずで勝利が確定するゲヌムである 麻雀の牌の衚偎にはその牌が䜕であるかが描かれおおり牌の裏偎には䜕も描かれおいない よっおあなたが麻雀で察局するずきはあなたの手牌の衚偎をあなたの偎に裏偎を察局者の偎に向けるずよい こうするこずであなたの手牌が䜕であるかはあなたのみが知るこずができる 麻雀の醍醐味はプレむダヌが互の手配を読み合いそれを楜しむこずにある しかしながら鷹巣牌は透明な麻雀牌である 鷹巣牌は透明であるためプレむダヌは互いに盞手の手牌を知るこずができる これでは手配の読み合いを楜しむこずができない 「もはやこれは麻雀ではない」ず鷹巣牌を甚いるこずに玍埗できないあなたは鷹巣さんずの玆䜙曲折を経た協議の結果鷹巣牌ず普通の麻雀牌を混ぜお察局するこずになった 手始めにあなたは鷹巣さんの手牌のアガリ牌が䜕であるかを予想するこずにした 鷹巣さんの手牌は鷹巣牌ず普通の牌から構成される 問題の簡単のため鷹巣牌ず普通の牌は1から12の12皮類各4枚からなるものずする 3 n +2枚の牌からなる手牌に぀いお次の3぀の条件が満たされるずきその手牌はアガリである 同じ数字を組み合わせた牌の組が1぀存圚する 残りの3 n 枚の牌は3個の数字の組合せの n グルヌプになる それぞれのグルヌプは同じ数字を3぀組み合わせたものか3぀の連続する数字を組み合わせたものである 䟋えば 1 1 1 3 3 3 5 5 5 7 7 7 8 9 のような14枚の牌は党お鷹巣牌で構成されおいる この堎合次のように2぀の牌の組合せ1぀ず4぀のグルヌプを䜜るこずができアガリである (1 1 1) (3 3 3) (5 5 5) (7 7) (7 8 9) 次に普通の牌ず鷹巣牌が混ざった手牌の䟋を挙げる 1 1 1 3 3 3 5 5 5 7 7 7 * * のような14枚の牌は12枚の鷹巣牌ず2枚の普通の牌(*)で構成されおいる この堎合次のように2぀の普通の牌が8ず9であるかもしれないので2぀の牌の組合せ1぀ず4぀のグルヌプを䜜るこずができアガリであるかもしれない (1 1 1) (3 3 3) (5 5 5) (7 7) (7 [8] [9]) 同じ䟋に぀いお次のように2぀の普通の牌が8ず8であるかもしれないので以䞋のような圢のアガリであるかもしれない (1 1 1) (3 3 3) (5 5 5) (7 7 7) ([8] [8]) しかし7ずいう同じ牌は4枚たでしか䜿えないので以䞋のような圢のアガリであるこずはない (1 1 1) (3 3 3) (5 5 5) (7 7 7) ([7] [7]) 鷹巣さんの3 n +1枚の手牌が入力ずしお䞎えられたずきに 鷹巣さんの手牌に1枚の牌を加えお3 n +2枚の手牌を䜜るこずを考える このずきに3 n +2枚の手牌がアガリになり埗るような加える1枚の牌(アガリ牌)を党お求めよ Input 入力は次の圢匏で䞎えられる n 牌 1 牌 2 ... 牌 3n+1 入力デヌタは次の条件を満たす n は0 <= n <= 15を満たす 各牌は鷹巣牌か普通の牌である鷹巣牌の堎合は1以䞊12以䞋の敎数で衚され普通の牌の堎合は文字*で衚される Output アガリ牌の䞀芧を昇順に出力せよアガリ牌を1぀出力する床に改行を出力せよ あがり牌がない堎合は-1を1行に衚瀺せよ Sample Input 1 4 1 1 1 3 3 3 5 5 5 7 7 7 9 Sample Output 1 8 9 Sample Input 2 4 1 2 2 3 3 4 5 6 7 8 8 8 9 Sample Output 2 1 4 7 9 10 Sample Input 3 4 1 1 1 4 4 4 7 7 7 8 8 9 * Sample Output 3 6 7 8 9 10 11 Sample Input 4 4 1 * * * * * * * * * * * * Sample Output 4 1 2 3 4 5 6 7 8 9 10 11 12 Sample Input 5 1 3 * 1 4 Sample Output 5 1 2 4 5
37,576
Bridge Removal ICPC islands once had been a popular tourist destination. For nature preservation, however, the government decided to prohibit entrance to the islands, and to remove all the man-made structures there. The hardest part of the project is to remove all the bridges connecting the islands. There are n islands and n -1 bridges. The bridges are built so that all the islands are reachable from all the other islands by crossing one or more bridges. The bridge removal team can choose any island as the starting point, and can repeat either of the following steps. Move to another island by crossing a bridge that is connected to the current island. Remove one bridge that is connected to the current island, and stay at the same island after the removal. Of course, a bridge, once removed, cannot be crossed in either direction. Crossing or removing a bridge both takes time proportional to the length of the bridge. Your task is to compute the shortest time necessary for removing all the bridges. Note that the island where the team starts can differ from where the team finishes the work. Input The input consists of at most 100 datasets. Each dataset is formatted as follows. n p 2 p 3 ... p n d 2 d 3 ... d n The first integer n (3 ≀ n ≀ 800) is the number of the islands. The islands are numbered from 1 to n . The second line contains n -1 island numbers p i (1 ≀ p i < i ), and tells that for each i from 2 to n the island i and the island p i are connected by a bridge. The third line contains n -1 integers d i (1 ≀ d i ≀ 100,000) each denoting the length of the corresponding bridge. That is, the length of the bridge connecting the island i and p i is d i . It takes d i units of time to cross the bridge, and also the same units of time to remove it. Note that, with this input format, it is assured that all the islands are reachable each other by crossing one or more bridges. The input ends with a line with a single zero. Output For each dataset, print the minimum time units required to remove all the bridges in a single line. Each line should not have any character other than this number. Sample Input 4 1 2 3 10 20 30 10 1 2 2 1 5 5 1 8 8 10 1 1 20 1 1 30 1 1 3 1 1 1 1 0 Output for the Sample Input 80 136 2
37,577
Score : 900 points Problem Statement There are N pinholes on the xy -plane. The i -th pinhole is located at (x_i,y_i) . We will denote the Manhattan distance between the i -th and j -th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|) . You have a peculiar pair of compasses, called Manhattan Compass . This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p -th and q -th pinholes, and the state where it points at the q -th and p -th pinholes. When the compass points at the p -th and q -th pinholes and d(p,q)=d(p,r) , one of the legs can be moved so that the compass will point at the p -th and r -th pinholes. Initially, the compass points at the a -th and b -th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints 2≩N≩10^5 1≩x_i, y_i≩10^9 1≩a < b≩N When i ≠ j , (x_i, y_i) ≠ (x_j, y_j) x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Sample Input 1 5 1 2 1 1 4 3 6 1 5 5 4 8 Sample Output 1 4 Initially, the compass points at the first and second pinholes. Since d(1,2) = d(1,3) , the compass can be moved so that it will point at the first and third pinholes. Since d(1,3) = d(3,4) , the compass can also point at the third and fourth pinholes. Since d(1,2) = d(2,5) , the compass can also point at the second and fifth pinholes. No other pairs of pinholes can be pointed by the compass, thus the answer is 4 . Sample Input 2 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Sample Output 2 4 Sample Input 3 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Sample Output 3 7
37,578
A: ç§€ / Steelyard 問題文 情倪くんは長さ $2L$ の棒を䜿っお䞋の図のような秀を䜜った 棒には等間隔に $2L+1$ 個の穎があけられ巊から順に $-L, -L+1, \cdots, -1, 0, 1, \cdots, L-1, L$ ずいう番号が付けられおいるそしお $0$ 番の穎の堎所を倩井から玐で吊るしおある 情倪くんは秀の穎に $N$ 個のおもりを吊るした$i$ 番目のおもりを吊るした穎の番号は $x_i$ でおもりの重さは $w_i$ であるおもりが $1$ ぀も吊るされない穎や耇数のおもりが吊るされる穎も存圚しうる 情倪くんが吊るしたおもりによっおは秀が傟いおいるかもしれない姉の立子さんは远加でいく぀かの重りを吊るすこずで秀を氎平にしたい (おもりの座暙ず重さ積の総和が $0$ になるずき秀は氎平になる)条件を満たすおもりの吊るし方を $1$ ぀出力しなさい候補が耇数ある堎合はどれを出力しおもよい 入力 $L$ $N$ $x_1 \ w_1$ $\vdots$ $x_N \ w_N$ 入力の制玄 $1 \leq L \leq 30$ $1 \leq N \leq 30$ $|x_i| \leq L$ $1 \leq w_i \leq 30$ 党お敎数 出力 答えは次のような圢匏で出力せよ $1$ 行目の $N’$ は立子さんが吊るしたおもりの数である $1+i$ 行目の $x_i’, w_i’$ はそれぞれ立子さんが吊るしたおもりの䜍眮ず重さである $N'$ $x_1' \ w_1'$ $\vdots$ $x_N' \ w_N'$ 出力の制玄 立子さんが远加で吊り䞋げるおもりは以䞋の条件を満たす必芁がある. $0 \leq N' \leq 50000$ $|x_i'| \leq L$ $1 \leq w_i' \leq 50000$ 党お敎数 サンプル サンプル入力1 3 3 1 1 2 1 3 1 サンプル出力1 1 -3 2 他にも䟋えば以䞋のような出力も正解ずしお扱われる. 2 -3 1 -3 1 これらを図瀺するず次のようになる サンプル入力2 3 3 1 1 0 2 -1 3 サンプル出力2 1 2 1 サンプル入力3 10 4 1 2 2 3 -7 1 -1 1 サンプル出力3 0 秀は最初から釣り合っおいるこずもある
37,579
消える数列、消えない数列 ただお君は頭の䜓操をするために、数列を䜿ったゲヌムをしおいたす。このゲヌムでは、はじめに、からたでの数字がランダムに䞊んだ列が䞎えられたす。ただお君は、数列からその䞀郚分を消しおいきたす。ルヌルは、以䞋の通りです。 数列から、同じ数字が぀以䞊䞊んでいる郚分を適圓に遞ぶ。その郚分を含み、連続しお珟れおいる同じ数字をすべお消す。 消した郚分の右偎に数列が残っおいた堎合は、それを巊に詰めお、数列を぀にたずめる。 䞊の぀の操䜜を繰り返した結果、すべおの数字が消えればゲヌムクリアずなる。 䟋えば、䞊の図のような 1,2,3,3,2,2,1,2,2 ずいう数列の堎合、 巊から数えお、番目、番目のを消すず 1,2,2,2,1,2,2 巊から数えお、番目から番目のを消すず 1,1,2,2 巊から数えお、番目ず番目のを消すず 2,2 巊から数えお、番目ず番目のを消すず、ゲヌムクリアずなりたす。 ただし、どのように数字を消しおもクリアできない数列がありたす。たずえば、1,2,3,3,1,2 や 1,2,3,1,2,3 などの数列です。短い数列であれば、ただお君でもクリアできるかどうかがすぐに分かり、クリアできないず分かれば違う数列にチャレンゞできたすが、長い数列になるずそう簡単にはいきたせん。 䞎えられた数列が䞊のゲヌムをクリアできるかどうか刀定するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N c 1 c 2 ... c N 行目の N (1 ≀ N ≀ 100) は、数列の長さを衚す敎数である。行目には぀の空癜で区切られた N 個の敎数 c i (1 ≀ c i ≀ 9) が䞎えられる。 c i は数列の i 番目の数字を瀺す。 Output 䞊に瀺されたルヌルで数列を消すこずができる堎合は「yes」、できない堎合は「no」を出力する。 Sample Input 1 8 1 2 3 3 2 1 2 2 Sample Output 1 yes Sample Input 2 7 1 2 2 1 1 3 3 Sample Output 2 yes Sample Input 3 16 9 8 8 7 7 6 5 4 4 5 1 1 2 2 3 3 Sample Output 3 no Sample Input 4 5 1 1 2 2 1 Sample Output 4 yes
37,580
おせんべい 問題 IOI補菓では創業以来の䌝統の補法で煎逅せんべいを焌いおいるこの䌝統の補法は炭火で䞀定時間衚偎を焌き衚偎が焌けるず裏返しお炭火で䞀定時間裏偎を焌くずいうものであるこの䌝統を守り぀぀煎逅を機械で焌いおいるこの機械は瞊 R (1 ≀ R ≀ 10) 行, 暪 C (1 ≀ C ≀ 10000) 列の長方圢状に煎逅を䞊べお焌く通垞は自動運転で衚偎が焌けたら䞀斉に煎逅を裏返し裏偎を焌く ある日煎逅を焌いおいるず煎逅を裏返す盎前に地震が起こり䜕枚かの煎逅が裏返っおしたった幞いなこずに炭火の状態は適切なたたであったがこれ以䞊衚偎を焌くず創業以来の䌝統で定められおいる焌き時間を超えおしたい煎逅の衚偎が焌けすぎお商品ずしお出荷できなくなるそこで急いで機械をマニュアル操䜜に倉曎しただ裏返っおいない煎逅だけを裏返そうずしたこの機械は暪の行を䜕行か同時に裏返したり瞊の列を䜕列か同時に裏返したりするこずはできるが残念なこずに煎逅を枚ごず裏返すこずはできない 裏返すのに時間がかかるず地震で裏返らなかった煎逅の衚偎が焌けすぎお商品ずしお出荷できなくなるので暪の䜕行かを同時に回裏返し匕き続き瞊の䜕列かを同時に回裏返しお衚偎を焌きすぎずに䞡面を焌くこずのできる煎逅぀たり「出荷できる煎逅」の枚数をなるべく倚くするこずにした暪の行を行も裏返さないあるいは瞊の列を列も裏返さない堎合も考えるこずにする出荷できる煎逅の枚数の最倧倀を出力するプログラムを曞きなさい 地震の盎埌に煎逅が次の図のような状態になったずする黒い䞞が衚偎が焌ける状態を癜い䞞が裏偎が焌ける状態を衚しおいる 1行目を裏返すず次の図のような状態になる さらに 1列目ず5列目を裏返すず次の図のような状態になるこの状態では出荷できる煎逅は9枚である ヒント R の䞊限 10 は C の䞊限 10000 に比べお小さいこずに泚意せよ 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる 入力の1行目には2぀の敎数 R, C (1 ≀ R ≀ 10, 1 ≀ C ≀ 10 000) が空癜を区切りずしお曞かれおいる続く R 行は地震盎埌の煎逅の状態を衚す (i+1) 行目 (1 ≀ i ≀ R) には C 個の敎数 a i,1 , a i,2 , 

, a i,C が空癜を区切りずしお曞かれおおり a i,j は i 行 j 列 の煎逅の状態を衚しおいる. a i,j が 1 なら衚偎が焌けるこずを 0 なら裏偎が焌けるこずを衚す C, R がずもに 0 のずき入力の終了を瀺す. デヌタセットの数は 5 を超えない 出力 デヌタセットごずに出荷できる煎逅の最倧枚数を1行に出力する 入出力䟋 入力䟋 2 5 0 1 0 1 0 1 0 0 0 1 3 6 1 0 0 0 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 0 出力䟋 9 15 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,581
Division of Big Integers Given two integers $A$ and $B$, compute the quotient, $\frac{A}{B}$. Round down to the nearest decimal. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the quotient in a line. Constraints $-1 \times 10^{1000} \leq A, B \leq 10^{1000}$ $B \ne 0$ Sample Input 1 5 8 Sample Output 1 0 Sample Input 2 100 25 Sample Output 2 4 Sample Input 3 -1 3 Sample Output 3 0 Sample Input 4 12 -3 Sample Output 4 -4
37,582
Sorting Five Numbers Write a program which reads five numbers and sorts them in descending order. Input Input consists of five numbers a , b , c , d and e (-100000 ≀ a , b , c , d , e ≀ 100000). The five numbers are separeted by a space. Output Print the ordered numbers in a line. Adjacent numbers should be separated by a space. Sample Input 3 6 9 7 5 Output for the Sample Input 9 7 6 5 3
37,583
K: 救枈 (Angel Relief) 倩䜿の倩真さんは、ある街を救うこずにした。 この街は南北 $H$ 区画 × 東西 $W$ 区画に分けられた長方圢の圢をしおおり、各区画に家がある。 北から数えお $X$ 番目、西から数えお $Y$ 番目の区画を $(X, Y)$ で衚す。 区画 $(i, j)$ にある家には $A_{i, j}$ 人の人が䜏んでいる。 倩真さんは、蟺が南北たたは東西に平行たたは垂盎な長方圢の゚リアを遞び、その䞭に䜏んでいる人党員を ひずりず぀ 救枈する。 倩真さんは、考えられるすべおの長方圢でこの操䜜を行う。 倩真さんが人を救枈する回数の合蚈を求めよ。 入力 1 行目に、敎数 $H, W$ が空癜区切りで䞎えられる。 続く $H$ 行のうち $i$ 行目には、敎数 $A_{i, 1}, A_{i, 2}, A_{i, 3}, \dots, A_{i, W}$ が空癜区切りで䞎えられる。 出力 倩真さんが人を救枈する回数の合蚈を出力せよ。 制玄 $H, W$ は $1$ 以䞊 $500$ 以䞋の敎数 $A_{i, j}$ はすべお $1$ 以䞊 $9$ 以䞋の敎数である。 入力䟋1 2 2 1 2 4 8 出力䟋1 60 䟋えば、巊䞊を $(1, 1)$、右䞋を $(2, 2)$ ずなるように長方圢の゚リアを遞ぶず、そこにいる $15$ 人をひずりず぀救枈するので、合蚈 $15$ 回の救枈を行う。 $9$ 通りの長方圢の゚リアに察しお、それぞれ $1, 2, 3, 4, 5, 8, 10, 12, 15$ 回の救枈を行うので、合蚈で $60$ 回である。 入力䟋2 2 3 1 2 3 4 5 6 出力䟋2 140
37,584
Score : 100 points Problem Statement Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter. Constraints 2\leq K\leq 100 K is an integer. Input Input is given from Standard Input in the following format: K Output Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). Sample Input 1 3 Sample Output 1 2 Two pairs can be chosen: (2,1) and (2,3) . Sample Input 2 6 Sample Output 2 9 Sample Input 3 11 Sample Output 3 30 Sample Input 4 50 Sample Output 4 625
37,585
Problem J: Tile Puzzle You are visiting Ancient and Contemporary Museum. Today there is held an exhibition on the history of natural science. You have seen many interesting exhibits about ancient, medieval, and modern science and mathematics, and you are in a resting space now. You have found a number of panels there. Each of them is equipped with N × N electric tiles arranged in a square grid. Each tile is lit in one of the following colors: black (unlit), red, green, yellow, blue, magenta, and cyan. Initially all the tiles are in black. When a tile is touched on, that tile and the eight adjacent tiles will change their colors as follows: black -> red, red -> green, green -> yellow, yellow -> blue, blue -> magenta, magenta -> cyan, and cyan -> black. Here, the leftmost and rightmost columns are considered adjacent, and so as the uppermost and lowermost rows. There is a goal pattern for each panel, and you are to change the colors of the tiles as presented in the goal pattern. For example, if you are given the goal pattern shown in the figure below for a panel of 4 × 4, you will touch on the upper-left tile once and then on the lower-right tile twice (note that this might not be the only way). Since you are good at programming, you guess you can find the solution using your computer. So your job in this problem is to write a program for it. Figure 1: Example Goal Pattern Input The input contains a series of datasets. Each dataset is given in the following format: N Row 1 ... Row N N indicates the size (i.e. the number of rows and columns) of the electrical panel (3 ≀ N ≀ 15). Row i describes the goal pattern of the i -th row and contains exactly N numbers separated by a space. The j -th number indicates the color of the j -th column, and it is one of the following: 0 (denoting black), 1 (red), 2 (green), 3 (yellow), 4 (blue), 5 (magenta), and 6 (cyan). The input is terminated by a line containing a single zero. This line is not part of any datasets. Output For each dataset, your program should produce the output of N lines. The i -th line should correspond to the i -th row and contain exactly N numbers separated by a space, where the j -th number should be the number of touches on the tile of the j -th column. The number should be in the range from 0 to 6 inclusive. If there is more than one solution, your program may output any of them. If it is impossible to make the goal pattern, your program should output a single line containing “ -1 ” (without quotes) instead of the N lines. A blank line should follow the output for every dataset (including the last one). Sample Input 4 3 1 2 3 1 1 0 1 2 0 2 2 3 1 2 3 5 3 3 3 0 0 3 3 3 0 0 3 3 0 4 4 0 0 4 4 4 0 0 4 4 4 0 Output for the Sample Input 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0
37,586
Score : 400 points Problem Statement You are given a tree with N vertices. Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices. The i -th edge (1≀i≀N-1) connects Vertices a_i and b_i , and has a length of c_i . You are also given Q queries and an integer K . In the j -th query (1≀j≀Q) : find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K . Constraints 3≀N≀10^5 1≀a_i,b_i≀N (1≀i≀N-1) 1≀c_i≀10^9 (1≀i≀N-1) The given graph is a tree. 1≀Q≀10^5 1≀K≀N 1≀x_j,y_j≀N (1≀j≀Q) x_j≠y_j (1≀j≀Q) x_j≠K,y_j≠K (1≀j≀Q) Input Input is given from Standard Input in the following format: N a_1 b_1 c_1 : a_{N-1} b_{N-1} c_{N-1} Q K x_1 y_1 : x_{Q} y_{Q} Output Print the responses to the queries in Q lines. In the j -th line j(1≀j≀Q) , print the response to the j -th query. Sample Input 1 5 1 2 1 1 3 1 2 4 1 3 5 1 3 1 2 4 2 3 4 5 Sample Output 1 3 2 4 The shortest paths for the three queries are as follows: Query 1 : Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3 Query 2 : Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2 Query 3 : Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4 Sample Input 2 7 1 2 1 1 3 3 1 4 5 1 5 7 1 6 9 1 7 11 3 2 1 3 4 5 6 7 Sample Output 2 5 14 22 The path for each query must pass Vertex K=2 . Sample Input 3 10 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 5 6 1000000000 6 7 1000000000 7 8 1000000000 8 9 1000000000 9 10 1000000000 1 1 9 10 Sample Output 3 17000000000
37,587
One Problem Statement 電車の窓から矎しい山䞊みが芋える 窓は巊䞋隅の座暙が (0, 0) 右䞊隅の座暙が (W, H) の長方圢である 窓からは N ぀の山が芋えおおり i 番目の山は䞊に凞な攟物線 y = a_i (x-p_i)^2 + q_i の圢をしおいる 山ず空ずの境界線の長さを求めよ 次の䞉぀の図は Sample Input に察応しおいる倪線で瀺されおいる郚分が山ず空ずの境界線である Input 入力は以䞋の圢匏に埓う䞎えられる数は党お敎数である W H N a_1 p_1 q_1 ... a_N p_N q_N Constraints 1 ≩ W, H ≩ 100 1 ≩ N ≩ 50 -100 ≩ a_i ≩ -1 0 ≩ p_i ≩ W 1 ≩ q_i ≩ H i \neq j ならば (a_i, p_i, q_i) \neq (a_j, p_j, q_j) Output 山ず空ずの境界線の長さを 1 行に出力せよ 出力する倀は真の倀ずの絶察誀差たたは盞察誀差が 10^{-6} 未満でなければならない Sample Input 1 20 20 1 -1 10 10 Output for the Sample Input 1 21.520346288593280 Sample Input 2 20 20 2 -1 10 10 -2 10 5 Output for the Sample Input 2 21.520346288593280 Sample Input 3 15 100 2 -2 5 100 -2 10 100 Output for the Sample Input 3 126.921542730127873
37,588
Score: 600 points Problem Statement E869120 is initially standing at the origin (0, 0) in a two-dimensional plane. He has N engines, which can be used as follows: When E869120 uses the i -th engine, his X - and Y -coordinate change by x_i and y_i , respectively. In other words, if E869120 uses the i -th engine from coordinates (X, Y) , he will move to the coordinates (X + x_i, Y + y_i) . E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines. He wants to go as far as possible from the origin. Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2} , the distance from the origin. Constraints 1 \leq N \leq 100 -1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000 -1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000 All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : : x_N y_N Output Print the maximum possible final distance from the origin, as a real value. Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10} . Sample Input 1 3 0 10 5 -5 -5 -5 Sample Output 1 10.000000000000000000000000000000000000000000000000 The final distance from the origin can be 10 if we use the engines in one of the following three ways: Use Engine 1 to move to (0, 10) . Use Engine 2 to move to (5, -5) , and then use Engine 3 to move to (0, -10) . Use Engine 3 to move to (-5, -5) , and then use Engine 2 to move to (0, -10) . The distance cannot be greater than 10 , so the maximum possible distance is 10 . Sample Input 2 5 1 1 1 0 0 1 -1 0 0 -1 Sample Output 2 2.828427124746190097603377448419396157139343750753 The maximum possible final distance is 2 \sqrt{2} = 2.82842... . One of the ways to achieve it is: Use Engine 1 to move to (1, 1) , and then use Engine 2 to move to (2, 1) , and finally use Engine 3 to move to (2, 2) . Sample Input 3 5 1 1 2 2 3 3 4 4 5 5 Sample Output 3 21.213203435596425732025330863145471178545078130654 If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5 , we will end up at (15, 15) , with the distance 15 \sqrt{2} = 21.2132... from the origin. Sample Input 4 3 0 0 0 1 1 0 Sample Output 4 1.414213562373095048801688724209698078569671875376 There can be useless engines with (x_i, y_i) = (0, 0) . Sample Input 5 1 90447 91000 Sample Output 5 128303.000000000000000000000000000000000000000000000000 Note that there can be only one engine. Sample Input 6 2 96000 -72000 -72000 54000 Sample Output 6 120000.000000000000000000000000000000000000000000000000 There can be only two engines, too. Sample Input 7 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Sample Output 7 148.660687473185055226120082139313966514489855137208
37,589
Score : 700 points Problem Statement In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N . M bidirectional roads connect these cities. The i -th road connects City A_i and City B_i . Every road connects two distinct cities. Also, for any two cities, there is at most one road that directly connects them. One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi. After the division, each city in Takahashi would belong to either Taka or Hashi. It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi. Here, the following condition should be satisfied: Any two cities in the same state, Taka or Hashi, are directly connected by a road. Find the minimum possible number of roads whose endpoint cities belong to the same state. If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1 . Constraints 2 \leq N \leq 700 0 \leq M \leq N(N-1)/2 1 \leq A_i \leq N 1 \leq B_i \leq N A_i \neq B_i If i \neq j , at least one of the following holds: A_i \neq A_j and B_i \neq B_j . If i \neq j , at least one of the following holds: A_i \neq B_j and B_i \neq A_j . Input Input is given from Standard Input in the following format: N M A_1 B_1 A_2 B_2 : A_M B_M Output Print the answer. Sample Input 1 5 5 1 2 1 3 3 4 3 5 4 5 Sample Output 1 4 For example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong to Hashi, the condition is satisfied. Here, the number of roads whose endpoint cities belong to the same state, is 4 . Sample Input 2 5 1 1 2 Sample Output 2 -1 In this sample, the condition cannot be satisfied regardless of which cities belong to each state. Sample Input 3 4 3 1 2 1 3 2 3 Sample Output 3 3 Sample Input 4 10 39 7 2 7 1 5 6 5 8 9 10 2 8 8 7 3 10 10 1 8 10 2 3 7 4 3 9 4 10 3 4 6 1 6 7 9 5 9 7 6 9 9 4 4 6 7 5 8 3 2 5 9 2 10 7 8 6 8 9 7 3 5 3 4 5 6 3 2 10 5 10 4 2 6 2 8 4 10 6 Sample Output 4 21
37,590
Score : 1000 points Problem Statement There are N oases on a number line. The coordinate of the i -th oases from the left is x_i . Camel hopes to visit all these oases. Initially, the volume of the hump on his back is V . When the volume of the hump is v , water of volume at most v can be stored. Water is only supplied at oases. He can get as much water as he can store at a oasis, and the same oasis can be used any number of times. Camel can travel on the line by either walking or jumping: Walking over a distance of d costs water of volume d from the hump. A walk that leads to a negative amount of stored water cannot be done. Let v be the amount of water stored at the moment. When v>0 , Camel can jump to any point on the line of his choice. After this move, the volume of the hump becomes v/2 (rounded down to the nearest integer), and the amount of stored water becomes 0 . For each of the oases, determine whether it is possible to start from that oasis and visit all the oases. Constraints 2 ≀ N,V ≀ 2 × 10^5 -10^9 ≀ x_1 < x_2 < ... < x_N ≀ 10^9 V and x_i are all integers. Input Input is given from Standard Input in the following format: N V x_1 x_2 ... x_{N} Output Print N lines. The i -th line should contain Possible if it is possible to start from the i -th oasis and visit all the oases, and Impossible otherwise. Sample Input 1 3 2 1 3 6 Sample Output 1 Possible Possible Possible It is possible to start from the first oasis and visit all the oases, as follows: Walk from the first oasis to the second oasis. The amount of stored water becomes 0 . Get water at the second oasis. The amount of stored water becomes 2 . Jump from the second oasis to the third oasis. The amount of stored water becomes 0 , and the volume of the hump becomes 1 . Sample Input 2 7 2 -10 -4 -2 0 2 4 10 Sample Output 2 Impossible Possible Possible Possible Possible Possible Impossible A oasis may be visited any number of times. Sample Input 3 16 19 -49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84 Sample Output 3 Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Impossible Impossible Impossible Impossible
37,591
Score : 500 points Problem Statement Takahashi is at an all-you-can-eat restaurant. The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i -th dish, whose deliciousness is B_i . The restaurant has the following rules: You can only order one dish at a time. The dish ordered will be immediately served and ready to eat. You cannot order the same kind of dish more than once. Until you finish eating the dish already served, you cannot order a new dish. After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served. Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant. What is the maximum possible happiness achieved by making optimal choices? Constraints 2 \leq N \leq 3000 1 \leq T \leq 3000 1 \leq A_i \leq 3000 1 \leq B_i \leq 3000 All values in input are integers. Input Input is given from Standard Input in the following format: N T A_1 B_1 : A_N B_N Output Print the maximum possible happiness Takahashi can achieve. Sample Input 1 2 60 10 10 100 100 Sample Output 1 110 By ordering the first and second dishes in this order, Takahashi's happiness will be 110 . Note that, if we manage to order a dish in time, we can spend any amount of time to eat it. Sample Input 2 3 60 10 10 10 20 10 30 Sample Output 2 60 Takahashi can eat all the dishes within 60 minutes. Sample Input 3 3 60 30 10 30 20 30 30 Sample Output 3 50 By ordering the second and third dishes in this order, Takahashi's happiness will be 50 . We cannot order three dishes, in whatever order we place them. Sample Input 4 10 100 15 23 20 18 13 17 24 12 18 29 19 27 23 21 18 20 27 15 22 25 Sample Output 4 145
37,592
Problem L: Product Problem 䌚接君は、玠数$P$、自然数からなる集合$G$、自然数$A$を䜿っおゲヌムをするこずにしたした。 たず、䌚接君は手元の玙に$1$を曞きたす。その埌、以䞋の䞀連の操䜜を任意の回数行いたす。 $G$から芁玠を䞀぀遞ぶ。これを$g$ずする。 手元の玙に曞かれた数ず$g$ずの積を新しく玙に曞く。 元々玙に曞かれおいた数を消す。 手元の玙に曞かれた数を$P$で割ったあたりず$A$が等しければ䌚接君の勝ちで、そうでなければ負けです。$P$、$G$、$A$が䞎えられたずきに䌚接君が勝぀こずができるか刀定しおください。 Input 入力は以䞋の圢匏で䞎えられる。 $P$ $T$ $Test_1$ $\vdots$ $Test_{T}$ 入力は耇数のテストケヌスからなる。たず$1$行に玠数$P$ずテストケヌスの数$T$が䞎えられる。$P$は党おのテストケヌスで共通である。続く$T$行に各テストケヌスが䞎えられる。 各テストケヌスは以䞋のように䞎えられる。 $|G|$ $G_1$ $\dots$ $G_{|G|}$ $A$ 各テストケヌスでは、$G$の芁玠数、$G$の各芁玠、$A$が順番に空癜で区切られお䞎えられる。 Constraints 入力は以䞋の条件を満たす。 入力はすべおは敎数である。 $2 \le P \le 2^{31}-1$ $1 \le T,|G| \le 10^5$ $1 \le G_i,A \le P-1$ $G_i \ne G_j,$ if $i \ne j$ 党おのテストケヌスの$|G|$の総和は$10^5$を超えない。 Output 各テストケヌスに察しお、䌚接君が勝぀こずができるならば$1$を、そうでなければ$0$を䞀行に出力する。 Sample Input 1 7 3 1 1 2 1 2 1 3 1 2 4 5 Sample Output 1 0 1 0 Sample Input 2 1000000007 8 3 2 9 7 5 3 2 9 5 1000001 3 39 1002 65537 12 2 1000000006 518012930 793649232 10 459268180 313723762 835892239 612038995 90424474 366392946 38051435 854115735 5132833 320534710 421820264 1 1 1 1 1 1000000006 1 1000000006 1 Sample Output 2 0 1 1 1 0 1 0 1
37,593
Testing Circuits A Boolean expression is given. In the expression, each variable appears exactly once. Calculate the number of variable assignments that make the expression evaluate to true. Input A data set consists of only one line. The Boolean expression is given by a string which consists of digits, x, (, ), |, &, and ~. Other characters such as spaces are not contained. The expression never exceeds 1,000,000 characters. The grammar of the expressions is given by the following BNF. <expression> ::= <term> | <expression> "|" <term> <term> ::= <factor> | <term> "&" <factor> <factor> ::= <variable> | "~" <factor> | "(" <expression> ")" <variable> ::= "x" <number> <number> ::= "1" | "2" |... | "999999" | "1000000" The expression obeys this syntax and thus you do not have to care about grammatical errors. When the expression contains N variables, each variable in {x1, x2,..., xN} appears exactly once. Output Output a line containing the number of variable assignments that make the expression evaluate to true in modulo 1,000,000,007. Sample Input 1 (x1&x2) Output for the Sample Input 1 1 Sample Input 2 (x1&x2)|(x3&x4)|(~(x5|x6)&(x7&x8)) Output for the Sample Input 2 121
37,594
Problem I: Hard Beans Problem 倧接倧孊では豆が盛んです。 N 個の豆が䞀盎線䞊に䞊んでいたす。 それぞれ0から順に N −1たで番号がふられおおり、 i 番目の豆の硬さを a i ずしたす。 シアン君は理想の豆の硬さを D だず考えおいたす。しかし、シアン君は面倒くさがりなのであたり遠くにある豆を取りに行きたくありたせん。したがっお、シアン君は l 番目の豆から r 番目の豆の䞭で硬さが D に最も近い豆を知りたいず思っおいたす。 シアン君は Q 個の質問をしおくるので、それぞれの質問に察し閉区間[ l , r ]番目にある | 豆の硬さ − D | の最小倀を求めるプログラムを䜜成しおください。(ただし、| a | は a の絶察倀を衚したす。) Input 入力は以䞋の圢匏で䞎えられる。 N a 0 a 1 ... a N−1 Q l 0 r 0 D 0 l 1 r 1 D 1 . . . l Q−1 r Q−1 D Q−1 1行目に、1぀の敎数 N が䞎えられる。2行目に、 N ぀の敎数が空癜区切りで䞎えられる。3行目に、ク゚リの数が぀の敎数 Q ずしお䞎えられる。続く4行から3+ Q 行たでにク゚リの倀 l , r , D が䞎えられる。 Constraints 入力は以䞋の制玄を満たす。 1 ≀ N ≀ 10 5 0 ≀ |a i | ≀ 10 6 (0 ≀ i ≀ N −1) 1 ≀ Q ≀ 10 5 0 ≀ D i ≀ 10 6 0 ≀ l i ≀ r i ≀ N −1 Output 各ク゚リに察し、 D ず[ l , r ]番目の豆の䞭で硬さ D に最も近い豆の硬さずの差の絶察倀を1行に出力せよ。 Sample Input1 3 1 2 3 3 0 2 2 0 2 4 0 0 2 Sample Output1 0 1 1 Sample Input2 10 4 5 0 21 9 100 12 9 0 8 5 0 3 20 2 5 100 8 9 9 5 5 10 0 9 20 Sample Output2 1 0 1 90 1
37,596
Score : 600 points Problem Statement The squirrel Chokudai has N acorns. One day, he decides to do some trades in multiple precious metal exchanges to make more acorns. His plan is as follows: Get out of the nest with N acorns in his hands. Go to Exchange A and do some trades. Go to Exchange B and do some trades. Go to Exchange A and do some trades. Go back to the nest. In Exchange X (X = A, B) , he can perform the following operations any integer number of times (possibly zero) in any order: Lose g_{X} acorns and gain 1 gram of gold. Gain g_{X} acorns and lose 1 gram of gold. Lose s_{X} acorns and gain 1 gram of silver. Gain s_{X} acorns and lose 1 gram of silver. Lose b_{X} acorns and gain 1 gram of bronze. Gain b_{X} acorns and lose 1 gram of bronze. Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze. What is the maximum number of acorns that he can bring to the nest? Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel. Constraints 1 \leq N \leq 5000 1 \leq g_{X} \leq 5000 1 \leq s_{X} \leq 5000 1 \leq b_{X} \leq 5000 All values in input are integers. Input Input is given from Standard Input in the following format: N g_A s_A b_A g_B s_B b_B Output Print the maximum number of acorns that Chokudai can bring to the nest. Sample Input 1 23 1 1 1 2 1 1 Sample Output 1 46 He can bring 46 acorns to the nest, as follows: In Exchange A , trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 } In Exchange B , trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 } In Exchange A , trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 } He cannot have 47 or more acorns, so the answer is 46 .
37,597
Pattern Search Find places where a R × C pattern is found within a H × W region. Print top-left coordinates ( i , j ) of sub-regions where the pattern found. The top-left and bottom-right coordinates of the region is (0, 0) and ( H -1, W -1) respectively. Input In the first line, two integers H and W are given. In the following H lines, i -th lines of the region are given. In the next line, two integers R and C are given. In the following R lines, i -th lines of the pattern are given. output For each sub-region found, print a coordinate i and j separated by a space character in a line. Print the coordinates in ascending order of the row numbers ( i ), or the column numbers ( j ) in case of a tie. Constraints 1 ≀ H, W ≀ 1000 1 ≀ R, C ≀ 1000 The input consists of alphabetical characters and digits Sample Input 1 4 5 00010 00101 00010 00100 3 2 10 01 10 Sample Output 1 0 3 1 2
37,598
おそろいの景品 ゞョり君ずダ゚さんは仲の良いカップルです。ゞョり君はカプセル玩具自販機(ガチャポン)の景品を集めおおり、二人で出かけたずきも、ガチャポンを芋぀けるず䜕床かやっおみるほどの熱の入りようです。ダ゚さんは楜しそうなゞョり君をそばで芋おいるだけでしたが、ゞョり君の今床の誕生日プレれントにガチャポンの景品をひず぀あげるこずにしたした。ダ゚さんはガチャポン自䜓にはあたり興味がわきたせんでしたが、できればゞョり君ずおそろいの景品が欲しいず思っおいたす。 ダ゚さんがやっおみようず思うガチャポンは、回のチャレンゞで景品がひず぀出たす。品切れのものも含め景品が䜕皮類あるのかず、それぞれの景品がいく぀残っおいるのかはわかりたす。しかし、1回のチャレンゞでどの景品が出るかはわかりたせん。そこで、景品が出る順番にかかわらず、ダ゚さんが同じ景品を必ず぀埗るために最䜎限必芁なチャレンゞの回数を出力するプログラムを䜜成しおください。 入力 入力は耇数のデヌタセットからなる。入力の終わりはれロ1぀の行で瀺される。各デヌタセットは以䞋の圢匏で䞎えられる。 N k 1 k 2 ... k N 各デヌタセットは2行であり、1行目に景品が䜕皮類あるかを衚す敎数 N (1 ≀ N ≀ 10000)が䞎えられる。続く1行に各景品がいく぀残っおいるのかを衚す敎数 k i (0 ≀ k i ≀ 10000)が䞎えられる。 デヌタセットの数は100を超えない。 出力 デヌタセットごずに、同じ景品を必ず぀埗るために最䜎限必芁なチャレンゞの回数を出力する。ただし、䞍可胜な堎合はNAず出力する。 入力䟋 2 3 2 3 0 1 1 1 1000 0 出力䟋 3 NA 2 ぀目のデヌタセットでは、運良く皮類目か皮類目の景品が連続で出お回で枈む可胜性はあるが、同じ皮類の景品を必ず぀埗るためには回チャレンゞする必芁がある。 ぀目のデヌタセットでは、぀以䞊残っおいる景品がもずもず無いので䞍可胜である。 ぀目のデヌタセットでは、景品は皮類だけなので回のチャレンゞでその景品が必ず぀埗られる。
37,599
F: Steps 問題 AORむカちゃんは株匏䌚瀟AORの芖察に蚪れた。 株匏䌚瀟AORは $N$ 階建おのビルであり、地䞋階は存圚しない。 AORむカちゃんは $1$ 階から芖察を開始し、ちょうど $M$ 回移動するこずにした。 $1$ 回の移動で $i$ 階から $i + 1$ 階、あるいは $i - 1$ 階に移動するこずができる。 ただし、 移動先が $0$ 階や $N + 1$ 階ずなるように移動するこずはできない。 AORむカちゃんが党おの階に䞀床以䞊蚪れる移動方法の通り数を $1000000007 (= 10^9 + 7)$ で割った䜙りを求めよ。 なお、 $1$ 階にはすでに蚪れたものずする。 制玄 $2 \leq N \leq 10^5$ $1 \leq M \leq 10^5$ 入力は党お敎数で䞎えらえる。 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N \ M$ 出力 移動方法の通り数を $1000000007 (= 10 ^ 9 + 7)$ で割った䜙りを出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 3 5 サンプル出力 1 3 サンプル入力 2 3 1 サンプル出力 2 0 サンプル入力 3 4883 5989 サンプル出力 3 956662807
37,600
(Please read problem A first. The maximum score you can get by solving this problem B is 1, which will have almost no effect on your ranking.) Beginner's Guide Let's first write a program to calculate the score from a pair of input and output. You can know the total score by submitting your solution, or an official program to calculate a score is often provided for local evaluation as in this contest. Nevertheless, writing a score calculator by yourself is still useful to check your understanding of the problem specification. Moreover, the source code of the score calculator can often be reused for solving the problem or debugging your solution. So it is worthwhile to write a score calculator unless it is very complicated. Problem Statement You will be given a contest schedule for D days. For each d=1,2,\ldots,D , calculate the satisfaction at the end of day d . Input Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A. D c_1 c_2 \cdots c_{26} s_{1,1} s_{1,2} \cdots s_{1,26} \vdots s_{D,1} s_{D,2} \cdots s_{D,26} t_1 t_2 \vdots t_D The constraints and generation methods for the input part are the same as those for Problem A. For each d , t_d is an integer satisfying 1\leq t_d \leq 26 , and your program is expected to work correctly for any value that meets the constraints. Output Let v_d be the satisfaction at the end of day d . Print D integers v_d to Standard Output in the following format: v_1 v_2 \vdots v_D Sample Input 1 5 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192 1 17 13 14 13 Sample Output 1 18398 35037 51140 65837 79325 Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case. Next Step We can build a solution (schedule) for this problem in the order of day 1, day 2, and so on. And for every partial solution we have built, we can calculate the goodness (satisfaction) by using the above score calculator. So we can construct the following algorithm: for each d=1,2,\ldots,D , we select the contest type that maximizes the satisfaction at the end of day d . You may have already encountered this kind of "greedy algorithms" in algorithm contests such as ABC. Greedy algorithms can guarantee the optimality for several problems, but unfortunately, it doesn't ensure optimality for this problem. However, even if it does not ensure optimality, we can still obtain a reasonable solution in many cases. Let's go back to Problem A and implement the greedy algorithm by utilizing the score calculator you just implemented! Greedy methods can be applied to a variety of problems, are easy to implement, and often run relatively fast compared to other methods. Greedy is often the most powerful method when we need to process huge inputs. We can further improve the score by changing the greedy selection criteria (evaluation function), keeping multiple candidates instead of focusing on one best partial solution (beam search), or using the output of greedy algorithms as an initial solution of other methods. For more information, please refer to the editorial that will be published after the contest.
37,601
Problem D: Dr. Nakamura's Lab. Dr.䞭村は偉倧な発明者である, 今日も垞人には発想出来ない新たな発明に取り掛かっおいる. 今発明しおいるのは新たな防犯システムずコンテナである. 防犯システムの方は䞊に乗った者を識別しお招かれざる客だった堎合, 電流を流しお気絶させるパネルである. このパネルは螏たないように䞊を通過しおも無駄で, 空䞭のものに察し攟電する. しかし, ただ完成には皋遠く, 電流は匷すぎお調節が䞍可胜で, 䞀床攟電するず充電しない限り䜿えない. たた, 人ず物䜓の区別ができず, そもそも人自䜓区別できない等欠点をあげればキリがない. コンテナの方は謎の力によっお垞時地面から浮いおおり, 運ぶのに䟿利に䜜っおある. しかし, これも完成には遠く, 䞀床抌すず䜕かにぶ぀かるたで止たらない. Dr.䞭村は䞀日の䜜業を終え, 垰宅しようずしたが未完成のパネルを蚭眮したたただずいうこずに気づいた. これを取り倖すのは防犯䞊非垞に困難に䜜っおある䞊に, これを通らなければ垰れないような䜍眮に蚭眮しおしたったのでDr.䞭村は途方に暮れた. しかし, 玠晎らしい頭脳を持぀Dr.䞭村は解決案をすぐに思い぀いた. パネルは䞀床攟電させれば通れるようになるため, コンテナを通過させればパネルの䞊を通れるようになる, そう考えた. 実際, パネルの䞊にコンテナを通過させるず, コンテナは電流によっお消滅しおしたう䞀方でパネルは攟電し, 通れるようになる. しかし, 研究宀の䞭は入り組んでおり, うたくコンテナを動かさないずパネルを通過させられない. Dr.䞭村は助手であるあなたにメヌルで助けを求めた. あなたの仕事は研究宀の情報が䞎えられた時にDr.䞭村が出口たでたどり着くためのプログラムを䜜成するこずである. 研究宀は二次元グリッドで䞎えられ, Dr.䞭村は䞀床の移動で䞊䞋巊右に隣接するセルに移動するこずが出来る.ただし, 障害物, コンテナ, 攟電しおいないパネルのセルには移動できない. Dr.䞭村は䞊䞋巊右に隣接するセルにあるコンテナを抌すこずができ, コンテナはDr.䞭村がいる方向ず逆の方向に別のコンテナか障害物にぶ぀かる, あるいは攟電されおいないパネルの䞊を通過するたで動く. たた, 䜕かにぶ぀かり止たった堎合, ぶ぀かる盎前のセルで静止する. 出口はコンテナもDr.䞭村も䟵入可胜である. コンテナは出口を通っおも消滅せず、そのたた通過する. 壁にぶ぀かっお出口をふさぐこずはある. この堎合は出口に進入できない. あなたのプログラムが芁求されおいるこずは, Dr.䞭村は最少䜕回の移動で研究宀を脱出できるかを出力するこずのみである. なぜなら, Dr.䞭村は玠晎らしい頭脳の持ち䞻なのでそれさえわかれば自分で脱出するからである. Input 入力は耇数のデヌタセットからなり, 䞀行目に研究宀の瞊の長さ H ず暪の長さ W が䞎えられる. 2行目以降では研究宀の状態が H × W 個の文字で衚わされる. 各文字が衚すものは以䞋のずおりである: ‘ # ’障害物を衚す. ‘ @ ’Dr.䞭村の初期䜍眮を衚す. ‘ w ’パネルを衚す. ‘ c ’コンテナを衚す. ‘ . ’䜕も眮かれおいない空癜のセルを衚す. ‘ E ’脱出口を衚す. 瞊ず暪の長さがずもに 0 のずき入力の終了を衚す. これに぀いお凊理をする必芁はない. 以䞋のこずを仮定しおよい. 3 ≀ H, W ≀ 10 研究宀の呚囲は障害物で囲たれおいる. パネルずコンテナの数はそれぞれ 3 を越えない. 研究宀にはただ぀の出口がある. Output 各デヌタセットに぀いお最少の移動回数を行に出力せよ. なお、Dr.䞭村が脱出できない堎合、” -1 ”ず出力せよ. Sample Input 5 5 ##### ##@## #wc.# #Ew.# ##### 5 5 ##### ##@.# #wc.# #E#.# ##### 3 6 ###### #@.wE# ###### 0 0 Output for the Sample Input 3 5 -1
37,602
日の出ず日の入り 倪陜が珟れるこずを「日の出」、隠れるこずを「日の入り」ず呌びたすが、その厳密な時刻は倪陜が地平線に察しおどのような䜍眮にある時でしょうか。 䞋の図のように、倪陜を円、地平線を盎線で衚すこずにしたす。このずき、倪陜の「日の出」「日の入り」の時刻は、倪陜を衚す円の䞊端が地平線を衚す盎線ず䞀臎する瞬間ずされおいたす。日の出の時刻を過ぎ、円の䞊端が盎線より䞊にある時間垯が昌間、円が盎線の䞋ぞ完党に隠れおいる時間垯が倜間ずなりたす。 ある時刻の地平線から倪陜の䞭心たでの高さず、倪陜の半埄を入力ずし、その時刻が「昌間」か、「日の出たたは日の入り」か、「倜間」かを出力するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 H R 入力は行からなり、ある時刻の地平線から倪陜の䞭心たでの高さを衚す敎数 H (-1000 ≀ H ≀ 1000) ず半埄を衚す敎数 R (1 ≀ R ≀ 1000) が䞎えられる。ただし、 H は倪陜の䞭心が地平線䞊にあるずきを 0 ずしお、それより䞊にあるずきは正、䞋にあるずきは負ずする。 Output 昌間のずき「1」、日の出たたは日の入りのずき「0」、倜間のずき「-1」を行に出力する。 Sample Input 1 -3 3 Sample Output 1 0 Sample Input 2 3 3 Sample Output 2 1 Sample Input 3 -4 3 Sample Output 3 -1
37,603
B: 呚期数列 - Periodic Sequence - 問題 H倧孊の教授を務めおいるPeriod博士は、䞇物に朜むずされる呚期ず呌ばれる性質を研究しおいる。䞀般的に知られおいる基本的な呚期ずしおは、数列に朜む呚期が考えられるだろう。すなわち、長さ N の数列 S = S_1, S_2, ... , S_N が以䞋の性質を満たすならば、呚期 t ( t \≀ N ) を持぀ずいう事実である。 1 \≀ i \≀ N − t に぀いお、 S_i=S_{i+t} である。 今、Period博士が着目しおいるのは、呚期を甚いおより簡易な蚘述ができる数列である。䟋えば、長さ N の数列が呚期 t ( \≀ N ) を持぀ずき、ある敎数 k を甚いお N=kt ず曞けるならば、その数列は長さ t の数列 S_1, ... , S_t が k 個連続したものである、ず蚘述できる。Period博士は数列を䟋のように蚘述できたずき、その数列は k -partであるず蚀うこずにした。 Period博士は、 k が最も倧きい k -partに興味を瀺しおいる。そこで助手であるあなたは、入力ずしお数列を受け取り、それが k -partであるずき最も倧きい k を出力するプログラムの䜜成を任されるこずずなった。Period博士の芁求に正確に応えるプログラムを䜜成しよう。 入力圢匏 N S_1 ... S_N 1行目には、数列の長さを衚す敎数 N が䞎えられる。2行目には、長さ N の数列の各芁玠を衚す敎数 S_i ( 1 \≀ i \≀ N ) が空癜区切りで䞎えられる。たた、入力は 1 \≀ N \≀ 200,000 ず 1 \≀ S_i \≀ 100,000 ( 1 \≀ i \≀ N ) を満たす。 出力圢匏 䞎えられた数列に察しお、 k -partであるずきの k の最倧倀を1行に出力せよ。 入力䟋1 6 1 2 3 1 2 3 出力䟋1 2 入力䟋2 12 1 2 1 2 1 2 1 2 1 2 1 2 出力䟋2 6 入力䟋3 6 1 2 3 4 5 6 出力䟋3 1
37,604
Recurring Decimals A decimal representation of an integer can be transformed to another integer by rearranging the order of digits. Let us make a sequence using this fact. A non-negative integer a 0 and the number of digits L are given first. Applying the following rules, we obtain a i +1 from a i . Express the integer a i in decimal notation with L digits. Leading zeros are added if necessary. For example, the decimal notation with six digits of the number 2012 is 002012. Rearranging the digits, find the largest possible integer and the smallest possible integer; In the example above, the largest possible integer is 221000 and the smallest is 000122 = 122. A new integer a i +1 is obtained by subtracting the smallest possible integer from the largest. In the example above, we obtain 220878 subtracting 122 from 221000. When you repeat this calculation, you will get a sequence of integers a 0 , a 1 , a 2 , ... . For example, starting with the integer 83268 and with the number of digits 6, you will get the following sequence of integers a 0 , a 1 , a 2 , ... . a 0 = 083268 a 1 = 886320 − 023688 = 862632 a 2 = 866322 − 223668 = 642654 a 3 = 665442 − 244566 = 420876 a 4 = 876420 − 024678 = 851742 a 5 = 875421 − 124578 = 750843 a 6 = 875430 − 034578 = 840852 a 7 = 885420 − 024588 = 860832 a 8 = 886320 − 023688 = 862632 
 Because the number of digits to express integers is fixed, you will encounter occurrences of the same integer in the sequence a 0 , a 1 , a 2 , ... eventually. Therefore you can always find a pair of i and j that satisfies the condition a i = a j ( i > j ). In the example above, the pair ( i = 8, j = 1) satisfies the condition because a 8 = a 1 = 862632. Write a program that, given an initial integer a 0 and a number of digits L , finds the smallest i that satisfies the condition a i = a j ( i > j ). Input The input consists of multiple datasets. A dataset is a line containing two integers a 0 and L separated by a space. a 0 and L represent the initial integer of the sequence and the number of digits, respectively, where 1 ≀ L ≀ 6 and 0 ≀ a 0 < 10 L . The end of the input is indicated by a line containing two zeros; it is not a dataset. Output For each dataset, find the smallest number i that satisfies the condition a i = a j ( i > j ) and print a line containing three integers, j , a i and i − j . Numbers should be separated by a space. Leading zeros should be suppressed. Output lines should not contain extra characters. You can assume that the above i is not greater than 20. Sample Input 2012 4 83268 6 1112 4 0 1 99 2 0 0 Output for the Sample Input 3 6174 1 1 862632 7 5 6174 1 0 0 1 1 0 1
37,605
A - アルデンテ 問題文 わたしは近所のスヌパヌで初めおスパゲッティずいうものを賌入したパッケヌゞの情報によるずこのスパゲッティは T 秒茹でるず最も理想的な状態に茹で䞊がるらしい ずころでわたしは電気が嫌いな人間であり家にはテレビやパ゜コンはおろか時蚈さえ無い そのためスパゲッティを茹でようにも時間を枬るこずができない 電池匏の時蚈は嫌なのでわたしは代わりに砂時蚈を買おうず考えスヌパヌの砂時蚈売り堎ぞず向かった 売り堎には N 個の砂時蚈があった i 番目の砂時蚈はちょうど x i 秒だけ枬るこずができるようであった いろいろあっお悩たしいがスパゲッティのために砂時蚈を䜕個も買うのは銬鹿らしいのでこの䞭からちょうど 1 個だけ買おうず思う しかし皆さんご存知の通り砂時蚈ずいうのは䞀床枬り始めるずそれから枬り終えるたでは途䞭の時刻ずいうのはたったく分からない そのためもし私が i 番目の砂時蚈を買っお䜿ったずするず時蚈を連続しお䜿うこずで x i 秒 2x i 秒 3x i 秒... ずいう時間を枬るこずができるが逆にそれ以倖の時間は枬るこずができない 理想的な茹で䞊がりに必芁な時間は T 秒らしいがここでは劥協しお E (< T) 秒以内の誀差を蚱そう すなわちどれか 1 ぀の砂時蚈を甚いお T−E 秒 T−E+1 秒 T−E+2 秒...  T+E−2 秒 T+E−1 秒 T+E 秒のうちのどれかの時間を蚈枬したい 私は䜕番目の砂時蚈を賌入しお䜿えばよいだろうか? 入力圢匏 入力は以䞋の圢匏で䞎えられる N T E x 1 ... x N N は砂時蚈の個数である T はスパゲッティを理想的に茹で䞊げるのに必芁な時間(秒) E は茹で䞊げに蚱容する誀差の時間(秒)である x i は i 番目の砂時蚈が 1 回で枬るこずできるの時間(秒)である 出力圢匏 䜕番目の砂時蚈を䜿えばよいか出力せよそのような砂時蚈が耇数個ある堎合それらの䞭からどれか 1 ぀を出力せよ どの砂時蚈を䜿っおも時間が枬れない堎合は -1 を出力せよ 制玄 1 ≀ N ≀ 100 1 ≀ T ≀ 1,440 0 ≀ E < T 1 ≀ x i ≀ 10^4 入力倀はすべお敎数である 入出力䟋 入力䟋 1 2 10 2 3 4 出力䟋 1 2 8秒9秒10秒11秒12秒 のうちのいずれかの時間を蚈枬できればよい 1 番目の砂時蚈で枬るこずのできる時間は 3秒6秒9秒12秒... であり 2 番目の砂時蚈で枬るこずのできる時間は 4秒8秒12秒16秒... であるのでどちらの砂時蚈を甚いおも目的の時間を蚈枬できる 出力䟋 1 では 2 を答えずしおいるが1 を答えにしおも正解である 入力䟋 2 3 10 5 16 17 18 出力䟋 2 -1 どの砂時蚈を䜿っおも目的の時間は蚈枬できない Writer: 花田裕䞀朗 Tester: 小浜翔倪郎
37,606
Problem H: Robot's Crash Prof. Jenifer A. Gibson is carrying out experiments with many robots. Since those robots are expensive, she wants to avoid their crashes during her experiments at her all effort. So she asked you, her assistant, as follows. “Suppose that we have n (2 ≀ n ≀ 100000) robots of the circular shape with the radius of r , and that they are placed on the xy -plane without overlaps. Each robot starts to move straight with a velocity of either v or - v simultaneously, say, at the time of zero. The robots keep their moving infinitely unless I stop them. I’d like to know in advance if some robots will crash each other. The robots crash when their centers get closer than the distance of 2 r . I’d also like to know the time of the first crash, if any, so I can stop the robots before the crash. Well, could you please write a program for this purpose?” Input The input consists of multiple datasets. Each dataset has the following format: n vx vy r rx 1 ry 1 u 1 rx 2 ry 2 u 2 ... rx n ry n u n n is the number of robots. ( vx , vy ) denotes the vector of the velocity v . r is the radius of the robots. ( rx i , ry i ) denotes the coordinates of the center of the i -th robot. u i denotes the moving direction of the i -th robot, where 1 indicates the i -th robot moves with the velocity ( vx , vy ), and -1 indicates (- vx , - vy ). All the coordinates range from -1200 to 1200. Both vx and vy range from -1 to 1. You may assume all the following hold for any pair of robots: they must not be placed closer than the distance of (2 r + 10 -8 ) at the initial state; they must get closer than the distance of (2 r - 10 -8 ) if they crash each other at some point of time; and they must not get closer than the distance of (2 r + 10 -8 ) if they don’t crash. The input is terminated by a line that contains a zero. This should not be processed. Output For each dataset, print the first crash time in a line, or “SAFE” if no pair of robots crashes each other. Each crash time should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10 -4 . Sample Input 2 1.0 0.0 0.5 0.0 0.0 1 2.0 0.0 -1 2 1.0 0.0 0.5 0.0 0.0 -1 2.0 0.0 1 0 Output for the Sample Input 0.500000 SAFE
37,607
Score : 100 points Problem Statement You planned a trip using trains and buses. The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket. Find the minimum total fare when the optimal choices are made for trains and buses. Constraints 1 \leq A \leq 1 000 1 \leq B \leq 1 000 1 \leq C \leq 1 000 1 \leq D \leq 1 000 All input values are integers. Input Input is given from Standard Input in the following format: A B C D Output Print the minimum total fare. Sample Input 1 600 300 220 420 Sample Output 1 520 The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket. Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen. On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen. Therefore, the minimum total fare is 300 + 220 = 520 yen. Sample Input 2 555 555 400 200 Sample Output 2 755 Sample Input 3 549 817 715 603 Sample Output 3 1152
37,608
Score : 800 points Problem Statement This is an interactive task. Snuke has a favorite positive integer, N . You can ask him the following type of question at most 64 times: "Is n your favorite integer?" Identify N . Snuke is twisted, and when asked "Is n your favorite integer?", he answers "Yes" if one of the two conditions below is satisfied, and answers "No" otherwise: Both n \leq N and str(n) \leq str(N) hold. Both n > N and str(n) > str(N) hold. Here, str(x) is the decimal representation of x (without leading zeros) as a string. For example, str(123) = 123 and str(2000) = 2000 . Strings are compared lexicographically. For example, 11111 < 123 and 123456789 < 9 . Constraints 1 \leq N \leq 10^{9} Input and Output Write your question to Standard Output in the following format: ? n Here, n must be an integer between 1 and 10^{18} (inclusive). Then, the response to the question shall be given from Standard Input in the following format: ans Here, ans is either Y or N . Y represents "Yes"; N represents "No". Finally, write your answer in the following format: ! n Here, n=N must hold. Judging After each output, you must flush Standard Output. Otherwise you may get TLE . After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined. When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give WA ). Sample Below is a sample communication for the case N=123 : Input Output ? 1 Y ? 32 N ? 1010 N ? 999 Y ! 123 Since 1 \leq 123 and str(1) \leq str(123) , the first response is "Yes". Since 32 \leq 123 but str(32) > str(123) , the second response is "No". Since 1010 > 123 but str(1010) \leq str(123) , the third response is "No". Since 999 \geq 123 and str(999) > str(123) , the fourth response is "Yes". The program successfully identifies N=123 in four questions, and thus passes the case.
37,609
Score : 300 points Problem Statement You are given a sequence of length N : A_1, A_2, ..., A_N . For each integer i between 1 and N (inclusive), answer the following question: Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints 2 \leq N \leq 200000 1 \leq A_i \leq 200000 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 : A_N Output Print N lines. The i -th line ( 1 \leq i \leq N ) should contain the maximum value among the N-1 elements other than A_i in the sequence. Sample Input 1 3 1 4 3 Sample Output 1 4 3 4 The maximum value among the two elements other than A_1 , that is, A_2 = 4 and A_3 = 3 , is 4 . The maximum value among the two elements other than A_2 , that is, A_1 = 1 and A_3 = 3 , is 3 . The maximum value among the two elements other than A_3 , that is, A_1 = 1 and A_2 = 4 , is 4 . Sample Input 2 2 5 5 Sample Output 2 5 5
37,610
Score : 700 points Problem Statement We have a directed weighted graph with N vertices. Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i . In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N , and its weight is {\rm min}(A_x,B_y) . We will consider a directed cycle in this graph that visits every vertex exactly once. Find the minimum total weight of the edges in such a cycle. Constraints 2 \leq N \leq 10^5 1 \leq A_i \leq 10^9 1 \leq B_i \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 B_1 A_2 B_2 : A_N B_N Output Print the minimum total weight of the edges in such a cycle. Sample Input 1 3 1 5 4 2 6 3 Sample Output 1 7 Consider the cycle 1→3→2→1 . The weights of those edges are {\rm min}(A_1,B_3)=1 , {\rm min}(A_3,B_2)=2 and {\rm min}(A_2,B_1)=4 , for a total of 7 . As there is no cycle with a total weight of less than 7 , the answer is 7 . Sample Input 2 4 1 5 2 6 3 7 4 8 Sample Output 2 10 Sample Input 3 6 19 92 64 64 78 48 57 33 73 6 95 73 Sample Output 3 227
37,611
Score : 300 points Problem Statement You are given N real values A_1, A_2, \ldots, A_N . Compute the number of pairs of indices (i, j) such that i < j and the product A_i \cdot A_j is integer. Constraints 2 \leq N \leq 200\,000 0 < A_i < 10^4 A_i is given with at most 9 digits after the decimal. Input Input is given from Standard Input in the following format. N A_1 A_2 \vdots A_N Output Print the number of pairs with integer product A_i \cdot A_j (and i < j ). Sample Input 1 5 7.5 2.4 17.000000001 17 16.000000000 Sample Output 1 3 There are 3 pairs with integer product: 7.5 \cdot 2.4 = 18 7.5 \cdot 16 = 120 17 \cdot 16 = 272 Sample Input 2 11 0.9 1 1 1.25 2.30000 5 70 0.000000001 9999.999999999 0.999999999 1.000000001 Sample Output 2 8
37,612
Problem Statement In A.D. 2101, war was beginning. The enemy has taken over all of our bases. To recapture the bases, we decided to set up a headquarters. We need to define the location of the headquarters so that all bases are not so far away from the headquarters. Therefore, we decided to choose the location to minimize the sum of the distances from the headquarters to the furthest $K$ bases. The bases are on the 2-D plane, and we can set up the headquarters in any place on this plane even if it is not on a grid point. Your task is to determine the optimal headquarters location from the given base positions. Input The input consists of a single test case in the format below. $N$ $K$ $x_{1}$ $y_{1}$ $\vdots$ $x_{N}$ $y_{N}$ The first line contains two integers $N$ and $K$. The integer $N$ is the number of the bases ($1 \le N \le 200$). The integer $K$ gives how many bases are considered for calculation ($1 \le K \le N$). Each of the following $N$ lines gives the x and y coordinates of each base. All of the absolute values of given coordinates are less than or equal to $1000$, i.e., $-1000 \le x_{i}, y_{i} \le 1000$ is satisfied. Output Output the minimum sum of the distances from the headquarters to the furthest $K$ bases. The output can contain an absolute or a relative error no more than $10^{-3}$. Examples Input Output 3 1 0 1 1 0 1 1 0.70711 6 3 1 1 2 1 3 2 5 3 8 5 13 8 17.50426 9 3 573 -50 -256 158 -751 14 314 207 293 567 59 -340 -243 -22 -268 432 -91 -192 1841.20904
37,613
Transformation Write a program which performs a sequence of commands to a given string $str$. The command is one of: print a b : print from the a -th character to the b -th character of $str$ reverse a b : reverse from the a -th character to the b -th character of $str$ replace a b p : replace from the a -th character to the b -th character of $str$ with p Note that the indices of $str$ start with 0. Input In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format. Output For each print command, print a string in a line. Constraints $1 \leq $ length of $str \leq 1000$ $1 \leq q \leq 100$ $0 \leq a \leq b < $ length of $str$ for replace command, $b - a + 1 = $ length of $p$ Sample Input 1 abcde 3 replace 1 3 xyz reverse 0 2 print 1 4 Sample Output 1 xaze Sample Input 2 xyz 3 print 0 2 replace 0 2 abc print 0 2 Sample Output 2 xyz abc
37,614
Problem A: Special Chat Problem 人気動画投皿サむト「ZouTube」は今、空前の「バヌチャルZouTuber」ブヌムの真っ只䞭にある。その䞭でも、最近特に泚目を集めおいるのは、埌茩系バヌチャルZouTuber「アむヅマリム通称アヅリム」だ。 アヅリムの倧ファンであるあなたは、本日のアヅリムのラむブ配信で、圌女に「スペシャルチャット」を送る぀もりだ。 「スペシャルチャット」ずは、ZouTubeの提䟛する「芖聎者が配信者にポむントをプレれントする機胜」 だ。芖聎者はスペシャルチャット$1$回に぀き、$500$, $1000$, $5000$, $10000$のいずれかだけポむントを消費しお、消費した量ず同じ量だけポむントを配信者にプレれントできる。 今あなたが保持しおいるポむントの総量が䞎えられたずき、そのポむントを消費しお、あなたがアヅリムにプレれントできるポむントの総量の最倧倀を求めよ。保持しおいるポむントの量が消費するポむントの量を䞋回らない限り、スペシャルチャットは䜕床でも行うこずができる。 Input 入力は以䞋の圢匏で䞎えられる。 $P$ 今あなたが保持しおいるポむントの総量を衚す敎数$P$が$1$行に䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \le P \le 10^5$ Output アヅリムにプレれントできるポむントの総量の最倧倀を$1$行に出力する。 Sample Input 1 5700 Sample Output 1 5500 Sample Input 2 1333 Sample Output 2 1000 Sample Input 3 100000 Sample Output 3 100000
37,615
Score : 100 points Problem Statement You are given an array a_0, a_1, ..., a_{N-1} of length N . Process Q queries of the following types. The type of i -th query is represented by T_i . T_i=1 : You are given two integers X_i,V_i . Replace the value of A_{X_i} with V_i . T_i=2 : You are given two integers L_i,R_i . Calculate the maximum value among A_{L_i},A_{L_i+1},\cdots,A_{R_i} . T_i=3 : You are given two integers X_i,V_i . Calculate the minimum j such that X_i \leq j \leq N, V_i \leq A_j . If there is no such j , answer j=N+1 instead. Constraints 1 \leq N \leq 2 \times 10^5 0 \leq A_i \leq 10^9 1 \leq Q \leq 2 \times 10^5 1 \leq T_i \leq 3 1 \leq X_i \leq N , 0 \leq V_i \leq 10^9 ( T_i=1,3 ) 1 \leq L_i \leq R_i \leq N ( T_i=2 ) All values in Input are integer. Input Input is given from Standard Input in the following format: N Q A_1 A_2 \cdots A_N First query Second query \vdots Q -th query Each query is given in the following format: If T_i=1,3 , T_i X_i V_i If T_i=2 , T_i L_i R_i Output For each query with T_i=2, 3 , print the answer. Sample Input 1 5 5 1 2 3 2 1 2 1 5 3 2 3 1 3 1 2 2 4 3 1 3 Sample Output 1 3 3 2 6 First query: Print 3 , which is the maximum of (A_1,A_2,A_3,A_4,A_5)=(1,2,3,2,1) . Second query: Since 3>A_2 , j=2 does not satisfy the conditionSince 3 \leq A_3 , print j=3 . Third query: Replace the value of A_3 with 1 . It becomes A=(1,2,1,2,1) . Fourth query: Print 2 , which is the maximum of (A_2,A_3,A_4)=(2,1,2) . Fifth query: Since there is no j that satisfies the condition, print j=N+1=6 .
37,616
Score : 900 points Problem Statement There is a tree with N vertices numbered 1 through N . The i -th of the N-1 edges connects vertices a_i and b_i . Initially, each vertex is uncolored. Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi: Select a vertex that is not painted yet. If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki. Then, after all the vertices are colored, the following procedure takes place: Repaint every white vertex that is adjacent to a black vertex, in black. Note that all such white vertices are repainted simultaneously, not one at a time. If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally. Constraints 2 ≀ N ≀ 10^5 1 ≀ a_i,b_i ≀ N a_i ≠ b_i The input graph is a tree. Input Input is given from Standard Input in the following format: N a_1 b_1 : a_{N-1} b_{N-1} Output Print First if Takahashi wins; print Second if Aoki wins. Sample Input 1 3 1 2 2 3 Sample Output 1 First Below is a possible progress of the game: First, Takahashi paint vertex 2 white. Then, Aoki paint vertex 1 black. Lastly, Takahashi paint vertex 3 white. In this case, the colors of vertices 1 , 2 and 3 after the final procedure are black, black and white, resulting in Takahashi's victory. Sample Input 2 4 1 2 2 3 2 4 Sample Output 2 First Sample Input 3 6 1 2 2 3 3 4 2 5 5 6 Sample Output 3 Second
37,617
Problem G: Nim Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove one to four stones from the heap. You play first and the one who removed the last stone loses. In this game, you have a winning strategy. To see this, you first remove four stones and leave 96 stones. No matter how I play, I will end up with leaving 92-95 stones. Then you will in turn leave 91 stones for me (verify this is always possible). This way, you can always leave 5 k + 1 stones for me and finally I get the last stone, sigh. If we initially had 101 stones, on the other hand, I have a winning strategy and you are doomed to lose. Let's generalize the game a little bit. First, let's make it a team game. Each team has n players and the 2 n players are seated around the table, with each player having opponents at both sides. Turns round the table so the two teams play alternately. Second, let's vary the maximum number ofstones each player can take. That is, each player has his/her own maximum number ofstones he/she can take at each turn (The minimum is always one). So the game is asymmetric and may even be unfair. In general, when played between two teams of experts, the outcome of a game is completely determined by the initial number ofstones and the minimum number of stones each player can take at each turn. In other words, either team has a winning strategy. You are the head-coach of a team. In each game, the umpire shows both teams the initial number of stones and the maximum number of stones each player can take at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether your team has a winning strategy. Incidentally, there is a rumor that Captain Future and her officers of Hakodate-maru love this game, and they are killing their time playing it during their missions. You wonder where the stones are?. Well, they do not have stones but do have plenty of balls in the fuel containers!. Input The input is a sequence of lines, followed by the last line containing a zero. Each line except the last is a sequence of integers and has the following format. n S M 1 M 2 ... M 2 n where n is the number of players in a team, S the initial number of stones, and M i the maximum number of stones i th player can take. 1st, 3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the opponents. Numbers are separated by a single space character. You may assume 1 ≀ n ≀ 10, 1 ≀ M i ≀ 16, and 1 ≀ S ≀ 2 13 . Output The out put should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise. Sample Input 1 101 4 4 1 100 4 4 3 97 8 7 6 5 4 3 0 Output for the Sample Input 0 1 1
37,618
Score : 600 points Problem Statement We have a tree with N vertices. The i -th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u , and Aoki is standing at Vertex v . Now, they will play a game of tag as follows: 1 . If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex. 2 . If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex. 3 . Go back to step 1 . Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible. Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy. It can be proved that the game is bound to end. Constraints 2 \leq N \leq 10^5 1 \leq u,v \leq N u \neq v 1 \leq A_i,B_i \leq N The given graph is a tree. Input Input is given from Standard Input in the following format: N u v A_1 B_1 : A_{N-1} B_{N-1} Output Print the number of moves Aoki will perform before the end of the game. Sample Input 1 5 4 1 1 2 2 3 3 4 3 5 Sample Output 1 2 If both players play optimally, the game will progress as follows: Takahashi moves to Vertex 3 . Aoki moves to Vertex 2 . Takahashi moves to Vertex 5 . Aoki moves to Vertex 3 . Takahashi moves to Vertex 3 . Here, Aoki performs two moves. Note that, in each move, it is prohibited to stay at the current vertex. Sample Input 2 5 4 5 1 2 1 3 1 4 1 5 Sample Output 2 1 Sample Input 3 2 1 2 1 2 Sample Output 3 0 Sample Input 4 9 6 1 1 2 2 3 3 4 4 5 5 6 4 7 7 8 8 9 Sample Output 4 5
37,619
Score : 900 points Problem Statement Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1 , Doughnut 2 , ..., Doughnut N . In order to make one Doughnut i (1 ≀ i ≀ N) , she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts. The recipes of these doughnuts are developed by repeated modifications from the recipe of Doughnut 1 . Specifically, the recipe of Doughnut i (2 ≀ i ≀ N) is a direct modification of the recipe of Doughnut p_i (1 ≀ p_i < i) . Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition: Let c_i be the number of Doughnut i (1 ≀ i ≀ N) that she makes. For each integer i such that 2 ≀ i ≀ N , c_{p_i} ≀ c_i ≀ c_{p_i} + D must hold. Here, D is a predetermined value. At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Constraints 2 ≀ N ≀ 50 1 ≀ X ≀ 10^9 0 ≀ D ≀ 10^9 1 ≀ m_i ≀ 10^9 (1 ≀ i ≀ N) 1 ≀ p_i < i (2 ≀ i ≀ N) All values in input are integers. Input Input is given from Standard Input in the following format: N X D m_1 m_2 p_2 : m_N p_N Output Print the maximum number of doughnuts that can be made under the condition. Sample Input 1 3 100 1 15 10 1 20 1 Sample Output 1 7 She has 100 grams of Moto, can make three kinds of doughnuts, and the conditions that must hold are c_1 ≀ c_2 ≀ c_1 + 1 and c_1 ≀ c_3 ≀ c_1 + 1 . It is optimal to make two Doughnuts 1 , three Doughnuts 2 and two Doughnuts 3 . Sample Input 2 3 100 10 15 10 1 20 1 Sample Output 2 10 The amount of Moto and the recipes of the doughnuts are not changed from Sample Input 1, but the last conditions are relaxed. In this case, it is optimal to make just ten Doughnuts 2 . As seen here, she does not necessarily need to make all kinds of doughnuts. Sample Input 3 5 1000000000 1000000 123 159 1 111 1 135 3 147 3 Sample Output 3 7496296
37,620
RPG Maker You are planning to create a map of an RPG. This map is represented by a grid whose size is $H \times W$. Each cell in this grid is either ' @ ', ' * ', ' # ', or ' . '. The meanings of the symbols are as follows. ' @ ': The start cell. The story should start from this cell. ' * ': A city cell. The story goes through or ends with this cell. ' # ': A road cell. ' . ': An empty cell. You have already located the start cell and all city cells under some constraints described in the input section, but no road cells have been located yet. Then, you should decide which cells to set as road cells. Here, you want a "journey" exists on this map. Because you want to remove the branch of the story, the journey has to be unforked. More formally, the journey is a sequence of cells and must satisfy the following conditions: The journey must contain as many city cells as possible. The journey must consist of distinct non-empty cells in this map. The journey must begin with the start cell. The journey must end with one of the city cells. The journey must contain all road cells. That is, road cells not included in the journey must not exist. The journey must be unforked. In more detail, all road cells and city cells except for a cell at the end of the journey must share edges with the other two cells both of which are also contained in the journey. Then, each of the start cell and a cell at the end of the journey must share an edge with another cell contained in the journey. You do not have to consider the order of the cities to visit during the journey. Initially, the map contains no road cells. You can change any empty cells to road cells to make a journey satisfying the conditions above. Your task is to print a map which maximizes the number of cities in the journey. Input The input consists of a single test case of the following form. $H$ $W$ $S_1$ $S_2$ : $S_H$ The first line consists of two integers $N$ and $W$. $H$ and $W$ are guaranteed to satisfy $H = 4n - 1$ and $W = 4m -1$ for some positive integers $n$ and $m$ ($1 \leq n, m \leq 10$). The following $H$ lines represent a map without road cells. The ($i+1$)-th line consists of a string $S_i$ of length $W$. The $j$-th character of $S_i$ is either ' * ', ' @ ' or ' . ' if both $i$ and $j$ are odd, otherwise ' . '. The number of occurrences of ' @ ' in the grid is exactly one. It is guaranteed that there are one or more city cells on the grid. Output Print a map indicating a journey. If several maps satisfy the condition, you can print any of them. Sample Input 1 11 7 ....... ....... *.....* ....... ..@.... ....... *...... ....... ....*.. ....... ....... Output for Sample Input 1 ....... ....... *#####* ......# ..@...# ..#.### *##.#.. #...#.. ####*.. ....... ....... Sample Input 2 7 11 ........*.. ........... ........... ........... ....*...*.. ........... ..*.@...*.. Output for Sample Input 2 ........*.. ........#.. ........#.. ........#.. ..##*##.*.. ..#...#.#.. ..*#@.##*..
37,621
Problem E: School Excursion 春、それは修孊旅行の時期である。 䌚接倧孊附属小孊校䌚接倧小でも、来幎床の修孊旅行の蚈画が建おられおいた。 修孊旅行は電車で移動するこずが䌝統になっおいる。 䌚接若束垂内では電車を利甚する機䌚が少ないためだ。 しかし、孊校に届いたある苊情により教員たちは頭を悩たせおいた。 それは、「ある駅で倧人数で乗り換えを行っおいたので、その駅を利甚する他の乗客に迷惑であった」ずいう内容だった。 ずくに、電車を降りたあずホヌムからの通路で倧混雑しおしたったずいう。 それは近幎、䌚接倧小の生埒数が急䞊昇しおいるからであった。 その原因は日本においお競技プログラミングが倧流行し、 小孊校から競技プログラマヌを逊成するための蚓緎を行っおいる䌚接倧小に生埒が集たったからである。 䌚接倧小競技プログラミング郚長であるあなたは、修孊旅行を楜しみにしおいる。 修孊旅行を成功させたいあなたは教員たちに提案をした。 「クラスごずに行動しお、同じ駅に同時に到着しないようにしたしょう 競技プログラミングの知識を応甚すれば簡単です」 耇数のクラスが同時に同じ駅に到着するこずがないように移動するずき同じ駅に耇数のクラスが滞圚するこずは可胜、 出発する駅から目的地たで到達できる最倧のクラス数を求め、そのずきの最小の運賃を求めるプログラムを぀くりなさい。 ただし、ある駅で同時に到着する電車ず発車する電車が存圚する堎合、乗り継ぎが可胜である。぀たり、乗り継ぎに芁する時間は無芖しお良い。 Input 入力は耇数のデヌタセットから構成される。 デヌタセットの数は20個以䞋である。 各デヌタセットの圢匏は次に瀺す通りである。 n m 1 x 1,1 y 1,1 c 1,1 x 1,2 y 1,2 c 1,2 ... x 1,m1 y 1,m1 c 1,m1 m 2 x 2,1 y 2,1 c 2,1 x 2,2 y 2,2 c 2,2 ... x 2,m2 y 2,m2 c 2,m2 ... m n-1 x n-1,1 y n-1,1 c n-1,1 x n-1,2 y n-1,2 c n-1,2 ... x n-1,m2 y n-1,m2 c n-1,m2 g n( 2 ≀ n ≀ 100)は駅の数を衚す。 駅の数は出発する駅ず目的地の駅、乗り換えする駅を含む。 駅は出発する駅を1番目、最初に乗り換える駅を2番目の駅ず数え、目的地をn番目の駅ずする。 m i はi番目の駅からi+1番目の駅ぞの電車の数を衚す。 m i (1 ≀ m ≀ 20)個の電車の情報が続く。 x i,j y i,j c i,j はi番目の駅からi+1番目の駅に向かう電車の出発時刻(x i,j )到着時刻(y i,j )運賃(c i,j )を衚す。 (0 ≀ x i,j , y i,j ≀ 200000 か぀ x i,j ≀ y i,j ) (1 ≀ c i,j ≀ 1000) その埌g(1≀ g ≀ 10)が続く。 gは修孊旅行に参加するクラス数を衚す。 䞎えられる数n,m,x,y,c,g)はすべお敎数である。 入力の終了はひず぀の0を含む行で瀺される。 Output 䞀行に到着可胜なクラス数ずそのずきの最小運賃をこの順に出力せよ。 もし、ただ䞀぀のクラスも到着できない堎合は0 0を出力せよ。 Sample Input 2 4 1 2 10 2 2 5 1 3 20 2 3 10 10 3 2 1 2 5 0 3 4 3 10 11 100 2 12 2 12 12 3 10 0 Sample Output 2 15 2 111
37,622
Pilling Blocks We make a tower by stacking up blocks. The tower consists of several stages and each stage is constructed by connecting blocks horizontally. Each block is of the same weight and is tough enough to withstand the weight equivalent to up to $K$ blocks without crushing. We have to build the tower abiding by the following conditions: Every stage of the tower has one or more blocks on it. Each block is loaded with weight that falls within the withstanding range of the block. The weight loaded on a block in a stage is evaluated by: total weight of all blocks above the stage divided by the number of blocks within the stage. Given the number of blocks and the strength, make a program to evaluate the maximum height (i.e., stages) of the tower than can be constructed. Input The input is given in the following format. $N$ $K$ The input line provides the number of blocks available $N$ ($1 \leq N \leq 10^5$) and the strength of the block $K$ ($1 \leq K \leq 10^5$). Output Output the maximum possible number of stages. Sample Input 1 4 2 Sample Output 1 3 Sample Input 2 5 2 Sample Output 2 4
37,623
迷子の双子 たかゆき君ずかずゆき君は仲良しの双子ですが、行動が真逆です。 䟋えば、たかゆき君が西に行けば、かずゆき君は東ぞ行き、かずゆき君が北ぞ行けば、たかゆき君は南ぞ行きたす。 珟圚 2 人はデパヌトに来おおり、別々の堎所にいたす。真逆に移動しおしたう 2 人ができるだけ早く出䌚うためにはどうしたらよいでしょうか? デパヌトは暪 W 個 × 瞊 H 個のマスで構成されるグリッドで衚され、2 人は単䜍時間に東西南北に 1 マス分移動するこずができたす。ただし、グリッドの範囲倖や障害物のあるマスに移動するこずはできたせん。 図のように、グリッドのマスの䜍眮は座暙 ( x , y ) で衚されたす。 グリッドの情報ず 2 人の初期䜍眮を入力ずし、2 人が出䌚うたでの最短の時間を出力するプログラムを䜜成しおください。出䌚うこずができない堎合や、出䌚うのに 100 以䞊の時間を芁する堎合は、NA ず出力しおください。グリッドの情報は、 H 行 W 列の数字、2 人の䜍眮情報は座暙によっお䞎えられたす。 移動埌にたかゆき君かかずゆき君のうち、どちらか䞀方が障害物やグリッドの範囲倖に䜍眮しおしたうずきには、移動ができないので、障害物やグリッドの範囲倖に䜍眮する方は元の堎所に戻りたすが、そうでない方は元の堎所に戻るこずなく動くこずができたす。 なお、2 人が出䌚うずは、移動埌に 2 人が同じマスに止たるこずを蚀いたす。2 人がすれ違っおも、出䌚ったこずにはなりたせん。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 W H tx ty kx ky d 11 d 21 ... d W1 d 12 d 22 ... d W2 : d 1H d 2H ... d WH 1 行目にデパヌトの倧きさ W , H (1 ≀ W, H ≀ 50) が䞎えられたす。2 行目にたかゆき君の初期䜍眮 tx, ty 、3 行目にかずゆき君の初期䜍眮 kx, ky が䞎えられたす。 続く H 行にデパヌトの情報が䞎えられたす。 d i,j はマス ( i, j ) の皮類を衚し、0 のずき移動可胜なマス、1 のずき障害物があるマスを衚したす。 デヌタセットの数は 100 を超えたせん。 Output 入力デヌタセットごずに、最短の時間を行に出力したす。 Sample Input 6 6 2 4 6 2 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 3 3 1 1 3 3 0 0 0 0 1 0 0 0 0 0 0 Output for the Sample Input 3 NA
37,624
Courage Test P「今日は肝詊し䌁画の番組の収録だ。」 O型智絵里「き、きもだめし  」 四村かな子「智絵里ちゃん倧䞈倫」 月野茜「さすがPさん䞀行で状況が明晰刀明にわかる玠晎らしい解説ですさあ火の玉に向かっお走りたしょう」 P「ルヌルは簡単、みんなで協力しおこの゚リアのすべおの神瀟にお参りしおくるだけだ。」 茜「オヌ゜ドックスですね早速神瀟に向かっお走りたしょう」 かな子「でもここ、日本有数の神瀟密床で行かなくちゃいけない神瀟がたくさんあるんじゃなかったっけ  」 かな子「でも茜ちゃんがいれば心匷いよ」 P「ちなみにスタヌトは党員違う神瀟からスタヌト、合流も犁止だ。それじゃあ智絵里ずかな子は初期䜍眮に぀いおくれ」 智絵里「うぅ  」 かな子「お、お菓子を持っおいけば倧䞈倫」 智絵里「頑匵る  」 茜「プロデュヌサヌ私はどうすれば」 P「お前は賑やかしだ」 茜「えっ」 P「座っおろ」 茜「」 Problem それぞれ1~ n の番号が付けられた n 個の神瀟ず n -1本の道がある。各道は神瀟 a i ず神瀟 b i を繋ぎ、双方向に移動するこずができる。それぞれの神瀟は、任意の神瀟から1本以䞊の道を経由しお到達するこずができる。たた、任意の2぀の神瀟の間の(迂回しない)経路は䞀意に定たる。 O型智絵里ちゃんず四村かな子ちゃんがそれぞれ神瀟 u ず神瀟 v にいる。 今、2人を任意に移動させるこずを考える。移動ずは、ある神瀟から1本の道を経由しお別の神瀟に進むこずである。 その際、以䞋のルヌルを満たしたい。 2人合わせお党おの神瀟を蚪問する。 2人の移動数を同じにする。 1぀の神瀟, 道は1人しか通れず、さらに1床しか通るこずができない。 神瀟 u , v は既に蚪れおいるものずし、通るこずはできない。 これらのルヌルを満たすこずができるかどうかを刀定し、できる堎合は"Yes"を、できない堎合は"No"を出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 n u v a 1 b 1 a 2 b 2 ... a n−1 b n−1 1行目に3぀の敎数 n , u , v が空癜区切りで䞎えられる。 2行目から n 行目たでに、2぀の敎数 a i , b i が空癜区切りで䞎えられる。 Constraints 2 ≀ n ≀ 10 5 1 ≀ a i , b i , u , v ≀ n u ≠ v a i < b i ( a i , b i ) ≠ ( a j , b j ) ( i ≠ j ) Output ルヌルを満たすこずができる堎合は"Yes"を、できない堎合は"No"を1行に出力する。 Sample Input 1 4 1 4 1 2 2 3 3 4 Sample Output 1 Yes Sample Input 2 8 3 6 1 2 2 4 3 4 4 5 5 6 6 7 7 8 Sample Output 2 No Sample Input 3 4 2 3 1 2 2 3 3 4 Sample Output 3 Yes Sample Input 4 6 1 5 1 2 1 4 2 3 4 5 3 6 Sample Output 4 No
37,625
Problem H: Testing Sorting Networks A N sorting network is a circuit that accepts N numbers as its input, outputs them sorted. Mr. Smith is an engineer of a company that sells various sizes of the circuit. One day, the company got an order of N sorting networks. Unfortunately, they didn't have the circuit for N numbers at the time. The clerk once declined the order for being out of stock, but the client was so urgent that he offered much money if he could get the circuit in a week. The deal escalated to a manager, and she asked Mr. Smith for a solution to produce the N sorting networks by the deadline. He came up with an idea to combine several N/2 sorting networks, because he noticed that they have many stocks of the circuits for N/2 numbers. He designed a new circuit using the N/2 sorting networks, but he was not sure if it would really work as an N sorting network. So, he asked a colleague, you, to check if it was actually an N sorting network. The circuit he designed consists of multiple stages. Each stage of the circuit consists of two N/2 sorting networks, which makes each stage accept a sequence of N numbers as inputs and output a sequence of N numbers. From the 1st to the N/2 -th input numbers of a stage goes to one of those N/2 sorting networks, and from the (N/2+1) -th to the N -th input numbers goes to the other. Similarly, the first half of the output numbers of a stage is the output of the former sorting network and the second half is the output of the latter, both of which are sorted in ascending order. Each output of a stage is connected to exactly one input of the next stage, and no two inputs are connected to the same output line. The input of the last stage is the input of the whole circuit and the output of the first stage is the output of the whole circuit. Input The input begins with a line containing a positive even integer N (4 <= N <= 100) and a positive integer D (1 <= D <= 10). N indicates the number of the input and output of the circuit and D indicates the number of the stages of the circuit. The i -th line of the following D-1 lines contains N integers w i 1 , w i 2 ,..., w i N (1 <= w i j <= N ), which describes the wiring between the i -th and (i+1) -th stages. w i j indicates the j -th input of the i -th stage and the w i j -th output of the (i+1) -th stage are wired. You can assume w i 1 , w i 2 ,..., w i N are unique for each i . Output Print a line containing "Yes" if the circuit works as an N sorting network. Print "No" otherwise. Sample Input 1 4 4 1 3 4 2 1 4 2 3 1 3 2 4 Output for the Sample Input 1 Yes Sample Input 2 4 3 3 1 2 4 1 3 2 4 Output for the Sample Input 2 No
37,626
問題文 圱の魔女 ELSAMARIA はすべおのものに察しお祈りを続ける魔女である圱に捕たったあらゆる呜はこの魔女の䞭に匕きずり蟌たれおしたう 圱は觊手のように動き 矎暹さやか を匕きずり蟌もうず狙っおいる 矎暹さやか はこの魔女に捕たるこずなく攻撃するために魔女にずっお予枬の難しいでたらめな動きをするこずを考えた具䜓的にはたず頭の䞭で 1 から N たでの敎数を䞀様な確率で K 個䜜りその合蚈倀の距離だけ今の䜍眮から魔女の方向ぞゞャンプするずいうこずを繰り返すのであるそしお魔女の䜍眮に到達した時に攻撃するのである さやか ず魔女はある䞀盎線䞊にいるずし最初魔女は座暙 0  さやか は座暙 S の䜍眮にいるものずするたた魔女は動かないものずする䜕床ゞャンプした埌に魔女に最初の攻撃を䞎えるこずができるだろうか期埅倀を求めよ 入力圢匏 入力は以䞋の圢匏で䞎えられる S\ N\ K S は最初に さやか がいる座暙である N は さやか が頭の䞭で䜜る敎数の範囲の䞊限 K は さやか が 1 回のゞャンプに甚いる敎数の個数である 出力圢匏 さやか が魔女の䜍眮に到達するために必芁なゞャンプの回数の期埅倀を 1 行に出力せよもし䜕床ゞャンプしおも魔女の䜍眮ぞ到達できないもしくは期埅倀が有限倀に収たらない堎合は -1 ず出力せよ 小数点以䞋䜕桁でも出力しお構わないが盞察誀差あるいは絶察誀差が 10 -6 未満になっおいなければならない 制玄 -10 9 ≀ S ≀ 10 9 1 ≀ N ≀ 10 1 ≀ K ≀ 10 入力倀は党お敎数である 入出力䟋 入力䟋 1 6 6 1 出力䟋1 6.00000000 入力䟋 2 -100 7 5 出力䟋 2 43.60343043 入力䟋 3 756434182 9 10 出力䟋 3 15128783.80867078 Problem Setter: Flat35
37,627
X-Ray Screening System これは 20XX 幎の話である再生発電網による安定した゚ネルギヌ䟛絊および液化合成燃料の発明によっお航空旅客数は鰻登りに増加したしかし航空機によるテロリズムの脅嚁は䟝然ずしお存圚するため高速でか぀高い信頌性をも぀自動手荷物怜査システムの重芁性が高たっおいる怜査官が党郚の荷物を調べるこずは実際問題ずしお困難であるこずから自動怜査においお疑わしいず刀断された手荷物だけを怜査官で改めお調べるずいう仕組みを敎えたいわけである 航空機業界からの芁請を受け囜際客宀保護䌚瀟International Cabin Protection Companyでは新たな自動怜査システムを開発するため最近の旅客の手荷物に぀いお調査した調査の結果最近の旅客の手荷物には次のような傟向があるこずが刀明した 手荷物は 1 蟺だけが短い盎方䜓のような圢状をしおいる 䞀般の乗客が手荷物に詰めお航空機に持ち蟌む品物ずしおはノヌトパ゜コン音楜プレむダヌ携垯ゲヌム機トランプなどがありこれらはいずれも長方圢である 個々の品物はその長方圢の蟺が手荷物の蟺ず平行になるように詰めこたれる 䞀方テロリズムに甚いられるような歊噚は長方圢ずは非垞に異なる圢状をも぀ 䞊蚘の調査結果をふたえお以䞋のような手荷物怜査のためのモデルを考案したそれぞれの手荷物は X 線に察しお透明である盎方䜓の容噚だずみなすその䞭には X 線に察しお䞍透明である耇数の品物が入っおいるここで盎方䜓の 3 蟺を x 軞y 軞z 軞ずする座暙系を考えx 軞ず平行な方向に X 線を照射しおy-z 平面に投圱された画像を撮圱する撮圱された画像は適圓な倧きさの栌子に分割され画像解析によっおそれぞれの栌子領域に映っおいる品物の材質が掚定されるこの䌚瀟には非垞に高床の解析技術があり材質の詳现な違いすらも解析するこずが可胜であるこずから品物の材質は互いに異なるず考えるこずができるなお耇数の品物が x 軞方向に関しお重なっおいるずきはそれぞれの栌子領域に぀いお最も手前にあるすなわち x 座暙が最小である品物の材質が埗られるたた2 ぀以䞊の品物の x 座暙が等しいこずはないず仮定する あなたの仕事は画像解析の結果が䞎えられたずきに長方圢ではない歊噚であるかもしれない品物が入っおいるこずが断蚀できるかあるいはその手荷物には長方圢の品物以倖のものは入っおいないず掚枬されるかを刀定するプログラムを䜜成するこずである Input 入力の先頭行には単䞀の正の敎数が含たれこれはデヌタセットの個数を衚すそれぞれのデヌタセットは次の圢匏で䞎えられる H W 解析結果その1 解析結果その2 ... 解析結果その H H は画像の瞊の倧きさ W は暪の倧きさを衚す敎数である (1 <= h , w <= 50)解析結果の各行は W 文字で構成されおおり i 番目の文字は行の巊から i 番目にある栌子領域における解析結果を衚す物質が怜出された栌子領域に぀いおはその材質が英倧文字A〜Zによっお衚されるこのずき同じ材質であれば同じ文字がたた異なる材質であれば異なる文字が甚いられる物質が怜出されなかった栌子領域に぀いおはドット.で衚される すべおのデヌタセットに぀いお材質の皮類は 7 皮類を超えないこずが保蚌されおいる Output それぞれのデヌタセットに察しお長方圢以倖の品物が確実に入っおいるずきは「SUSPICIOUS」をそうでないずきは「SAFE」をそれぞれ 1 行に出力しなさい Sample Input 6 1 1 . 3 3 ... .W. ... 10 10 .......... .DDDDCCC.. .DDDDCCC.. .DDDDCCC.. ADDDDCCC.. AAA..CCC.. AAABBBBC.. AAABBBB... ..BBBBB... .......... 10 10 .......... .DDDDDD... .DDDDCCC.. .DDDDCCC.. ADDDDCCC.. AAA..CCC.. AAABBBBC.. AAABBBB... ..BBBBB... .......... 10 10 R..E..C.T. R.EEE.C.T. .EEEEE.... EEEEEEE... .EEEEEEE.. ..EEEEEEE. ...EEEEEEE ....EEEEE. .....EEE.. ......E... 16 50 .................................................. .........AAAAAAAAAAAAAAAA......................... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... ....PPP..............AAAAA.AAAAAAAAAAAAAAAA....... ....PPP................A....AAA.AAAAAAAAAA........ ....PPP...........IIIIIAAIIAIII.AAAAAAAAAA........ ..CCCCCCCCCCCCC...IIIIIIAAAAAAAAAAAAAAAAAA........ ..CCCCCCCCCCCCC...IIIIIIIIIIIII...AAAAAAAAAA...... ....PPP............................AAAAAAAAAA..... MMMMPPPMMMMMMMMMMMMMMM.............AAAAAAAAAAA.... MMMMPPPMMMMMMMMMMMMMMM..............AAAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM...............AAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM...............AAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM............................ Output for the Sample Input SAFE SAFE SAFE SUSPICIOUS SUSPICIOUS SUSPICIOUS
37,628
Problem H: Magic Walls You are a magician and have a large farm to grow magical fruits. One day, you saw a horde of monsters, approaching your farm. They would ruin your magical farm once they reached your farm. Unfortunately, you are not strong enough to fight against those monsters. To protect your farm against them, you decided to build magic walls around your land. You have four magic orbs to build the walls, namely, the orbs of Aquamarine (A), Bloodstone (B), Citrine (C) and Diamond (D). When you place them correctly and then cast a spell, there will be magic walls between the orbs A and B, between B and C, between C and D, and between D and A. The walls are built on the line segments connecting two orbs, and form a quadrangle as a whole. As the monsters cannot cross the magic walls, the inside of the magic walls is protected. Nonetheless, you can protect only a part of your land, since there are a couple of restrictions on building the magic walls. There are N hills in your farm, where the orbs can receive rich power of magic. Each orb should be set on the top of one of the hills. Also, to avoid interference between the orbs, you may not place two or more orbs at the same hill. Now, you want to maximize the area protected by the magic walls. Please figure it out. Input The input begins with an integer N (4 ≀ N ≀ 1500), the number of the hills. Then N line follow. Each of them has two integers x (0 ≀ x ≀ 50000) and y (0 ≀ y ≀ 50000), the x - and y -coordinates of the location of a hill. It is guaranteed that no two hills have the same location and that no three hills lie on a single line. Output Output the maximum area you can protect. The output value should be printed with one digit after the decimal point, and should be exact. Sample Input 1 5 2 0 0 1 1 3 4 2 3 4 Output for the Sample Input 1 7.5 Sample Input 2 4 0 0 0 3 1 1 3 0 Output for the Sample Input 2 3.0
37,629
Problem G: Surface Area of Cubes Taro likes a single player game "Surface Area of Cubes". In this game, he initially has an $A \times B \times C$ rectangular solid formed by $A \times B \times C$ unit cubes (which are all 1 by 1 by 1 cubes). The center of each unit cube is placed at 3-dimentional coordinates $(x, y, z)$ where $x$, $y$, $z$ are all integers ($0 \leq x \leq A-1, 0 \leq y \leq B -1, 0 \leq z \leq C - 1$). Then, $N$ distinct unit cubes are removed from the rectangular solid by the game master. After the $N$ cubes are removed, he must precisely tell the total surface area of this object in order to win the game. Note that the removing operation does not change the positions of the cubes which are not removed. Also, not only the cubes on the surface of the rectangular solid but also the cubes at the inside can be removed. Moreover, the object can be divided into multiple parts by the removal of the cubes. Notice that a player of this game also has to count up the surface areas which are not accessible from the outside. Taro knows how many and which cubes were removed but this game is very difficult for him, so he wants to win this game by cheating! You are Taro's friend, and your job is to make a program to calculate the total surface area of the object on behalf of Taro when you are given the size of the rectangular solid and the coordinates of the removed cubes. Input The input is formatted as follows. The first line of a test case contains four integers $A$, $B$, $C$, and $N$ ($1 \leq A, B, C \leq 10^8, 0 \leq N \leq $ min{$1,000, A \times B \times C - 1$}). Each of the next $N$ lines contains non-negative integers $x$, $y$, and $z$ which represent the coordinate of a removed cube. You may assume that these coordinates are distinct. Output Output the total surface area of the object from which the $N$ cubes were removed. Sample Input 2 2 2 1 0 0 0 Output for the Sample Input 24 Sample Input 1 1 5 2 0 0 1 0 0 3 Output for the Sample Input 18 Sample Input 3 3 3 1 1 1 1 Output for the Sample Input 60
37,630
Score : 700 points Problem Statement Snuke has decided to play a game, where the player runs a railway company. There are M+1 stations on Snuke Line, numbered 0 through M . A train on Snuke Line stops at station 0 and every d -th station thereafter, where d is a predetermined constant for each train. For example, if d = 3 , the train stops at station 0 , 3 , 6 , 9 , and so forth. There are N kinds of souvenirs sold in areas around Snuke Line. The i -th kind of souvenirs can be purchased when the train stops at one of the following stations: stations l_i , l_i+1 , l_i+2 , ... , r_i . There are M values of d , the interval between two stops, for trains on Snuke Line: 1 , 2 , 3 , ... , M . For each of these M values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of d at station 0 . Here, assume that it is not allowed to change trains. Constraints 1 ≩ N ≩ 3 × 10^{5} 1 ≩ M ≩ 10^{5} 1 ≩ l_i ≩ r_i ≩ M Input The input is given from Standard Input in the following format: N M l_1 r_1 : l_{N} r_{N} Output Print the answer in M lines. The i -th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every i -th station. Sample Input 1 3 3 1 2 2 3 3 3 Sample Output 1 3 2 2 If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind 1 , 2 and 3 . If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind 1 and 2 . If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind 2 and 3 . Sample Input 2 7 9 1 7 5 9 5 7 5 9 1 1 6 8 3 4 Sample Output 2 7 6 6 5 4 5 5 3 2
37,631
Problem C: How can I satisfy thee? Let me count the ways... Three-valued logic is a logic system that has, in addition to "true" and "false", "unknown" as a valid value. In the following, logical values "false", "unknown" and "true" are represented by 0, 1 and 2 respectively. Let "-" be a unary operator (i.e. a symbol representing one argument function) and let both "*" and "+" be binary operators (i.e. symbols representing two argument functions). These operators represent negation (NOT), conjunction (AND) and disjunction (OR) respectively. These operators in three-valued logic can be defined in Table C-1. Table C-1: Truth tables of three-valued logic operators -X (X*Y) (X+Y) Let P, Q and R be variables ranging over three-valued logic values. For a given formula, you are asked to answer the number of triples (P,Q,R) that satisfy the formula, that is, those which make the value of the given formula 2. A formula is one of the following form (X and Y represent formulas). Constants: 0, 1 or 2 Variables: P, Q or R Negations: -X Conjunctions: (X*Y) Disjunctions: (X+Y) Note that conjunctions and disjunctions of two formulas are always parenthesized. For example, when formula (P*Q) is given as an input, the value of this formula is 2 when and only when (P,Q,R) is (2,2,0), (2,2,1) or (2,2,2). Therefore, you should output 3. Input The input consists of one or more lines. Each line contains a formula. A formula is a string which consists of 0, 1, 2, P, Q, R, -, *, +, (, ). Other characters such as spaces are not contained. The grammar of formulas is given by the following BNF. <formula> ::= 0 | 1 | 2 | P | Q | R | -<formula> | (<formula>*<formula>) | (<formula>+<formula>) All the formulas obey this syntax and thus you do not have to care about grammatical errors. Input lines never exceed 80 characters. Finally, a line which contains only a "." (period) comes, indicating the end of the input. Output You should answer the number (in decimal) of triples (P,Q,R) that make the value of the given formula 2. One line containing the number should be output for each of the formulas, and no other characters should be output. Sample Input (P*Q) (--R+(P*Q)) (P*-P) 2 1 (-1+(((---P+Q)*(--Q+---R))*(-R+-P))) . Output for the Sample Input 3 11 0 27 0 7
37,632
Electric Metronome A boy PCK is playing with N electric metronomes. The i -th metronome is set to tick every t_i seconds. He started all of them simultaneously. He noticed that, even though each metronome has its own ticking interval, all of them tick simultaneously from time to time in certain intervals. To explore this interesting phenomenon more fully, he is now trying to shorten the interval of ticking in unison by adjusting some of the metronomes’ interval settings. Note, however, that the metronomes do not allow any shortening of the intervals. Given the number of metronomes and their preset intervals t_i (sec), write a program to make the tick-in-unison interval shortest by adding a non-negative integer d_i to the current interval setting of the i -th metronome, and report the minimum value of the sum of all d_i . Input The input is given in the following format. N t_1 t_2 : t_N The first line provides the number of metronomes N (1 ≀ N ≀ 10 5 ). Each of the subsequent N lines provides the preset ticking interval t_i (1 ≀ t_i ≀ 10 4 ) of the i -th metronome. Output Output the minimum value. Sample Input 1 3 3 6 8 Sample Output 1 3 If we have three metronomes each with a ticking interval of 3, 6, and 8 seconds, respectively, simultaneous activation of these will produce a tick in unison every 24 seconds. By extending the interval by 1 second for the first, and 2 seconds for the second metronome, the interval of ticking in unison will be reduced to 8 seconds, which is the shortest possible for the system. Sample Input 2 2 10 10 Sample Output 2 0 If two metronomes are both set to 10 seconds, then simultaneous activation will produce a tick in unison every 10 seconds, which is the shortest possible for this system.
37,633
サむコロパズル 各面にアルファベット䞀文字(a 〜 z、A 〜 Z)が描かれたサむコロがありたす。 このようなサむコロを8぀組み合わせお 2 × 2 × 2 の立方䜓を䜜るこずを考えたす。 組み合わせ方には条件があり、各サむコロの向き合う面は同じアルファベットでか぀䞀方が小文字、も う䞀方が倧文字でなければなりたせん。䟋えば、a ず描かれた面に接するこずができるのは A ず描か れた面です。ただし、接するずきの文字の向きは問いたせん。 このルヌルに埓い、぀のサむコロの情報を入力ずし、立方䜓を䜜れるか吊かを刀定するプログラムを䜜成しおください。立方䜓を䜜れる堎合は YES半角英倧文字、䜜れない堎合は NO半角英倧文字ず出力しおください。 なお、サむコロの各面の文字を次の図にあるように c1 〜 c6 ず衚すこずにしたす。 たた、1぀のサむコロに同じ文字が耇数回描かれおいるこずは無いものずしたす同じアルファベットの倧文字ず小文字はその限りではありたせん。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 s 1 s 2 : s 8 i 行目に i 個目のサむコロの情報 s i が䞎えられたす。 s i は長さ 6 の文字列であり j 文字目がサむコロの各面 cj に察応したす。 デヌタセットの数は 50 を超えたせん。 Output デヌタセットごずに刀定結果半角英倧文字を行に出力したす。 Sample Input zabZNq BCxmAi ZcbBCj aizXCm QgmABC JHzMop ImoXGz MZTOhp zabZnQ BCxmAi ZcbBCj aizXCm QgmABC JHzMop ImoXGz MZTOhp abcdef ABDCFE FBDCAE abcdef BEACDF bfcaed fabcde DEABCF UnivOf AizuaH zTXZYW piglIt GRULNP higGtH uAzIXZ FizmKZ UnivOf AizuaH piglIt higGtH GRULNP uAzIXZ FizmKZ ZTXzYW 0 Output for the Sample Input YES NO YES YES NO
37,634
JOI 王章(JOI Emblem) 情報オリンピック日本委員䌚では台湟倧䌚に向かう遞手を応揎するために新しいJOI 旗を䜜成するこずにした JOI 旗は正方圢が瞊に $M$ 行暪に $N$ 列䞊んだ圢をしおおり各正方圢には JOI のいずれかの文字が1 ぀ず぀曞かれおいる 情報オリンピック日本委員䌚はJOI 旗ずは別にJOI 王章ずいうものを決めおいるJOI 王章は正方圢が瞊に2 行暪に2 列䞊んだ圢をしおおり各正方圢にはJOI のいずれかの文字が1 ぀ず぀曞かれおいる JOI 旗に含たれるJOI 王章の個数ずはJOI 旗に含たれる瞊2 行暪2 列の領域のうちその領域のJOI の配眮がJOI 王章ず(回転や裏返しをせずに) 䞀臎しおいるものの個数のこずである条件を満たす瞊2 行暪2 列の領域同士が重なっおいおもそれらを別々に数えるものずする 情報オリンピック日本委員䌚は叀いJOI 旗ず1 枚の癜玙を持っおいる癜玙はJOI 旗を構成する正方圢1 個分の倧きさでJOI のうち奜きな1 文字を曞き蟌むこずができる情報オリンピック日本委員䌚は以䞋のいずれか1 ぀の操䜜をしお新しいJOI 旗を䜜成するこずにした 叀いJOI 旗に察しお䜕も操䜜せずそのたた新しいJOI 旗ずする癜玙は䜿甚しない 癜玙に1 文字曞き蟌み叀いJOI 旗のいずれかの正方圢に重ねお貌り付けるこずで叀いJOI 旗のうち1 箇所を倉曎する倉曎埌のJOI 旗を新しいJOI 旗ずする 情報オリンピック日本委員䌚は新しいJOI 旗に含たれるJOI 王章の個数をできるだけ倚くしたいず思っおいるあなたは新しいJOI 旗に含たれるJOI 王章の個数の最倧倀を求めるこずになった 課題 叀いJOI 旗ずJOI 王章の情報が䞎えられたずき新しいJOI 旗に含たれるJOI 王章の個数の最倧倀を求めるプログラムを䜜成せよ 入力 暙準入力から以䞋のデヌタを読み蟌め 1 行目には2 個の敎数 $M, N$ が空癜を区切りずしお曞かれおいるこれはJOI 旗が正方圢が瞊に$M$行暪に$N$ 列䞊んだ圢であるこずを衚しおいる 続く$M$ 行の各行にはそれぞれ$N$ 文字からなる文字列が曞かれおいる各文字はJOI のいずれかであり$M$ 行のうち䞊から$i$ 行目$(1 \leq i \leq M)$ に曞かれおいる文字列の巊から$j$ 文字目$(1 \leq j \leq N)$は叀いJOI 旗の䞊から$i$ 行目巊から$j$ 列目の正方圢に曞かれおいる文字を衚す 続く2 行の各行にはそれぞれ2 文字からなる文字列が曞かれおいる各文字はJOI のいずれかであり2 行のうち䞊から$i$ 行目$(1 \leq i \leq 2)$ に曞かれおいる文字列の巊から$j$ 文字目$(1 \leq j \leq 2)$ はJOI 王章の䞊から$i$ 行目巊から$j$ 列目の正方圢に曞かれおいる文字を衚す 出力 暙準出力に新しいJOI 旗に含たれるJOI 王章の個数の最倧倀を衚す敎数を1 行で出力せよ 制玄 すべおの入力デヌタは以䞋の条件を満たす $2 \leq M \leq 1000$ $2 \leq N \leq 1000$ 入出力䟋 入力䟋 1 3 5 JOIJO IJOOO IIJIJ JO IJ 出力䟋 1 3 叀いJOI 旗ずJOI 王章は問題文䞭の䟋の通りである䞊から2 行目巊から4 列目の正方圢を癜玙を甚いおJ に倉曎するず次のような圢になる このように倉曎した埌のJOI 旗には次に瀺す3 箇所にJOI 王章ず同じ配眮の領域がある JOI 王章ず同じ配眮の領域が4 箇所以䞊ずなる新しいJOI 旗は存圚しないので新しいJOI 旗に含たれるJOI 王章の個数の最倧倀は3 である 入力䟋 2 2 6 JOJOJO OJOJOJ OJ JO 出力䟋 2 2 癜玙を䜿甚しなくおも最倧倀が埗られる堎合があるこずに泚意せよ 入力䟋 3 2 2 JI IJ JJ JJ 出力䟋 3 0 この入出力䟋の堎合考えられるどの新しいJOI 旗においおもJOI 王章が1 ぀も含たれない 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,635
A : Taps / 蛇口 Problem あいづ君の家の济槜には2぀の蛇口 A , B が぀いおおり、 ひねるずひねった角床に比䟋した量の氎が出おくる。 出おくる氎の枩床は A は t A [℃]、 B は t B [℃]で䞀定である。 颚呂に入るずきにはそれらの量を調節しお混ぜあわせるこずで適床な枩床になるようにしおいる。 t A [℃] の氎 v A [L] ず t B [℃]の氎 v B [L] を混ぜ合わせた埌の枩床は、( t A × v A + t B × v B ) / ( v A + v B ) になる。 䟋えば、 t A = 80℃, t B = 20℃ で 40℃ のお湯を匵りたいずきには、 A から 100mL/s 、 B から 200mL/s ず぀出るように蛇口を調節すればよい。 ずころが、このごろ蛇口が老朜化しおきたため A は d A [mL/s] 、 B は d B [mL/s] 単䜍でしか 出る氎の量を調節できなくなっおしたった。 すなわち、 A から出る氎の量が 0, d A , 2× d A , 3× d A , ...[mL/s] ずいう 飛び飛びの倀にしか蚭定できなくなっおしたったのである( B も同様)。 あいづ君は長幎培っおきた勘によっお狙った枩床ぎったりに 調節できたのだが、それが通甚しなくなった。 困ったあいづ君は、自動的に目的の枩床 T [℃] に最も近い枩床になるような氎の出し方に調節するプログラムを曞くこずにした。 ただし、毎秒出せる氎の量には䞊限があり、 A ず B の氎の量の和は D [mL/s] 以䞋でなければならない。 たた、氎を出さないこずにはお颚呂に入れないため、毎秒出る氎の量は最䜎でも 1mL/s 以䞊でなければならない。 ぀たり、 1 ≩ A から出る氎の量 + B から出る氎の量 ≩ D を満たす必芁がある。 Input 入力は次のような圢匏で䞎えられる T D t A t B d A d B 1行目の T , D は、それぞれ目的の枩床、氎の量の䞊限を衚す。 2行目の t A , t B は、それぞれ A の枩床、 B の枩床を衚す。 3行目の d A , d B は、それぞれ A から出る氎の量の単䜍、 B から出る氎の量の単䜍を衚す。 Constraints 入力は党お敎数である。 1 ≩ T , t A , t B ≩ 99 1 ≩ D ≩ 1000 1 ≩ d A , d B ≩ D Output T に最も近くなるように氎を混ぜあわせた埌の枩床ず T ずの差の倧きさの最小倀を1行で出力せよ。ただし、10 -4 以䞋の誀差は蚱容される。 Samples Sample Input 1 40 1000 80 20 100 100 Sample Output 1 0.0000000000 問題文の䟋である。 Sample Input 2 38 1000 80 20 100 95 Sample Output 2 0.2222222222 Aの蛇口から 200mL/s、Bの蛇口から 475mL/s の量の氎を出したずき、37.7777...℃ ずなり、これが38℃ に最も近くなる。
37,636
Problem I: Common Polynomial Math teacher Mr. Matsudaira is teaching expansion and factoring of polynomials to his students. Last week he instructed the students to write two polynomials (with a single variable x), and to report GCM (greatest common measure) of them as a homework, but he found it boring to check their answers manually. So you are asked to write a program to check the answers. Hereinafter, only those polynomials with integral coefficients, called integral polynomials, are considered. When two integral polynomials A and B are given, an integral polynomial C is a common factor of A and B if there are some integral polynomials X and Y such that A = CX and B = CY . GCM of two integral polynomials is a common factor which has the highest degree (for x , here); you have to write a program which calculates the GCM of two polynomials. It is known that GCM of given two polynomials is unique when constant multiplication factor is ignored. That is, when C and D are both GCM of some two polynomials A and B , p × C = q × D for some nonzero integers p and q . Input The input consists of multiple datasets. Each dataset constitutes a pair of input lines, each representing a polynomial as an expression defined below. A primary is a variable x, a sequence of digits 0 - 9, or an expression enclosed within ( ... ). Examples: x, 99, (x+1) A factor is a primary by itself or a primary followed by an exponent. An exponent consists of a symbol ^ followed by a sequence of digits 0 - 9. Examples: x^05, 1^15, (x+1)^3 A term consists of one or more adjoining factors. Examples: 4x, (x+1)(x-2), 3(x+1)^2 An expression is one or more terms connected by either + or -. Additionally, the first term of an expression may optionally be preceded with a minus sign -. Examples: -x+1, 3(x+1)^2-x(x-1)^2 Integer constants, exponents, multiplications (adjoining), additions (+) and subtractions/negations (-) have their ordinary meanings. A sequence of digits is always interpreted as an integer con- stant. For example, 99 means 99, not 9 × 9. Any subexpressions of the input, when fully expanded normalized, have coefficients less than 100 and degrees of x less than 10. Digit sequences in exponents represent non-zero values. All the datasets are designed so that a standard algorithm with 32-bit two’s complement integers can solve the problem without overflows. The end of the input is indicated by a line containing a period. Output For each of the dataset, output GCM polynomial expression in a line, in the format below. c 0 x^ p 0 ± c 1 x^ p 1 ... ± c n x^ p n Where c i and p i ( i = 0, . . . , n ) are positive integers with p 0 > p 1 > . . . > p n , and the greatest common divisor of { c i | i = 0, . . . , n } is 1. Additionally: When c i is equal to 1, it should be omitted unless corresponding p i is 0, x^0 should be omitted as a whole, and x^1 should be written as x. Sample Input -(x^3-3x^2+3x-1) (x-1)^2 x^2+10x+25 x^2+6x+5 x^3+1 x-1 . Output for the Sample Input x^2-2x+1 x+5 1
37,637
Problem J: 倚項匏の解の個数 きたたさ君は倧孊でさたざたな倉換に぀いお研究しおいる。最近、きたたさ君が興味を持っおいる倉換は次のようなものである。 N+1個の敎数 a 0 , ..., a N を固定し、敎数 z を入力ずする。たた P を玠数ずする。 t = ( a N z N + a N-1 z N-1 + ... + a 2 z 2 + a 1 z + a 0 ) mod P きたたさ君はこの倉換によっお t = 0 になっおしたう z がいく぀もあるこずに気が぀いた。 そこで、きたたさ君は友人でありスヌパヌプログラマヌでもあるあなたに、倉換埌に t = 0 になる z が 0〜P-1 にいく぀あるかを蚈算しおもらうこずにした。 Input 入力は次の圢匏で䞎えられる。 N P a 0 a 1 ... a N 入力の1行目には敎数Nず玠数Pがスペヌス文字で区切られお䞎えられる。これらは、問題文で䞎えられたずおりである。 0 ≀ N ≀ 100, 2 ≀ P ≀ 10 9 を満たす。 次の1行には a 0 〜a N がスペヌス文字で区切られお䞎えられる。これらは |a i | ≀ 10 9 を満たす敎数である。たた a N ≠ 0 である。 Output 倉換埌に0になるような z (0 ≀ z < P) の個数を出力せよ。 Notes on Test Cases 䞊蚘入力圢匏で耇数のデヌタセットが䞎えられたす。各デヌタセットに察しお䞊蚘出力圢匏で出力を行うプログラムを䜜成しお䞋さい。 N, P がずもに 0 のずき入力の終わりを瀺したす。 Sample Input 2 3 1 2 1 2 3 1 2 6 0 0 Output for Sample Input 1 1
37,638
Score : 500 points Problem Statement You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters. You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j . You can perform this operation at most once. How many different strings can you obtain? Constraints 1 \leq |A| \leq 200,000 A consists of lowercase English letters. Input Input is given from Standard Input in the following format: A Output Print the number of different strings you can obtain by reversing any substring in A at most once. Sample Input 1 aatt Sample Output 1 5 You can obtain aatt (don't do anything), atat (reverse A[2..3] ), atta (reverse A[2..4] ), ttaa (reverse A[1..4] ) and taat (reverse A[1..3] ). Sample Input 2 xxxxxxxxxx Sample Output 2 1 Whatever substring you reverse, you'll always get xxxxxxxxxx . Sample Input 3 abracadabra Sample Output 3 44
37,639
テトラ姫のパズル パレス王囜のテトラ姫は無類のパズル奜きずしお知られおおり最近は自身が考案した「テトラパズル」に熱䞭しおいる これは正䞉角圢のマスを敷き詰めたボヌドずテトラブロックず呌ばれる 4 枚の正䞉角圢のパネルからなる正四面䜓のブロックを䜿ったパズルである パズルの目的はボヌド䞊のすべおのテトラブロックを以䞋の条件をみたすように展開するこずである 展開されたテトラブロックが眮かれおいたマスおよびそれぞれに指定された異なる 2 ぀のマスを芆っおいる どのマスも2 ぀以䞊のパネルで芆われおいない 図F-1はテトラパズルの問題䟋およびその解答䟋である ★のマスず●のマスにテトラブロックが眮かれおおりそれぞれが☆のマスず○のマスを芆うように指定されおいる 解答䟋においお暪線が匕かれたマスは★のマスに眮かれおいたテトラブロックを展開しお芆ったマス瞊線のマスは●のマスに眮かれたテトラブロックで芆ったマスである 図 F-1: テトラパズルの問題䟋(å·Š)および解答䟋(右) 図F-2は䞊図の問題䟋における無効な解答䟋である 巊の䟋では★のマスの真䞊のマスが2぀のパネルで芆われおおり右の䟋ではそれぞれがテトラブロックを展開しお埗られる圢状でないため答えずしおは認められない 図 F-2: 無効な解答䟋 テトラ姫はパズルの面癜さを倚くの人に知っおもらうべく来たる倏の祭兞に向けおパズルの問題集を䜜った そしお王囜で二番目のパズル奜きずしお姫から信頌を眮かれおいるあなたは問題集のチェック担圓に任呜された さおチェック䜜業を進めおいたあなたは困った事に気が付いた 問題集には解が存圚しないパズルが含たれおいるようなのだ なるべく姫の䜜ったパズルを尊重したいず考えたあなたは解が存圚しないパズルに぀いおどれか 1 個のテトラブロックをそのテトラブロックが芆うべきマスの情報ごず削陀するこずでパズルを解けるものに倉えるこずにした 姫から蚀い枡されたチェックの締切りたではあず3時間しかない 問題集を芋なおしお手早く修正を枈たせおしたおう Input 入力は耇数のデヌタセットから構成される 各デヌタセットの圢匏は次の通りである n x 1a y 1a x 1b y 1b x 1c y 1c x 2a y 2a x 2b y 2b x 2c y 2c ... x na y na x nb y nb x nc y nc n (2 ≀ n ≀ 5,000) はボヌドに眮かれたテトラブロックの数を衚す敎数である 各テトラブロックには 1 から n の番号が割り圓おられおいる 続く n 行はそれぞれ 1 個のスペヌスで区切られた 6 個の敎数からなる (x ia , y ia ) は i 番目のテトラブロックが眮かれおいるマスの座暙を衚し(x ib , y ib ) および(x ic , y ic ) はそれぞれ i 番目のテトラブロックを展開した時に芆うべきマスの座暙を衚す 䞎えられる x 座暙および y 座暙の絶察倀は 20,000 以䞋ず仮定しお良い たた䞎えられる座暙は党お互いに異なるず仮定しお良い すなわち同じマスに 2 個以䞊のテトラブロックが眮かれる事やテトラブロックが眮かれおいるマスが芆うべきマスずしお指定される事や芆うべきマスが重耇しお指定される事はない 各マスには図F-3のように座暙が割り圓おられおいる 図 F-3: マスに割り圓おられた座暙 n=0 は入力の終わりを瀺すこれはデヌタセットには含めない Output 各デヌタセットに぀いおテトラブロックを陀くこずなくパズルが解ける堎合は Valid をそれぞれ 1 行に出力しなさい テトラブロックを 1 個だけ陀くこずでパズルが解けるようになる堎合は Remove を 1 行に出力し続けお陀くこずでパズルが解けるようになるテトラブロックの番号を小さいものから順にそれぞれ 1 行に出力しなさい 2 個以䞊のテトラブロックを陀かないずパズルが解けるようにならない堎合は Invalid を1行に出力しなさい Sample Input 3 2 3 2 2 2 1 2 0 1 -1 2 -1 -2 -1 -1 -1 -2 0 6 -1 0 1 0 0 -1 -2 -1 -3 -2 -4 -2 2 -1 2 0 1 -1 4 -2 3 -2 3 -1 1 1 0 1 -1 1 -3 -1 -1 -1 -2 0 5 -1 2 0 2 1 2 -3 1 -2 1 -2 2 -2 0 -2 -1 -3 0 1 -1 0 -1 -1 -1 1 0 2 0 2 -1 4 -2 0 -2 -1 -3 -1 -1 -1 0 -1 1 -1 2 -1 2 0 3 -1 -1 0 0 0 1 0 5 -2 1 3 -1 2 1 -5 2 -5 3 -4 2 -2 -1 0 -1 -3 -1 4 2 5 2 5 3 1 -1 2 -1 -1 -1 0 以䞋の図はそれぞれサンプルの配眮を瀺しおいる 図 F-4: 1番目のサンプル 図 F-5: 2番目のサンプル 図 F-6: 3番目のサンプル 図 F-7: 4番目のサンプル 図 F-8: 5番目のサンプル Output for Sample Input Remove 1 Remove 2 6 Valid Remove 1 2 3 4 Invalid
37,640
Problem C Sibling Rivalry You are playing a game with your elder brother. First, a number of circles and arrows connecting some pairs of the circles are drawn on the ground. Two of the circles are marked as the start circle and the goal circle . At the start of the game, you are on the start circle. In each turn of the game, your brother tells you a number, and you have to take that number of steps . At each step, you choose one of the arrows outgoing from the circle you are on, and move to the circle the arrow is heading to. You can visit the same circle or use the same arrow any number of times. Your aim is to stop on the goal circle after the fewest possible turns, while your brother's aim is to prevent it as long as possible. Note that, in each single turn, you must take the exact number of steps your brother tells you. Even when you visit the goal circle during a turn, you have to leave it if more steps are to be taken. If you reach a circle with no outgoing arrows before completing all the steps, then you lose the game. You also have to note that, your brother may be able to repeat turns forever, not allowing you to stop after any of them. Your brother, mean but not too selfish, thought that being allowed to choose arbitrary numbers is not fair. So, he decided to declare three numbers at the start of the game and to use only those numbers. Your task now is, given the configuration of circles and arrows, and the three numbers declared, to compute the smallest possible number of turns within which you can always nish the game, no matter how your brother chooses the numbers. Input The input consists of a single test case, formatted as follows. $n$ $m$ $a$ $b$ $c$ $u_1$ $v_1$ ... $u_m$ $v_m$ All numbers in a test case are integers. $n$ is the number of circles $(2 \leq n \leq 50)$. Circles are numbered 1 through $n$. The start and goal circles are numbered 1 and $n$, respectively. $m$ is the number of arrows $(0 \leq m \leq n(n - 1))$. $a$, $b$, and $c$ are the three numbers your brother declared $(1 \leq a, b, c \leq 100)$. The pair, $u_i$ and $v_i$, means that there is an arrow from the circle $u_i$ to the circle $v_i$. It is ensured that $u_i \ne v_i$ for all $i$, and $u_i \ne u_j$ or $v_i \ne v_j$ if $i \ne j$. Output Print the smallest possible number of turns within which you can always finish the game. Print IMPOSSIBLE if your brother can prevent you from reaching the goal, by either making you repeat the turns forever or leading you to a circle without outgoing arrows. Sample Input 1 3 3 1 2 4 1 2 2 3 3 1 Sample Output 1 IMPOSSIBLE Sample Input 2 8 12 1 2 3 1 2 2 3 1 4 2 4 3 4 1 5 5 8 4 6 6 7 4 8 6 8 7 8 Sample Output 2 2 For Sample Input 1, your brother may choose 1 first, then 2, and repeat these forever. Then you can never finish. For Sample Input 2 (Figure C.1), if your brother chooses 2 or 3, you can finish with a single turn. If he chooses 1, you will have three options. Move to the circle 5. This is a bad idea: Your brother may then choose 2 or 3 and make you lose. Move to the circle 4. This is the best choice: From the circle 4, no matter any of 1, 2, or 3 your brother chooses in the next turn, you can finish immediately. Move to the circle 2. This is not optimal for you. If your brother chooses 1 in the next turn, you cannot finish yet. It will take three or more turns in total. In summary, no matter how your brother acts, you can finish within two turns. Thus the answer is 2. Figure C.1. Sample Input 2
37,641
Score : 700 points Problem Statement You are given a string s of length n . Does a tree with n vertices that satisfies the following conditions exist? The vertices are numbered 1,2,..., n . The edges are numbered 1,2,..., n-1 , and Edge i connects Vertex u_i and v_i . If the i -th character in s is 1 , we can have a connected component of size i by removing one edge from the tree. If the i -th character in s is 0 , we cannot have a connected component of size i by removing any one edge from the tree. If such a tree exists, construct one such tree. Constraints 2 \leq n \leq 10^5 s is a string of length n consisting of 0 and 1 . Input Input is given from Standard Input in the following format: s Output If a tree with n vertices that satisfies the conditions does not exist, print -1 . If a tree with n vertices that satisfies the conditions exist, print n-1 lines. The i -th line should contain u_i and v_i with a space in between. If there are multiple trees that satisfy the conditions, any such tree will be accepted. Sample Input 1 1111 Sample Output 1 -1 It is impossible to have a connected component of size n after removing one edge from a tree with n vertices. Sample Input 2 1110 Sample Output 2 1 2 2 3 3 4 If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3 . If Edge 2 is removed, we will have two connected components, each of size 2 . Sample Input 3 1010 Sample Output 3 1 2 1 3 1 4 Removing any edge will result in a connected component of size 1 and another of size 3 .
37,642
Problem C: Dial Lock A dial lock is a kind of lock which has some dials with printed numbers. It has a special sequence of numbers, namely an unlocking sequence, to be opened. You are working at a manufacturer of dial locks. Your job is to verify that every manufactured lock is unlocked by its unlocking sequence. In other words, you have to rotate many dials of many many locks. It’s a very hard and boring task. You want to reduce time to open the locks. It’s a good idea to rotate multiple dials at one time. It is, however, a difficult problem to find steps to open a given lock with the fewest rotations. So you decided to write a program to find such steps for given initial and unlocking sequences. Your company’s dial locks are composed of vertically stacked k (1 ≀ k ≀ 10) cylindrical dials. Every dial has 10 numbers, from 0 to 9, along the side of the cylindrical shape from the left to the right in sequence. The neighbor on the right of 9 is 0. A dial points one number at a certain position. If you rotate a dial to the left by i digits, the dial newly points the i -th right number. In contrast, if you rotate a dial to the right by i digits, it points the i -th left number. For example, if you rotate a dial pointing 8 to the left by 3 digits, the dial newly points 1. You can rotate more than one adjacent dial at one time. For example, consider a lock with 5 dials. You can rotate just the 2nd dial. You can rotate the 3rd, 4th and 5th dials at the same time. But you cannot rotate the 1st and 3rd dials at one time without rotating the 2nd dial. When you rotate multiple dials, you have to rotate them to the same direction by the same digits. Your program is to calculate the fewest number of rotations to unlock, for given initial and unlocking sequences. Rotating one or more adjacent dials to the same direction by the same digits is counted as one rotation. Input The input consists of multiple datasets. Each datset consists of two lines. The first line contains an integer k. The second lines contain two strings, separated by a space, which indicate the initial and unlocking sequences. The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed. Output For each dataset, print the minimum number of rotations in one line. Sample Input 4 1357 4680 6 777777 003330 0 Output for the Sample Input 1 2
37,643
鉄道路線 耇線䞊りず䞋りが別の線路になっおいおどこででもすれ違えるの鉄道路線がありたす。この路線には終端駅を含めお11 の駅があり、それぞれの駅ず駅の間は図で瀺す区間番号で呌ばれおいたす。 この路線の䞡方の終端駅から列車が同時に出発しお、途䞭で停たらずに走りたす。各区間の長さず2 本の列車の速床を読み蟌んで、それぞれの堎合に぀いお列車がすれ違う区間の番号を出力するプログラムを䜜成しおください。ただし、ちょうど駅のずころですれ違う堎合は、䞡偎の区間番号のうち小さいほうの数字を出力したす。たた、列車の長さ、駅の長さは無芖できるものずしたす。 Input 耇数のデヌタセットが䞎えられる。各デヌタセットは以䞋のような圢匏で䞎えられる。 l 1 , l 2 , l 3 , l 4 , l 5 , l 6 , l 7 , l 8 , l 9 , l 10 , v 1 , v 2 l i (1 ≀ l i ≀ 2,000) は区間 i の長さ(km)を衚す敎数である。 v 1 は区間 1 偎の終端駅を出発した列車の速床(km/h)、 v 2 は区間 10 偎の終端駅を出発した列車の速床(km/h)を衚す敎数である (1 ≀ v 1 , v 2 ≀ 2,000)。 デヌタセットの数は 50 を超えない。 Output デヌタセットごずに、列車がすれちがう区間の番号を行に出力する。 Sample Input 1,1,1,1,1,1,1,1,1,1,40,60 1,1,1,1,1,3,3,3,3,3,50,50 10,10,10,10,10,10,10,10,10,10,50,49 Output for the Sample Input 4 7 6
37,644