task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Balls and Boxes 9 Balls Boxes Any way At most one ball At least one ball Distinguishable Distinguishable 1 2 3 Indistinguishable Distinguishable 4 5 6 Distinguishable Indistinguishable 7 8 9 Indistinguishable Indistinguishable 10 11 12 Problem You have $n$ balls and $k$ boxes. You want to put these balls into the boxes. Find the number of ways to put the balls under the following conditions: Each ball is distinguished from the other. Each box is not distinguished from the other. Each ball can go into only one box and no one remains outside of the boxes. Each box must contain at least one ball. Note that you must print this count modulo $10^9+7$. Input $n$ $k$ The first line will contain two integers $n$ and $k$. Output Print the number of ways modulo $10^9+7$ in a line. Constraints $1 \le n \le 1000$ $1 \le k \le 1000$ Sample Input 1 4 3 Sample Output 1 6 Sample Input 2 10 5 Sample Output 2 42525 Sample Input 3 100 30 Sample Output 3 203169470
37,223
新薬開発 英䞖博士は日々研究を行い、新しい薬を開発しようずしおいたす。新薬を開発するためには、色々な物質を組み合わせお薬を䜜り詊隓を行い、良い薬を芋぀けなければなりたせん。様々な組み合わせを詊しおいくうちに、英䞖博士は物質の組み合わせが暹圢図で衚せるこずを突き止めたした。 右の図は、薬の調合を衚す暹圢図の䟋です。図の䞭で䞞く囲たれたものを物質ノヌド、䞉角で囲たれたものを遞択ノヌドず呌びたす。物質ノヌドは物質を衚したす。遞択ノヌドは、物質の遞択を衚すもので、それ自䜓は物質を衚したせん。遞択ノヌドには or 型√が付いたものず alt 型⇔が付いたものの皮類がありたす。たた ? が付いたノヌドは、それがオプションであるこずを衚したす。ただし、遞択ノヌドの子ノヌド䞋向きの枝の先にあるノヌドがオプションになるこずはありたせん。暹圢図に珟れる異なる物質ノヌドは、それぞれ別の物質を衚すものずしたす。 薬の調合を行うずきは、暹圢図の䞀番䞊のノヌドからはじめお、順々にノヌドをたどっおいきながら以䞋のようにしおノヌドを遞んでいきたす。 たどり着いたノヌドがオプションでない物質ノヌドなら、それを必ず遞ぶ。 オプションの物質ノヌドなら、それを遞ぶかどうかは任意。 or 型の遞択ノヌドなら、その子から少なくずも䞀぀を遞ぶ。ただし、その遞択ノヌドがオプションなら、子を䞀぀も遞ばなくおもよい。 alt 型の遞択ノヌドなら、その子から䞀぀だけを遞ぶ。ただし、その遞択ノヌドがオプションなら、子を遞ばなくおもよい。 あるノヌドが遞ばれたずきだけ、そのノヌドから䞋に向かう枝をそれぞれたどっおいきたす。遞ばれなければ、それらをたどるこずはありたせん。 あなたは英䞖博士から、薬の物質の組み合わせを衚す暹圢図を受け取り、組み合わせの数が党郚で䜕通りあるか求めるよう指瀺されたした。暹圢図が䞎えられたずき、組み合わせの総数を出力するプログラムを䜜成しおください。 入力 入力は以䞋の圢匏で䞎えられる。 N node 1 node 2 : node N s 1 t 1 s 2 t 2 : s N -1 t N -1 行目にノヌドの数 N (1 ≀ N ≀ 1000) が䞎えられる。続く N 行に、 i 番目のノヌドの情報 node i が䞎えられる。番目のノヌドを暹圢図の䞀番䞊のノヌドずする。続く N -1 行に s i 番目のノヌドからその子である t i 番目のノヌド (1 ≀ s i ≠ t i ≀ N ) ぞ向かう枝が䞎えられる。 t 1 から t N -1 たでには、2 から N たでの数が䞀床だけ珟れる。 ノヌドの情報は以䞋の圢匏である。 E オプションでない物質ノヌド。 E? オプションである物質ノヌド。 type オプションでない遞択ノヌド。 type は A か R のいずれかで、 A は alt型、 R は or 型を衚す。 type ? オプションである遞択ノヌド。 type の圢匏は同䞊。 入力から埗られる暹圢図は、以䞋の条件を満たす。 遞択ノヌドは぀以䞊の子ノヌドを持぀。 遞択ノヌドの子ノヌドはオプションでない。 暹圢図の䞀番䞊のノヌドはオプションでない。 どのノヌドに぀いおも、子ノヌドの数は 10 を超えない。 出力 䞎えられた暹圢図から埗られるすべおの組み合わせの総数を行に出力する。ただし、出力すべき倀は非垞に倧きくなりうるので、代わりに 1,000,000,007 で割った䜙りを出力する。 入出力䟋 入力䟋 12 A E E E R? E? E? E E E E? E 1 2 1 3 1 4 2 5 4 6 4 7 5 8 5 9 7 10 7 11 11 12 出力䟋 11 入力䟋 10 E R? E R E E A E E E 1 2 1 7 2 3 2 4 4 5 4 6 7 8 7 9 7 10 出力䟋 24
37,224
Score : 500 points Problem Statement You are given a string S consisting of 0 and 1 . Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times. Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r , do the following: if S_i is 0 , replace it with 1 ; if S_i is 1 , replace it with 0 . Constraints 1\leq |S|\leq 10^5 S_i(1\leq i\leq N) is either 0 or 1 . Input Input is given from Standard Input in the following format: S Output Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times. Sample Input 1 010 Sample Output 1 2 We can turn all the characters of S into 0 by the following operations: Perform the operation on the segment S[1,3] with length 3 . S is now 101 . Perform the operation on the segment S[1,2] with length 2 . S is now 011 . Perform the operation on the segment S[2,3] with length 2 . S is now 000 . Sample Input 2 100000000 Sample Output 2 8 Sample Input 3 00001111 Sample Output 3 4
37,225
Princess' Japanese お姫様の日本語 English text is not available in this practice contest. ある貧乏な囜のおおんばで勇敢なお姫様は日本語の勉匷をしおいる負けず嫌いな性栌も手䌝っおお姫様は日本語を流暢に話せるようになったが発音に少しばかり䞍自然なずころが残っおいるどうやら母音の無声化ずいう日本語固有の発音法が関係しおいるらしいお姫様は完璧な日本語話者を目指しおいるため䜕ずしおも自分の䞍自然な発音を矯正したいず思っおいるそこでお姫様は埓者であるあなたに察しお発音を矯正するためのプログラムを䜜成するように呜じたあなたの仕事は入力で䞎えられた日本語の語句に察しお無声化の生じる様子を瀺すプログラムを䜜成するこずである お姫様はひらがなカタカナ日垞的に䜿甚される挢字など日本語の衚蚘䜓系に぀いお十分に習埗しおいるため読み曞きに困るこずはめったにないずころがお姫様の囜にあるコンピュヌタでは日本語の入力に困難があるためプログラムにおいおは「ロヌマ字」ず呌ばれる英文字による衚蚘を甚いるこずに決めた たず日本語の発音䜓系に぀いお倧たかに説明する必芁があるだろう日本語では拍モヌラを単䜍ずしお発音がなされる拍は以䞋のいずれかの圢をしおいる 母音 子音+母音 「y」「w」以倖の子音+「y」+母音 促音「っ」 撥音「ん」 長音「ヌ」 ここで母音は「a」「i」「u」「e」「o」のいずれか子音は「k」「s」「t」「n」「h」「m」「y」「r」「w」「g」「z」「d」「b」「p」のいずれかであるただし「y」の盎埌に珟れる母音は「a」「u」「o」に限られるたた「w」の盎埌に珟れる母音は「a」に限られる 促音は盎埌の拍の子音を重ねお衚蚘されるたずえば「se k ki」石噚は促音を含む語句のひず぀である促音は母音あるいは「n」「y」「w」の前には珟れないたれに促音が文節末に珟れるこずはあるがここではそのような䟋は考えない 撥音は単独の「n」によっお衚蚘されるただし母音たたは「y」の盎前に珟れるずきは䞊蚘の2.たたは3.ずの区別が぀かなくなるため「n」の盎埌にアポストロフィヌを぀ける以䞋にいく぀かの䟋をしめす ta n go単語ni n gen人間ha n 'ei繁栄 特に以䞋の違いには泚意されたい zenin是認zen'in党員zennin前任 長音は単独で音節を構成するわけではないが垞に1拍を䜙分に䜿っおすなわち2拍で発音される日本語では長音ず短音の察立がありしかも長音ず短音の違いによっお語句が区別されるため長音ず短音の違いは重芁であるここでは長音は母音「a」「i」「u」のいずれかを重ねお衚蚘するこずずする以䞋にいく぀かの䟋をしめす ra a menラヌメンbi i ruビヌルkyu u syu u 九州kare i カレヌkyo u to京郜 撥音促音長音は先頭に珟れずたた連続しお珟れないたた撥音ず促音が互いに隣り合っお珟れるこずもない さお日本語では特定の条件䞋においお母音がきちんず発音されないこずがあるこれが母音の無声化ず呌ばれる珟象である母音が無声化される条件に関しおは諞説あるがここでは比范的簡単でか぀機械的適甚が可胜である以䞋のような堎合に぀いお考えるこずにするただし無声子音ずは「k」「s」「t」「h」「p」のこずであるたたこれらに「y」が続いたもの「ky」なども䟿宜的に無声子音ずみなす 母音「i」「u」の短音に぀いおその母音が無声子音にはさたれたずきたたは無声子音の盎埌でしかも語句の末尟にあるずきこの芏則は母音「i」「u」の盎埌の拍が促音であっおも適甚される以䞋にいく぀かの䟋を瀺す k u sa草hat i 蜂k i k u 菊k i tte切手k i pp u 切笊suup u スヌプ 母音「a」「o」に぀いお同䞀の母音が無声子音ずずもに連続する2぀以䞊の拍で珟れたずきただしその連続する拍のうちで最埌のものに぀いおは陀倖される以䞋にいく぀かの䟋を瀺す促音および長音はひず぀の拍を構成するこずから連続郚分の先頭たたは途䞭に促音たたは長音があっおはならないずいう点には泚意するこず h a ha母k o to琎h a k a ta博倚s a kana魚k a katoかかず ただし䞊蚘に該圓する党郚の母音が無声化されるわけではないこれは耇数の母音が続けお無声化されるこずはないこずによる母音が無声化されるのは最初の母音である堎合たたは盎近の母音が無声化されなかった堎合でありたたそれらの堎合にかぎるたずえば「h a k a ta」に぀いおは最初の「a」は無声化されるが2番目の「a」は無声化されない同様にしお「k i pp u 」に぀いおは語䞭の「i」は無声化されるが語尟の「u」は無声化されずに発音される 「hott o kouhii」ホットコヌヒヌはわかりにくい䟋のひず぀であるこれを拍に分解するず「ho-促音-to-ko-長音-hi-長音」のようになる最初の「o」は盎埌の拍が促音であるため䞊蚘の泚意曞きにあるように「無声子音 + o」が連続しおいるずはみなされないすなわち最初1拍目の「o」は無声化されないずころが次の「o」に぀いおは盎埌に長音があるものの「to」「ko」ずいうように「無声子音 + o」が連続しお珟れおいるしたがっお2番目3拍目の「o」は無声化されるたた語尟付近に無声子音「h」の埌に珟れる「i」が存圚するがこれは長音であるため無声化されない Input 入力は耇数のデヌタセットからなるそれぞれのデヌタセットは100文字以䞋の1぀の語句が含たれる1行だけからなるそれぞれの語句には英小文字ずアポストロフィヌだけが含たれるたたそれぞれの語句は問題文䞭で説明した拍だけから構成されるが暙準日本語の語句であるずは限らない 入力の終わりは「#」のみが含たれる行によっお衚されるこれはデヌタセットに含たれない Output それぞれのデヌタセットに぀いお䞎えられた語句を無声化される母音を括匧で囲んだうえで䞀行に出力しなさい Sample Input kusa haha hakata kippu sasakisan sosonokasu hottokouhii i # Output for the Sample Input k(u)sa h(a)ha h(a)kata k(i)ppu s(a)sak(i)san s(o)sonokas(u) hott(o)kouhii i
37,226
Score : 100 points Problem Statement Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7 : The sum of the digits in base ten is a multiple of D . Constraints All values in input are integers. 1 \leq K < 10^{10000} 1 \leq D \leq 100 Input Input is given from Standard Input in the following format: K D Output Print the number of integers satisfying the condition, modulo 10^9 + 7 . Sample Input 1 30 4 Sample Output 1 6 Those six integers are: 4, 8, 13, 17, 22 and 26 . Sample Input 2 1000000009 1 Sample Output 2 2 Be sure to print the number modulo 10^9 + 7 . Sample Input 3 98765432109876543210 58 Sample Output 3 635270834
37,227
Equation 恒等匏 English text is not available in this practice contest. 論理挔算では倀は T ず F の2皮類だけを扱う "-"を単項挔算子入力が 1 ぀の挔算を衚す蚘号 "*", "+", "->" を 2 項挔算子入力が 2 ぀の挔算を衚す蚘号ずする "-" は論理吊定(NOT) "*" は論理積(AND) "+" は論理和(OR) "->" は論理包含(IMP)を衚す挔算子である これらの論理挔算の真理倀衚を䞋の衚に瀺す x y -x (x*y) (x+y) (x->y) T T F T T T T F F F T F F T T F T T F F T F F T 論理匏は以䞋のいずれかの圢匏である X, Yは論理匏ずし 2 項挔算子は必ず括匧で囲むものずする 定数: T, F 倉数: a, b, c, d, e, f, g, h, i, j, k 論理吊定: -X 論理積: (X*Y) 論理和: (X+Y) 論理包含: (X->Y) 2 ぀の論理匏を等号 "=" で結合した等匏が䞎えられる 恒等匏ずは匏に珟れる倉数がどのような倀であっおも成立する等匏のこずである 䞎えられた等匏が恒等匏であるかを刀定するプログラムを䜜りたい Input 入力は耇数の行で構成され各行は 1 ぀のデヌタセットである デヌタセットはT, F, a, b, c, d, e, f, g, h, i, j, k, (, ), =, -, +, *, > から成る文字列であり 空癜など他の文字を含たない 1 行の文字数は 1000 文字以䞋ず仮定しおよい 1 ぀のデヌタセットは等匏ひず぀を含む 等匏の文法は次の BNF で䞎えられる すべおの等匏はこの構文芏則に埓う <equation> ::= <formula> "=" <formula> <formula> ::= "T" | "F" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "-" <formula> | "(" <formula> "*" <formula> ")" | "(" <formula> "+" <formula> ")" | "(" <formula> "->" <formula> ")" 入力の終わりは "#" だけからなる行で瀺されおおりこの行はデヌタセットではない Output 各デヌタセットに぀いお等匏が恒等匏であれば“YES”を そうでなければ“NO”をそれぞれ1行に出力しなさい 出力には䜙分な文字を含んではならない Sample Input -(a+b)=(-a*-b) (a->b)=(-a+b) ((a*T)+b)=(-c+(a*-b)) # Output for Sample Input YES YES NO
37,228
Problem G. Additions You are given an integer $N$ and a string consisting of ' + ' and digits. You are asked to transform the string into a valid formula whose calculation result is smaller than or equal to $N$ by modifying some characters. Here, you replace one character with another character any number of times, and the converted string should still consist of ' + ' and digits. Note that leading zeros and unary positive are prohibited. For instance, ' 0123+456 ' is assumed as invalid because leading zero is prohibited. Similarly, ' +1+2 ' and ' 2++3 ' are also invalid as they each contain a unary expression. On the other hand, ' 12345 ', ' 0+1+2 ' and ' 1234+0+0 ' are all valid. Your task is to find the minimum number of the replaced characters. If there is no way to make a valid formula smaller than or equal to $N$, output $-1$ instead of the number of the replaced characters. Input The input consists of a single test case in the following format. $N$ $S$ The first line contains an integer $N$, which is the upper limit of the formula ($1 \leq N \leq 10^9$). The second line contains a string $S$, which consists of ' + ' and digits and whose length is between $1$ and $1,000$, inclusive. Note that it is not guaranteed that initially $S$ is a valid formula. Output Output the minimized number of the replaced characters. If there is no way to replace, output $-1$ instead. Examples Sample Input 1 100 +123 Output for Sample Input 1 2 Sample Input 2 10 +123 Output for Sample Input 2 4 Sample Input 3 1 +123 Output for Sample Input 3 -1 Sample Input 4 10 ++1+ Output for Sample Input 4 2 Sample Input 5 2000 1234++7890 Output for Sample Input 5 2 In the first example, you can modify the first two characters and make a formula ' 1+23 ', for instance. In the second example, you should make ' 0+10 ' or ' 10+0 ' by replacing all the characters. In the third example, you cannot make any valid formula less than or equal to $1$.
37,230
投石おみくじ 「自分の道は自分で切り開く」をモットヌに、ある神瀟が自分自身の手で運勢を決めるおみくじを䜜りたした。そのおみくじを匕く人にはたず6぀の石を投げおもらい、その投げた石の䞀぀目ず二぀目を結ぶ 線分、䞉぀目ず四぀目を結ぶ 線分、五぀目ず六぀目を結ぶ 線分の3本の線分の亀点を頂点ずする 䞉角圢の面積から運勢を決めるずいうものです。各運勢ず䞉角圢の面積ずの関係は以䞋のずおりです。 線分の亀点を頂点ずする䞉角圢の面積 運勢 1,900,000以䞊 倧吉 (dai-kichi) 1,000,000以䞊1,900,000未満 䞭吉 (chu-kichi) 100,000以䞊1,000,000未満 吉 (kichi) 0より倧きく 100,000未満 小吉 (syo-kichi) 䞉角圢なし 凶 (kyo) しかし、䞉角圢の面積の倧きさの刀定は神䞻さんが手蚈算でやっおいるので正確ずはいえず、時間もかかっおしたいたす。そこで近所に䜏む優秀なプログラマであるあなたは、䞀刻でも早くプログラムを曞いお神䞻さんを助けおあげるこずにしたした。 3 本の線分の情報を入力ずし、線分の亀点を頂点ずする 䞉角圢の面積から運勢を出力するプログラムを䜜成しおください。線分の情報は始点の座暙 ( x 1 , y 1 ) ず、終点の座暙 ( x 2 , y 2 ) が䞎えられ、始点ず終点の座暙は必ず異なるこずずしたす。たた、2 ぀以䞊の線分が同䞀盎線䞊にある堎合、亀点を持たない 2 ぀の線分がある堎合、3 ぀の線分が 1 点で亀わる堎合は、「䞉角圢なし」ずなりたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロ4぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 line 1 line 2 line 3 i 行目に i 番目の線分の情報が䞎えられたす。各線分の情報は以䞋の圢匏で䞎えられたす。 x 1 y 1 x 2 y 2 線分の端点の座暙を衚す敎数 ( x 1 , y 1 ), ( x 2 , y 2 ) (-1000 ≀ x 1 , y 1 , x 2 , y 2 ≀ 1000 ) が空癜区切りで䞎えられたす。 Output デヌタセット毎に、おみくじの結果を行に出力したす。 Sample Input -3 -2 9 6 3 -2 7 6 -1 0 5 0 2 2 -1 -1 0 1 2 1 -3 -1 3 1 0 0 0 0 Output for the Sample Input syo-kichi kyo
37,231
Min-Max Element Write a program which manipulates a sequence $A = \{a_0, a_1, ..., a_{n-1}\}$ by the following operations: min($b, e$): report the minimum element in $a_b, a_{b+1}, ..., a_{e-1}$ max($b, e$): report the maximum element in $a_b, a_{b+1}, ..., a_{e-1}$ Input The input is given in the following format. $n$ $a_0 \; a_1, ..., \; a_{n-1}$ $q$ $com_1 \; b_1 \; e_1$ $com_2 \; b_2 \; e_2$ : $com_{q} \; b_{q} \; e_{q}$ In the first line, $n$ (the number of elements in $A$) is given. In the second line, $a_i$ (each element in $A$) are given. In the third line, the number of queries $q$ is given and each query is given in the following $q$ lines. $com_i$ denotes a type of query. 0 and 1 represents min($b, e$) and max($b, e$) respectively. Output For each query, print the minimum element or the maximum element in a line. Constraints $1 \leq n \leq 1,000$ $-1,000,000,000 \leq a_i \leq 1,000,000,000$ $1 \leq q \leq 1,000$ $0 \leq b < e \leq n$ Sample Input 1 7 8 3 7 1 9 1 4 3 0 0 3 0 1 5 1 0 7 Sample Output 1 3 1 9
37,232
Score : 300 points Problem Statement Snuke is making sugar water in a beaker. Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations. Operation 1: Pour 100A grams of water into the beaker. Operation 2: Pour 100B grams of water into the beaker. Operation 3: Put C grams of sugar into the beaker. Operation 4: Put D grams of sugar into the beaker. In our experimental environment, E grams of sugar can dissolve into 100 grams of water. Snuke will make sugar water with the highest possible density. The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker. Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it. If there is more than one candidate, any of them will be accepted. We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent. Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water. Constraints 1 \leq A < B \leq 30 1 \leq C < D \leq 30 1 \leq E \leq 100 100A \leq F \leq 3 000 A , B , C , D , E and F are all integers. Inputs Input is given from Standard Input in the following format: A B C D E F Outputs Print two integers separated by a space. The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it. Sample Input 1 1 2 10 20 15 200 Sample Output 1 110 10 In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances. We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once. It is not possible to make sugar water with higher density. For example, the following sequences of operations are infeasible: If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker. If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams. Sample Input 2 1 2 1 2 100 1000 Sample Output 2 200 100 There are other acceptable outputs, such as: 400 200 However, the output below is not acceptable: 300 150 This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible. Sample Input 3 17 19 22 26 55 2802 Sample Output 3 2634 934
37,233
Score : 500 points Problem Statement Given is a string S of length N . Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following: l_1 + len \leq l_2 S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1) If there is no such integer len , print 0 . Constraints 2 \leq N \leq 5 \times 10^3 |S| = N S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N S Output Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead. Sample Input 1 5 ababa Sample Output 1 2 The strings satisfying the conditions are: a , b , ab , and ba . The maximum length among them is 2 , which is the answer. Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2 . Sample Input 2 2 xy Sample Output 2 0 No non-empty string satisfies the conditions. Sample Input 3 13 strangeorange Sample Output 3 5
37,234
Problem G JAG-channel II JAG (Japan Alumni Group) is a group of $N$ members that devotes themselves to activation of the competitive programming world. The JAG staff members talk every day on the BBS called JAG-channel. There are several threads in JAG-channel and these are kept sorted by the time of their latest posts in descending order. One night, each of the $N$ members, identified by the first $N$ uppercase letters respectively, created a thread in JAG-channel. The next morning, each of the $N$ members posted in exactly $K$ different threads which had been created last night. Since they think speed is important, they viewed the threads from top to bottom and posted in the thread immediately whenever they came across an interesting thread. Each member viewed the threads in a different period of time, that is, there was no post of other members while he/she was submitting his/her $K$ posts. Your task is to estimate the order of the members with respect to the periods of time when members posted in the threads. Though you do not know the order of the threads created, you know the order of the posts of each member. Since the threads are always kept sorted, there may be invalid orders of the members such that some members cannot post in the top-to-bottom order of the threads due to the previous posts of other members. Find out the lexicographically smallest valid order of the members. Input The input consists of a single test case. The first line contains two integers separated by a space: $N$ $(4 \leq N \leq 16)$ and $K$ $(N - 3 \leq K \leq N - 1)$. Then $N$ lines of strings follow. Each of the $N$ lines consists of exactly $K$ distinct characters. The $j$-th character of the $i$-th line denotes the $j$-th thread in which the member denoted by the $i$-th uppercase letter posted. Each thread is represented by its creator (e.g. ' B ' represents the thread created by member B, the second member). It is guaranteed that at least one valid order exists. Output Display a string that consists of exactly $N$ characters in a line, which denotes the valid order in which the members posted in the threads. The $i$-th character of the output should represent the member who posted in the $i$-th period. In case there are multiple valid orders, output the lexicographically smallest one. Sample Input 1 7 4 DEFG FEDA EFGB BGEA AGFD DABC CADE Output for the Sample Input 1 ABCDEFG Sample Input 2 4 3 CDB DAC BAD ABC Output for the Sample Input 2 DCBA Sample Input 3 16 13 NDHPFJIBLMCGK CMDJKPOLGIHNE MOLBIEJFPHADN KPNAOHBLMCGEI FCMLBHDOANJPK NHIGLOAPKJDMC KMLBIPHDEOANJ IEGCMLBOAPKJD JNAOEDHBLMCGF OEDHPFIBLMGKC GMLBIFPHDNAEO ENHGOPKJDMCAF JKPAOBLGEIHNF HPKFGJEIBLCOM LBINEJDAGFKPH FGMOCADJENIBL Output for the Sample Input 3 PONCAKJGIEDHMFBL
37,235
Problem A: Membership Management Peter is a senior manager of Agile Change Management (ACM) Inc., where each employee is a member of one or more task groups. Since ACM is agile, task groups are often reorganized and their members frequently change, so membership management is his constant headache. Peter updates the membership information whenever any changes occur: for instance, the following line written by him means that Carol and Alice are the members of the Design Group. design:carol,alice. The name preceding the colon is the group name and the names following it specify its members. A smaller task group may be included in a larger one. So, a group name can appear as a member of another group, for instance, as follows. development:alice,bob,design,eve. Simply unfolding the design above gives the following membership specification, which is equivalent to the original. development:alice,bob,carol,alice,eve. In this case, however, alice occurs twice. After removing one of the duplicates, we have the following more concise specification. development:alice,bob,carol,eve. Your mission in this problem is to write a program that, given group specifications, identifies group members. Note that Peter's specifications can include deeply nested groups. In the following, for instance, the group one contains a single member dave. one:another. another:yetanother. yetanother:dave. Input The input is a sequence of datasets, each being in the following format. n group 1 : member 1,1 ,..., member 1, m 1 . . . . group i : member i ,1 ,..., member i , m i . . . . group n : member n ,1 ,..., member n , m n . The first line contains n, which represents the number of groups and is a positive integer no more than 100. Each of the following n lines contains the membership information of a group: group i (1 ≀ i ≀ n ) is the name of the i -th task group and is followed by a colon ( : ) and then the list of its m i member s that are delimited by a comma ( , ) and terminated by a period ( . ). Those group names are mutually different. Each m i (1 ≀ i ≀ n ) is between 1 and 10, inclusive. A member is another group name if it is one of group 1 , group 2 ,..., or groupn. Otherwise it is an employee name. There are no circular (or recursive) definitions of group(s). You may assume that m i member names of a group are mutually different. Each group or employee name is a non-empty character string of length between 1 and 15, inclusive, and consists of lowercase letters. The end of the input is indicated by a line containing a zero. Output For each dataset, output the number of employees included in the first group of the dataset, that is group 1 , in a line. No extra characters should occur in the output. Sample Input 2 development:alice,bob,design,eve. design:carol,alice. 3 one:another. another:yetanother. yetanother:dave. 3 friends:alice,bob,bestfriends,carol,fran,badcompany. bestfriends:eve,alice. badcompany:dave,carol. 5 a:b,c,d,e. b:c,d,e,f. c:d,e,f,g. d:e,f,g,h. e:f,g,h,i. 4 aa:bb. cc:dd,ee. ff:gg. bb:cc. 0 Output for the Sample Input 4 1 6 4 2
37,236
Score : 200 points Problem Statement Our world is one-dimensional, and ruled by two empires called Empire A and Empire B. The capital of Empire A is located at coordinate X , and that of Empire B is located at coordinate Y . One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control. If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out. X < Z \leq Y x_1, x_2, ..., x_N < Z y_1, y_2, ..., y_M \geq Z Determine if war will break out. Constraints All values in input are integers. 1 \leq N, M \leq 100 -100 \leq X < Y \leq 100 -100 \leq x_i, y_i \leq 100 x_1, x_2, ..., x_N \neq X x_i are all different. y_1, y_2, ..., y_M \neq Y y_i are all different. Input Input is given from Standard Input in the following format: N M X Y x_1 x_2 ... x_N y_1 y_2 ... y_M Output If war will break out, print War ; otherwise, print No War . Sample Input 1 3 2 10 20 8 15 13 16 22 Sample Output 1 No War The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement. X = 10 < 16 \leq 20 = Y 8, 15, 13 < 16 16, 22 \geq 16 Sample Input 2 4 2 -48 -1 -20 -35 -91 -23 -22 66 Sample Output 2 War Sample Input 3 5 3 6 8 -10 3 1 5 -100 100 6 14 Sample Output 3 War
37,237
Score : 900 points Problem Statement You are in charge of controlling a dam. The dam can store at most L liters of water. Initially, the dam is empty. Some amount of water flows into the dam every morning, and any amount of water may be discharged every night, but this amount needs to be set so that no water overflows the dam the next morning. It is known that v_i liters of water at t_i degrees Celsius will flow into the dam on the morning of the i -th day. You are wondering about the maximum possible temperature of water in the dam at noon of each day, under the condition that there needs to be exactly L liters of water in the dam at that time. For each i , find the maximum possible temperature of water in the dam at noon of the i -th day. Here, consider each maximization separately, that is, the amount of water discharged for the maximization of the temperature on the i -th day, may be different from the amount of water discharged for the maximization of the temperature on the j -th day (j≠i) . Also, assume that the temperature of water is not affected by anything but new water that flows into the dam. That is, when V_1 liters of water at T_1 degrees Celsius and V_2 liters of water at T_2 degrees Celsius are mixed together, they will become V_1+V_2 liters of water at \frac{T_1*V_1+T_2*V_2}{V_1+V_2} degrees Celsius, and the volume and temperature of water are not affected by any other factors. Constraints 1≀ N ≀ 5*10^5 1≀ L ≀ 10^9 0≀ t_i ≀ 10^9(1≀i≀N) 1≀ v_i ≀ L(1≀i≀N) v_1 = L L , each t_i and v_i are integers. Input Input is given from Standard Input in the following format: N L t_1 v_1 t_2 v_2 : t_N v_N Output Print N lines. The i -th line should contain the maximum temperature such that it is possible to store L liters of water at that temperature in the dam at noon of the i -th day. Each of these values is accepted if the absolute or relative error is at most 10^{-6} . Sample Input 1 3 10 10 10 20 5 4 3 Sample Output 1 10.0000000 15.0000000 13.2000000 On the first day, the temperature of water in the dam is always 10 degrees: the temperature of the only water that flows into the dam on the first day. 10 liters of water at 15 degrees of Celsius can be stored on the second day, by discharging 5 liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second day. 10 liters of water at 13.2 degrees of Celsius can be stored on the third day, by discharging 8 liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second and third days. Sample Input 2 4 15 0 15 2 5 3 6 4 4 Sample Output 2 0.0000000 0.6666667 1.8666667 2.9333333 Sample Input 3 4 15 1000000000 15 9 5 8 6 7 4 Sample Output 3 1000000000.0000000 666666669.6666666 400000005.0000000 293333338.8666667 Although the temperature of water may exceed 100 degrees Celsius, we assume that water does not vaporize.
37,238
H : Typing Game / タむピングゲヌム 物語 ここからはタむピング力を鍛える時間です皆さんは早く正確なタむピングをするこずができたすか Let’s enjoy Typing Game! 問題 タむピングゲヌムのゲヌムシステムずしおゞャッゞ偎が甚意したAIの出力に察しお察話的に動䜜するプログラムを䜜成せよ AIは N 個の文字列をタむプするゲヌムをプレむするAIは䜓力を持っおおりその初期倀は H であるAIは䜓力がある限り文字のタむプを行うシステムはAIがタむプすべき文字列を提瀺するシステムはAIがタむプした文字を受け取り次にタむプすべき文字ず䞀臎するか刀定するAIが文字列を 1 ぀タむプし終えるずシステムは次の文字列を提瀺する たた各文字列にはミスタむプの蚱容回数が定められおいるAIのある文字列におけるミスタむプの回数が蚱容回数を超えおしたうずAIの䜓力が 1 枛りシステムは匷制的に次の文字列を提瀺するAIの䜓力が 0 になったらゲヌムオヌバヌずなる最埌の文字列においおタむプの成功たたはミスタむプ回数の超過が起こった埌でAIの䜓力が残っおいればゲヌムクリアずなる 入出力圢匏 たず入力が以䞋の圢匏で䞎えられる N H S_1 T_1 ... S_N T_N 1行目に文字列の数 N ずAIの䜓力 H が空癜区切りで䞎えられる 続く N 行には i 行目に i 番目の文字列 S_i ずその文字列に察するミスタむプの蚱容回数 T_i が空癜区切りで䞎えられる この入力は以䞋の制玄を満たす 1 ≀ N ≀ 100 1 ≀ H ≀ 10 1 ≀ |S_i| ≀ 15 S_i はアルファベット'a'-'z'たたは'A'-'Z'のみを含む 0 ≀ T_i ≀ 20 システムは文字列 S_1 から文字列 S_N たで順番に凊理をする i 番目の文字列 S_i に぀いおAIが j 文字目たでタむプを成功させおいる堎合以䞋の凊理をする システムはAIに察しお S_i を出力するこのずき S_i の1文字目から j 文字目を"_" (アンダヌスコア) に眮き換えるシステムが間違った文字列を出力するずAIは動䜜を䞭断しWrong Answerずなるシステムが出力する文字列が S である堎合C/C++での出力䟋は以䞋のようになる printf("? %s\n", S); fflush(stdout); AIはシステムの出力を受け取るず c の圢匏で返答する c はAIがタむプした文字である c はアルファベット'a'-'z'からなるC/C++での返答の受け取り方の䟋は以䞋のようになる char c[2]; scanf("%s", c); /* char c; scanf("%c", &c); は空癜文字読み蟌みの仕様䞊の理由で非掚奚です */ システムはAIの出力を受け取るずたずAIがタむプした文字ず S_i の j+1 文字目が䞀臎しおいるか刀定するこの刀定の際倧文字ず小文字は区別しないこずに泚意せよ䞀臎しおいるずきは j+1 文字目たでタむプに成功したずするここで j+1 文字目が S_i の最埌の文字だった堎合 S_i のタむプに成功したずし S_i の凊理を終えるAIがタむプした文字ず S_i の j+1 文字目が䞀臎しおいなければ S_i に察するミスタむプ回数を1だけ増やすこのずき T_i を超えおいればAIの䜓力を 1 枛らし S_i の凊理を終える S_i の凊理を終えたずきAIの䜓力が 0 であればゲヌムオヌバヌずするゲヌムオヌバヌでなければ i が N のずきゲヌムクリアずしそうでなければ S_{i+1} の凊理に移る たたゲヌムオヌバヌの際には"! Game Over"をゲヌムクリアの際には"! Game Clear [タむプ成功率]"を出力せよタむプ成功率は (成功タむプ数) / (合蚈タむプ数) × 100 で蚈算される倀を小数点以䞋第2䜍で切り捚おた倀ずする小数点以䞋第1䜍が0であっおも小数点以䞋第1䜍たで出力せよC/C++での出力䟋は以䞋のようになる printf("! Game Over\n"); fflush(stdout); printf("! Game Clear %.1lf\n", success_type_rate); fflush(stdout); ※printfの曞匏を"%.1lf"ずした堎合出力される浮動小数点数は小数点以䞋第2䜍が四捚五入されるこのためタむプ成功率を浮動小数点数ずしお蚈算し䞊蚘の手法で出力した堎合正しい出力結果が埗られないこずがあるこずに泚意せよ 入出力䟋1 システムの出力 システムぞの入力・AIの返答 2 5 ICPC 2 Tsurai 5 ? ICPC i ? _CPC g ? _CPC p ? _CPC c ? __PC p ? ___C c ? Tsurai t ? _surai s ? __urai u ? ___rai r ? ____ai a ? _____i i ! Game Clear 83.3 入出力䟋2 システムの出力 システムぞの入力・AIの返答 2 1 ICPC 1 Tsurai 1 ? ICPC i ? _CPC c ? __PC o ? __PC p ? ___C d ! Game Over
37,239
Score: 500 points Problem Statement N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals. The preliminary stage consists of several rounds, which will take place as follows: All the N contestants will participate in the first round. When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows: The organizer will choose two consecutive digits in the decimal notation of X , and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round. For example, when X = 2378 , the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen). When X = 100 , the number of contestants advancing to the next round will be 10 , no matter which two digits are chosen. The preliminary stage ends when 9 or fewer contestants remain. Ringo, the chief organizer, wants to hold as many rounds as possible. Find the maximum possible number of rounds in the preliminary stage. Since the number of contestants, N , can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M , which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1 , the following c_2 digits are all d_2 , \ldots , and the last c_M digits are all d_M . Constraints 1 \leq M \leq 200000 0 \leq d_i \leq 9 d_1 \neq 0 d_i \neq d_{i+1} c_i \geq 1 2 \leq c_1 + \ldots + c_M \leq 10^{15} Input Input is given from Standard Input in the following format: M d_1 c_1 d_2 c_2 : d_M c_M Output Print the maximum possible number of rounds in the preliminary stage. Sample Input 1 2 2 2 9 1 Sample Output 1 3 In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows: 229 contestants participate in Round 1 , 49 contestants participate in Round 2 , 13 contestants participate in Round 3 , and 4 contestants advance to the finals. Here, three rounds take place in the preliminary stage, which is the maximum possible number. Sample Input 2 3 1 1 0 8 7 1 Sample Output 2 9 In this case, 1000000007 will participate in the first round.
37,240
Problem A: Strange String Manipulation A linear congruential generator produces a series R(⋅) of pseudo-random numbers by the following for- mulas: where S , A , C , and M are all parameters. In this problem, 0 ≀ S , A , C ≀ 15 and M = 256. Now suppose we have some input string I (⋅), where each character in the string is an integer between 0 and ( M - 1). Then, using the pseudo-random number series R (⋅), we obtain another string O (⋅) as the output by the following formula: Your task is to write a program that shows the parameters S , A , and C such that the information entropy of the output string O (⋅) is minimized. Here, the information entropy H is given by the following formula: where N is the length of the string and #( x ) is the number of occurences of the alphabet x . Input The input consists of multiple datasets. Each dataset has the following format: N I (1) I (2) ... I ( N ) N does not exceed 256. 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 in a line the values of the three parameters S , A , and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S , A , and then C . Sample Input 5 5 4 3 2 1 5 7 7 7 7 7 10 186 8 42 24 154 40 10 56 122 72 0 Output for the Sample Input 0 1 1 0 0 0 8 7 14
37,241
Score : 1100 points Problem Statement You are given integers N, K , and an integer sequence A of length M . An integer sequence where each element is between 1 and K (inclusive) is said to be colorful when there exists a contiguous subsequence of length K of the sequence that contains one occurrence of each integer between 1 and K (inclusive). For every colorful integer sequence of length N , count the number of the contiguous subsequences of that sequence which coincide with A , then find the sum of all the counts. Here, the answer can be extremely large, so find the sum modulo 10^9+7 . Constraints 1 \leq N \leq 25000 1 \leq K \leq 400 1 \leq M \leq N 1 \leq A_i \leq K All values in input are integers. Input Input is given from Standard Input in the following format: N K M A_1 A_2 ... A_M Output For every colorful integer sequence of length N , count the number of the contiguous subsequences of that sequence which coincide with A , then print the sum of all the counts modulo 10^9+7 . Sample Input 1 3 2 1 1 Sample Output 1 9 There are six colorful sequences of length 3 : (1,1,2) , (1,2,1) , (1,2,2) , (2,1,1) , (2,1,2) and (2,2,1) . The numbers of the contiguous subsequences of these sequences that coincide with A=(1) are 2 , 2 , 1 , 2 , 1 and 1 , respectively. Thus, the answer is their sum, 9 . Sample Input 2 4 2 2 1 2 Sample Output 2 12 Sample Input 3 7 4 5 1 2 3 1 2 Sample Output 3 17 Sample Input 4 5 4 3 1 1 1 Sample Output 4 0 Sample Input 5 10 3 5 1 1 2 3 3 Sample Output 5 1458 Sample Input 6 25000 400 4 3 7 31 127 Sample Output 6 923966268 Sample Input 7 9954 310 12 267 193 278 294 6 63 86 166 157 193 168 43 Sample Output 7 979180369
37,242
Problem I: 盗たれた宝石 パル王囜の囜宝である宝石が盗賊たちに盗たれた。 冒険者のあなたはその噂を聞き぀けお盗賊のアゞトに向かい、なんずか宝石を取り戻すこずが出来た。 ずころが、宝石をパル王囜の城ぞ返しに出向いたずころ、城の守衛兵から、 「我が王はただあなたを完党に信甚しおいない。その宝石が本物かどうか怪しいし、そもそも宝石を取り戻したずいうのがり゜で、王の呜を狙う盗賊の仲間なのではないかず疑っおおられる。」 ずいわれた。 あなたは、守衛兵から本圓に信頌できる人物かどうか詊すための詊緎を䞎えられた。詊緎の内容は、 「城の地䞋倉庫ぞ行き指定された堎所に宝石を眮け。そこに宝石が本物だったずきに反応する魔法陣が曞かれおある。ただしあなたが怪しい行動を起こさないようにするため、地䞋倉庫の入り口から指定された堎所に向かうたで、守衛兵が蚀った移動パタヌンをずっおはならない。」 ずいうものだった。 䟋えば、䞋図にある地䞋倉庫の圢状、犁止パタヌンに察し、図にかかれおあるような移動パタヌンを取っおはならない。移動パタヌンの䞀郚(赀くハむラむトされおいる)が、犁止パタヌンの䞭に含たれるからである。(たた、この移動パタヌンの䞭の2,3番目の動き「↓↓」も犁止パタヌンの䞭に含たれる) 䞀方、 䞋図のような移動パタヌンは、移動パタヌンのどの郚分も犁止パタヌンの䞭に含たれないので蚱される。 入力ずしお、地䞋倉庫の圢状、犁止パタヌンが䞎えられる。犁止パタヌンを取らずに、入り口から魔法陣ぞ移動するのに最䜎限必芁な移動回数を求めよ。 Input 入力には、地䞋倉庫の圢状ず犁止パタヌンが含たれる。 地䞋倉庫の圢状は、以䞋のように衚される。 たず、二぀の敎数 N,M が䞎えられる。それぞれ地䞋倉庫の行数ず列数を意味する。(1 ≀ N,M ≀ 50) 続いお、それぞれM個の文字からなる文字列がN行䞎えられる。 含たれる文字ずその意味は以䞋の通りである。 文字 意味 S 地䞋倉庫の入り口。䞀぀の地䞋倉庫に぀き必ず䞀぀だけ含たれる。 G 魔法陣。䞀぀の地䞋倉庫に぀き必ず䞀぀だけ含たれる。 . 通路。(犁止パタヌンをずらなければ)通るこずが出来る。 # 壁。壁の䞭を通るこずはできない。 次に、犁止パタヌンが䞎えられる。犁止パタヌンは以䞋のように衚される。 たず、䞀぀の敎数 P が䞎えられる。犁止パタヌンの数を意味する。(0 ≀ P ≀ 10) 続いお、犁止パタヌンを意味する文字列がP行にわたっお䞎えられる。 犁止パタヌンに含たれる文字ずその意味は以䞋の通りである。 文字 意味 U ↑の移動。 R →の移動。 D ↓の移動。 L ←の移動。 犁止パタヌンの長さは1以䞊、10以䞋である。 ある犁止パタヌンが、別の犁止パタヌンの郚分文字列ずなっおいるこずもある。たた、同䞀の犁止パタヌンが含たれるこずもある。 Output 最䜎限必芁な移動回数を意味する敎数を出力せよ。魔法陣ぞ蟿り぀けない堎合は -1 を出力せよ。 Notes on Test Cases 䞊蚘入力圢匏で耇数のデヌタセットが䞎えられたす。各デヌタセットに察しお䞊蚘出力圢匏で出力を行うプログラムを䜜成しお䞋さい。 n, m が 0 のずき入力の終わりを瀺したす。 Sample Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output for Sample Input 13 60 7 10 -1
37,243
芳音堂 䞀郎君の家の裏山には芳音堂がありたす。この芳音堂たではふもずから 30 段の階段があり、䞀郎君は、毎日のように芳音堂たで遊びに行きたす。䞀郎君は階段を足で段たで䞊がるこずができたす。遊んでいるうちに階段の䞊り方の皮類段の飛ばし方の個数が非垞にたくさんあるこずに気が぀きたした。 そこで、䞀日に 10 皮類の䞊り方をし、すべおの䞊り方を詊そうず考えたした。しかし数孊を熟知しおいるあなたはそんなこずでは䞀郎君の寿呜が尜きおしたうこずを知っおいるはずです。 䞀郎君の蚈画が実珟䞍可胜であるこずを䞀郎君に玍埗させるために、階段の段数 n を入力ずし、䞀日に 10 皮類の䞊り方をするずしお、䞀郎君がすべおの䞊り方を実行するのに芁する幎数を出力するプログラムを䜜成しおください。䞀幎は 365 日ずしお蚈算しおください。䞀日でも必芁なら䞀幎ずしたす。365 日なら 1 幎であり、366 日なら 2 幎ずなりたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットずしお、段数を衚す぀の敎数 n (1 ≀ n ≀ 30) が行に䞎えられたす。 デヌタセットの数は 30 を超えたせん。 Output デヌタセットごずに䞀郎君がすべおの䞊り方を実行するのに必芁な幎数敎数を行に出力したす。 Sample Input 1 10 20 25 0 Output for the Sample Input 1 1 34 701
37,244
Problem G: First Kiss Problem ビット君の通う男子校では昌䌑みにキッポヌゲヌムが行われおいる。 この孊校のキッポヌゲヌムは少し倉わっおいるこずで有名だ。 たず、$N$本のキッポヌを甚意する。$i$本目のキッポヌの長さは$a_i$である。 先手ず埌手が亀互にキッポヌを食べる。 すごい長さのキッポヌを䞀気に食べるず喉に刺さっおしたうので、$1$以䞊$D$以䞋の奜きな長さだけ食べる。 圌らは䞍噚甚なので、敎数長でのみキッポヌを食べるこずができる。 最初にいずれかのキッポヌが食べ぀くされたずき、食べきった人の負けずなる。 䞡者が最適に行動したずき、先手が勝぀なら"First"、埌手が勝぀なら"Second"を出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 $N$ $D$ $a_1$ $a_2$ $...$ $a_N$ 入力はすべお敎数で䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \le N \le 3\times 10^5$ $1 \le D \le 10^9$ $1 \le a_i \le 10^9$ Output 䞡者が最適に行動したずき、どちらが勝぀か出力せよ。 先手が勝぀なら"First"、埌手が勝぀なら"Second"を出力せよ。 Sample Input 1 1 1 2 Sample Output 1 First Sample Input 2 2 25 19 19 Sample Output 2 Second Sample Input 3 3 5 10 8 4 Sample Output 3 First Sample Input 4 4 10 3 8 5 2 Sample Output 4 Second
37,245
むルミネヌション (Illumination) 問題 JOI 瀟の建物は図のような 1 蟺 1 メヌトルの正六角圢を぀なぎ合わせた圢であるクリスマスが近づいおいるのでJOI 瀟では建物の壁面をむルミネヌションで食り付けるこずにしたただし倖から芋えない郚分にむルミネヌションを斜すのは無駄なのでむルミネヌションは倖から建物の䞭を通らずに行くこずのできる壁面にのみ食り付けるこずにした 図: JOI 瀟の建物の配眮の䟋 䞊の図は䞊空から芋た JOI 瀟の建物の配眮の䟋である正六角圢内の数字は座暙を衚す灰色の正六角圢は建物がある堎所を衚し癜色の正六角圢は建物がない堎所を衚すこの䟋では赀の実線で瀺される郚分がむルミネヌションで食り付けを行う壁面ずなりその壁面の長さの合蚈は 64 メヌトルずなる JOI 瀟の建物の配眮を衚す地図が䞎えられたずき食り付けを行う壁面の長さの合蚈を求めるプログラムを䜜成せよただし地図の倖偎は自由に行き来できるものずし隣接した建物の間は通るこずはできないものずする 入力 入力ファむルの 1 行目には 2 ぀の敎数 W, H (1 ≩ W ≩ 1001 ≩ H ≩ 100) が空癜を区切りずしお曞かれおいる続く H 行には JOI 瀟の建物の配眮が曞かれおいるi + 1 行目 (1 ≩ i ≩ H) には W 個の敎数が空癜を区切りずしお曞かれおおりj 個目 (1 ≩ j ≩ W) の敎数は座暙 (j, i) の正六角圢に建物がある時は 1 でありない時は 0 であるたた䞎えられる入力デヌタには建物が必ず 1 ぀以䞊ある 地図は以䞋の芏則によっお蚘述されおいる 地図の最も北の行の最も西の正六角圢は座暙 (1, 1) である 座暙 (x, y) の正六角圢に隣接する東隣の正六角圢は座暙 (x + 1, y) である y が奇数の時座暙 (x, y) の正六角圢に隣接する南西の正六角圢の座暙は (x, y + 1) である y が偶数の時座暙 (x, y) の正六角圢に隣接する南東の正六角圢の座暙は (x, y + 1) である 出力 むルミネヌションで食り付けを行う壁面の長さの合蚈を 1 行で出力せよ 入出力䟋 入力䟋 1 8 4 0 1 0 1 0 1 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 1 1 1 0 1 1 0 1 0 1 0 出力䟋 1 64 入出力䟋 1 は問題文䞭の䟋に察応しおおりむルミネヌションで食り付けを行う壁面の長さの合蚈は 64 メヌトルである 入力䟋 2 8 5 0 1 1 1 0 1 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1 1 1 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 0 0 出力䟋 2 56 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,246
ケヌキの切り分け (Cake 2) JOI くんず IOI ちゃんは双子の兄効であるJOI くんは最近お菓子䜜りに凝っおいお今日も JOI くんはケヌキを焌いお食べようずしたのだが焌きあがったずころで匂いをかぎ぀けた IOI ちゃんが来たので 2人でケヌキを分けるこずになった ケヌキは円圢であるある点から攟射状に切り目を入れケヌキを N 個のピヌスに切り分けピヌスに 1 から N たで反時蚈回りに番号を぀けた぀たり1 ≀ i ≀ N に察し i 番目のピヌスは i − 1 番目ず i + 1 番目のピヌスず隣接しおいる (ただし 0 番目は N 番目 N + 1 番目は 1 番目ずみなす)  i 番目のピヌスの倧きさは A i だったが切り方がずおも䞋手だったので A i はすべお異なる倀になった 図 1: ケヌキの䟋 ( N = 5, A 1 = 2, A 2 = 8, A 3 = 1, A 4 = 10, A 5 = 9) この N 個を JOI くんず IOI ちゃんで分けるこずにした分け方は次のようにするこずにした たず JOI くんが N 個のうちの奜きな 1 ぀を遞んで取る その埌IOI ちゃんからはじめお IOI ちゃんず JOI くんが亀互に残りのピヌスを 1 ぀ず぀取っおいくただし䞡隣のピヌスのうち少なくずも䞀方が既に取られおいるようなピヌスしか取るこずができず取れるピヌスが耇数あるずきはIOI ちゃんはそのうち最も倧きいものを遞んで取りJOI くんはそのうちで奜きなものを遞んで取るこずができる JOI くんは自分が最終的に取るピヌスの倧きさの合蚈を最倧化したい 課題 ケヌキのピヌスの数 N ず N 個のピヌスの倧きさの情報が䞎えられたずきJOI くんが取れるピヌスの倧きさの合蚈の最倧倀を求めるプログラムを䜜成せよ 入力 暙準入力から以䞋の入力を読み蟌め 1 行目には敎数 N が曞かれおおりケヌキが N 個のピヌスに切り分けられおいるこずを衚す 続く N 行のうちの i 行目 (1 ≀ i ≀ N ) には敎数 A i が曞かれおおり i 番目のピヌスの倧きさが A i であるこずを衚す 出力 暙準出力にJOI くんが取れるピヌスの倧きさの合蚈の最倧倀を衚す敎数を 1 行で出力せよ. 制限 すべおの入力デヌタは以䞋の条件を満たす 1 ≀ N ≀ 2 000 1 ≀ A i ≀ 1 000 000 000 A i はすべお異なる 入出力䟋 入力䟋 1 5 2 8 1 10 9 出力䟋 1 18 JOI くんは次のようにピヌスを取るのが最適である JOI くんは 2 番目のピヌスを取るこのピヌスの倧きさは 8 である IOI ちゃんは 1 番目のピヌスを取るこのピヌスの倧きさは 2 である JOI くんは 5 番目のピヌスを取るこのピヌスの倧きさは 9 である IOI ちゃんは 4 番目のピヌスを取るこのピヌスの倧きさは 10 である JOI くんは 3 番目のピヌスを取るこのピヌスの倧きさは 1 である 最終的にJOI くんが取ったピヌスの倧きさの合蚈は8 + 9 + 1 = 18 ずなる. 入力䟋 2 8 1 10 4 5 6 2 9 3 出力䟋 2 26 入力䟋 3 15 182243672 10074562 977552215 122668426 685444213 3784162 463324752 560071245 134465220 21447865 654556327 183481051 20041805 405079805 564327789 出力䟋 3 3600242976 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,247
Score : 1200 points Problem Statement We have N lamps numbered 1 to N , and N buttons numbered 1 to N . Initially, Lamp 1, 2, \cdots, A are on, and the other lamps are off. Snuke and Ringo will play the following game. First, Ringo generates a permutation (p_1,p_2,\cdots,p_N) of (1,2,\cdots,N) . The permutation is chosen from all N! possible permutations with equal probability, without being informed to Snuke. Then, Snuke does the following operation any number of times he likes: Choose a lamp that is on at the moment. (The operation cannot be done if there is no such lamp.) Let Lamp i be the chosen lamp. Press Button i , which switches the state of Lamp p_i . That is, Lamp p_i will be turned off if it is on, and vice versa. At every moment, Snuke knows which lamps are on. Snuke wins if all the lamps are on, and he will surrender when it turns out that he cannot win. What is the probability of winning when Snuke plays optimally? Let w be the probability of winning. Then, w \times N! will be an integer. Compute w \times N! modulo (10^9+7) . Constraints 2 \leq N \leq 10^7 1 \leq A \leq \min(N-1,5000) Input Input is given from Standard Input in the following format: N A Output Print w \times N! modulo (10^9+7) , where w is the probability of Snuke's winning. Sample Input 1 3 1 Sample Output 1 2 First, Snuke will press Button 1 . If Lamp 1 turns off, he loses. Otherwise, he will press the button that he can now press. If the remaining lamp turns on, he wins; if Lamp 1 turns off, he loses. The probability of winning in this game is 1/3 , so we should print (1/3)\times 3!=2 . Sample Input 2 3 2 Sample Output 2 3 Sample Input 3 8 4 Sample Output 3 16776 Sample Input 4 9999999 4999 Sample Output 4 90395416
37,248
Problem A: K Cards ある日、先生は次のようなゲヌムを思い぀いた。 ゲヌムは 1 から 10 たでの数がひず぀曞かれたカヌドを n 枚䜿甚し、以䞋のように進む。 先生が n 枚のカヌドを数が芋えるようにしお暪䞀列に黒板に貌り付け、ある敎数 k (k ≥ 1) を生埒に宣蚀する。暪䞀列に䞊べられた n 枚のカヌドに぀いお、連続した k 枚のカヌドの積の最倧倀を C k ずする。たた、先生が䞊べた時点での C k を C k ' ずおく。 生埒は 1. で貌られたカヌドの列を芋お C k を倧きくするこずを考える。ある 2 枚を入れ替えるこずで C k をより倧きくするこずができた堎合、生埒の成瞟は C k - C k ' 点䞊がる。誰かが成瞟点を埗たらゲヌムを終了する。 あなたの仕事は先生が䞊べたカヌドの列を入力し、生埒が埗られる最倧の成瞟点を出力するプログラムを曞くこずである。ただし、そこからどの 2 枚を遞んで亀換しおもC k を䞋げるこずしかできない (C k - C k ' < 0) 堎合、文字列 "NO GAME" (匕甚笊を含たない)を出力せよ。 先生が䞊べたカヌドが7, 2, 3, 5の堎合。このずき 7 ず 3 を亀換するこずで生埒は最倧で 35 - 15 = 20 成瞟点を埗る。 Input 入力は耇数のテストケヌスからなる。ひず぀のテストケヌスは以䞋の圢匏に埓う。 n k c 1 c 2 c 3 
 c n n は先生が䞊べるカヌドの枚数、k は宣蚀する敎数である。 たた c i (1 ≀ i ≀ n) はカヌドに曞かれた数を瀺す。たた、この順で先生が暪に黒板に貌り付けるずする。入力の終わりは、ふた぀の0が䞀文字の空癜で区切られる䞀行で瀺される。 Constraints 入力はすべお敎数 2 ≀n ≀ 100 1 ≀k ≀ 5 k ≀ n 1 ≀ c i ≀ 10 (1 ≀ i ≀ n) テストケヌスの数は 100 を超えない。 Output 生埒が埗られる成瞟点の最倧倀あるいは文字列 "NO GAME" (匕甚笊を含たない)を各テストケヌスに付き 1 行で出力せよ。 Sample Input 4 2 2 3 7 5 0 0 Sample Output 0 Hint サンプルにおいお C 2 ' = 35 であり、ここからどの 2 枚を䞊び替えおも C 2 の最倧倀は 35 より倧きくならない。したがっお生埒が埗られる成瞟点は最倧 0 点である。
37,249
Beautiful Sequence Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists only of 0 and 1 if it includes $M$ successive array of 1s as its sub-sequence. Using his skills in programming, Alice decided to calculate how many "Beautiful sequences" she can generate and compile a report on it. Make a program to evaluate the possible number of "Beautiful sequences" given the sequence length $N$ and sub-sequence length $M$ that consists solely of 1. As the answer can be extremely large, divide it by $1,000,000,007 (= 10^9 + 7)$ and output the remainder. Input The input is given in the following format. $N$ $M$ The input line provides the length of sequence $N$ ($1 \leq N \leq 10^5$) and the length $M$ ($1 \leq M \leq N$) of the array that solely consists of 1s. Output Output the number of Beautiful sequences in a line. Sample Input 1 4 3 Sample Output 1 3 The sequences with length 4 that include 1s in successive array of length 3 are: 0111, 1110 and 1111. Sample Input 2 4 2 Sample Output 2 8 The sequences with length 4 that include 1s in successive array of length 2 are: 0011, 0110, 0111, 1011, 1100, 1101, 1110 and 1111.
37,251
コンテストTシャツ (Contest T-shirts) Segtree 君は、 $M$ 枚のコンテストTシャツを持っおいたす。 圌は今から $N$ 日間、コンテストTシャツだけで過ごそうず考え、$i = 1, 2, 3, \dots, N$ に察しお「 $i$ 日目に $A_i$ 枚目のTシャツを着る」ずいう $N$ 個の蚈画を立おたした。 しかし、今の蚈画のたただず掗濯が間に合わない可胜性があるので、必芁に応じお蚈画を倉曎し、2日連続で同じ服を着ないようにしたいです。 倉曎する必芁のある蚈画の個数の最小倀を求めおください。なお、䞎えられた制玄の元で、蚈画の倉曎によっお必ず条件を満たすようにできるこずが蚌明できたす。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $M$ $N$ $A_1$ $A_2$ $\ldots$ $A_N$ 出力 倉曎する必芁のある蚈画の個数の最小倀を出力しおください。 ただし、最埌には改行を入れるこず。 制玄 $2 \leq M \leq 10^9$ $1 \leq N \leq 10^5$ $1 \leq A_i \leq M$ 入力は党お敎数である。 入力䟋1 2 3 2 2 1 出力䟋1 1 入力䟋2 3 6 1 1 1 2 2 3 出力䟋2 2
37,252
カロリヌ蚈算 食べ物には3倧栄逊玠ず呌ばれる「たんぱく質」「脂質」「炭氎化物」の぀の栄逊玠が含たれおいたす。gグラムあたりたんぱく質ず炭氎化物は 4 kcalキロカロリヌ、脂質は 9 kcal ず蚈算されたす。たずえば、䞋の衚によるず番号のケヌキにはタンパク質 7 g、脂質 14 g、炭氎化物 47 g が含たれおいたす。これをもずに含たれるカロリヌを蚈算するず、4 × 7 + 9 × 14 + 4 × 47 = 342 kcal ずなりたす。その他も同様に蚈算されおいたす。 番号 名前 たんぱく質(g) 脂質(g) 炭氎化物(g) カロリヌ(kcal) 1 ケヌキ 7 14 47 342 2 ポテトチップス 5 35 55 555 3 どら焌き 6 3 59 287 4 プリン 6 5 15 129 分類の察象ずなるお菓子の個数 n ず各お菓子の情報、制限の情報を入力ずし、そのお菓子䞀぀だけなら制限を超えるこずがない食べおもよいお菓子の䞀芧を出力するプログラムを䜜成しおください。 お菓子の情報は、お菓子の番号 s 、そのお菓子に含たれるたんぱく質の重量 p 、脂質の重量 q 、炭氎化物の重量 r から構成されおいたす。制限の情報には、含むこずができる最倧のたんぱく質の重量 P 、脂質の重量 Q 、炭氎化物の重量 R 、および摂取するこずができる最倧のカロリヌ C からなっおおり、タンパク質、脂質、炭氎化物、カロリヌのいずれかの䞀぀でも超えるず制限違反ずなり、「食べおはいけないお菓子」ず刀断されたす。 食べおもよいお菓子の䞀芧は、食べおもよいお菓子の番号を入力順に出力しおください。なお、食べおもよいお菓子がなければ”NA”ず出力しおください。 䞊の衚にある4個のお菓子に぀いお、 P = 10, Q = 15, R = 50, C = 400 ずいう制限の堎合、ケヌキずプリンはそれぞれの栄逊玠及びカロリヌが制限倀以䞋なので食べおもよいお菓子に分類されたすが、ポテトチップスは炭氎化物ずカロリヌ、どら焌きは炭氎化物の量が制限倀を超えおいるため食べおはいけないお菓子に分類されたす。 入力 耇数のデヌタセットの䞊びが䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 n s 1 p 1 q 1 r 1 s 2 p 2 q 2 r 2 : s n p n q n r n P Q R C 1行目にお菓子の数 n (1 ≀ n ≀ 1000)が䞎えられたす。続く n 行に第 i のお菓子の番号 s i (1 ≀ s i ≀ 1000)、各栄逊玠の重量を衚す敎数 p i , q i , r i (0 ≀ p i , q i , r i ≀ 100) が䞎えられたす。 続く行に各栄逊玠及びカロリヌの制限倀を衚す敎数 P , Q , R (0 ≀ P , Q , R ≀ 100), C (0 ≀ C ≀ 1700) が䞎えられたす。 デヌタセットの数は 100 を超えたせん。 出力 デヌタセットごずに、食べおもよいお菓子の番号たたは ”NA” を出力したす。 入力䟋 4 1 7 14 47 2 5 35 55 3 6 3 59 4 6 5 15 10 15 50 400 2 1 8 10 78 2 4 18 33 10 10 50 300 0 出力䟋 1 4 NA
37,253
Problem G: Star Problem 半埄1の円に内接する正 N / K 角圢の面積を求めよ。 ただし、正 N / K 角圢を 「円呚䞊に等間隔に N 個の点を取り、 K -1個おきにそれぞれの点を結んだ䞀番倖偎の図圢」 ず定矩する。 䟋えば、5/2角圢は次のように描くこずができる。 たず、半埄1の円呚䞊に等間隔に5぀の点を取る。 次に、それぞれの点を2-1=1぀おきに結ぶ。 䞀番倖偎の図圢が正5/2角圢になる。 Input 入力は以䞋の圢匏で䞎えられる。 N K 2぀の敎数 N , K が1行に䞎えられる。 Constraints 入力は以䞋の制玄を満たす。 5 ≀ N ≀ 10 6 1 < K < N /2 N , K は互いに玠である敎数 Output 半埄1の円に内接する正 N / K 角圢の面積を1行に出力せよ。10 -5 以䞋の誀差が蚱容される。 Sample Input 1 5 2 Sample Output 1 1.12256994 æ­£5/2角圢は䞊蚘の図圢である。 Sample Input 2 20 3 Sample Output 2 2.93114293 æ­£20/3角圢は䞋のような図圢である。 Sample Input 3 7 3 Sample Output 3 1.08395920 Sample Input 4 100000 3 Sample Output 4 3.14159265
37,254
Score : 500 points Problem Statement There are N cards placed face down in a row. On each card, an integer 1 or 2 is written. Let A_i be the integer written on the i -th card. Your objective is to guess A_1, A_2, ..., A_N correctly. You know the following facts: For each i = 1, 2, ..., M , the value A_{X_i} + A_{Y_i} + Z_i is an even number. You are a magician and can use the following magic any number of times: Magic : Choose one card and know the integer A_i written on it. The cost of using this magic is 1 . What is the minimum cost required to determine all of A_1, A_2, ..., A_N ? It is guaranteed that there is no contradiction in given input. Constraints All values in input are integers. 2 \leq N \leq 10^5 1 \leq M \leq 10^5 1 \leq X_i < Y_i \leq N 1 \leq Z_i \leq 100 The pairs (X_i, Y_i) are distinct. There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.) Input Input is given from Standard Input in the following format: N M X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_M Y_M Z_M Output Print the minimum total cost required to determine all of A_1, A_2, ..., A_N . Sample Input 1 3 1 1 2 1 Sample Output 1 2 You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards. Sample Input 2 6 5 1 2 1 2 3 2 1 3 3 4 5 4 5 6 5 Sample Output 2 2 Sample Input 3 100000 1 1 100000 100 Sample Output 3 99999
37,255
Problem B: Cover Time Let G be a connected undirected graph where N vertices of G are labeled by numbers from 1 to N . G is simple, i.e. G has no self loops or parallel edges. Let P be a particle walking on vertices of G . At the beginning, P is on the vertex 1. In each step, P moves to one of the adjacent vertices. When there are multiple adjacent vertices, each is selected in the same probability. The cover time is the expected number of steps necessary for P to visit all the vertices. Your task is to calculate the cover time for each given graph G. Input The input has the following format. N M a 1 b 1 . . . a M b M N is the number of vertices and M is the number of edges. You can assume that 2 ≀ N ≀ 10. a i and b i (1 ≀ i ≀ M ) are positive integers less than or equal to N , which represent the two vertices connected by the i -th edge. You can assume that the input satisfies the constraints written in the problem description, that is, the given graph G is connected and simple. Output There should be one line containing the cover time in the output. The answer should be printed with six digits after the decimal point, and should not have an error greater than 10 -6 . Sample Input and Output Input #1 3 2 1 2 2 3 Output #1 4.000000 Input #2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output #2 5.500000
37,256
問題文 お菓子の魔女 CHARLOTTE は 巎マミ ずクッキヌゲヌムを楜しんでいるクッキヌゲヌムは 8\times 8 の栌子状に区切られたテヌブルクロスの䞊にチヌズクッキヌずチョコレヌトクッキヌを眮いお行われる各栌子には高々 1 個のチョコレヌトクッキヌたたはチヌズクッキヌしか眮くこずはできない お菓子の魔女はチヌズクッキヌを 巎マミ はチョコレヌトクッキヌを亀互に眮いおゲヌムを行う自分のクッキヌを眮いたあずそのクッキヌから䞊䞋巊右斜めの各 8 方向に぀いお眮くクッキヌずすでに眮いおいた自分のクッキヌの間に盞手のクッキヌのみが盎線に䞊んでいた堎合にその挟たれた盞手のクッキヌのすべおが自分のクッキヌで眮き換えられるクッキヌゲヌムのプレむダヌは自分のタヌンが回っおきた時 1 ぀自分のクッキヌを眮くこずができるただし盞手のクッキヌを少なくずも 1 ぀以䞊自分のクッキヌに眮き換えられなければならないそのような眮き堎がない堎合自分のタヌンをパスをしなければならない お菓子の魔女も 巎マミ も考えるのが少々苊手であるそこで回っおきたタヌン毎にそのタヌンの䞭で眮き換えられるクッキヌの数を最倧化するこずを考えるこずにした 巎マミ のタヌンのずきに眮き換えられるクッキヌの数を最倧にするようなクッキヌを眮く堎所の候補が耇数ある堎合はより䞊の堎所をそれでも耇数ある堎合はより巊の堎所を遞択するこずにしたたた同様にお菓子の魔女のタヌンのずきに候補が耇数ある堎合はより䞋の堎所をそれでも耇数ある堎合はより右の堎所を遞択するこずにした テヌブルクロスに眮かれたクッキヌの状態が䞎えられるので巎マミからはじめ圌女たちがそこからクッキヌゲヌムを行い共に新たなクッキヌが眮けなくなるたでゲヌムを続けた時のテヌブルクロスの䞊に眮かれたクッキヌの状態を求めよ 入力圢匏 入力は以䞋の圢匏で䞎えられる s_{11} s_{12} ... s_{18}\\ s_{21} s_{22} ... s_{28}\\ ...\\ s_{81} s_{82} ... s_{88}\\ s_{ij} はテヌブルクロスに眮かれたクッキヌの初期状態を衚す文字で䞊から i 行目巊から j 列目の栌子の状態を衚すチョコレヌトクッキヌが眮かれおいるずき s_{ij} は 'o' でありチヌズクッキヌが眮かれおいるずきは 'x' , 䜕も眮かれおいないずきは '.' ずなる 出力圢匏 クッキヌゲヌムが行われた埌のテヌブルクロスの䞊に眮かれたクッキヌの状態を入力圢匏ず同じ圢匏で出力せよ 制玄 s_{ij} は 'o' , 'x' , '.' のいずれかである 入出力䟋 入力䟋 1 ooox.... .x...... ooo..... ........ ........ ........ ........ ........ 出力䟋1 ooooo... .o...... ooo..... ...o.... ....o... ........ ........ ........ 入力䟋 2 ........ ........ ........ ...ox... ...xo... ........ ........ ........ 出力䟋 2 xxxxxxxx xxxooxxx xxxxooxx xxxxxxxx ooxxooox ooxoooox oxooooox ooooooox 入力䟋 3 ........ ........ ..ooo... ..oxo... ..ooo... ........ ........ ........ 出力䟋 3 ........ ........ ..ooo... ..ooo... ..ooo... .....o.. ......o. ........ Problem Setter: Flat35
37,257
Score : 100 points Problem Statement We have a 3×3 square grid, where each square contains a lowercase English letters. The letter in the square at the i -th row from the top and j -th column from the left is c_{ij} . Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right. Constraints Input consists of lowercase English letters. Input Input is given from Standard Input in the following format: c_{11}c_{12}c_{13} c_{21}c_{22}c_{23} c_{31}c_{32}c_{33} Output Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right. Sample Input 1 ant obe rec Sample Output 1 abc The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a , b and c from top-right to bottom-left. Concatenate these letters and print abc . Sample Input 2 edu cat ion Sample Output 2 ean
37,258
Score : 2000 points Problem Statement You are given a connected graph with N vertices and M edges. The vertices are numbered 1 to N . The i -th edge is an undirected edge of length C_i connecting Vertex A_i and Vertex B_i . Additionally, an odd number MOD is given. You will be given Q queries, which should be processed. The queries take the following form: Given in the i -th query are S_i , T_i and R_i . Print YES if there exists a path from Vertex S_i to Vertex T_i whose length is R_i modulo MOD , and print NO otherwise. A path may traverse the same edge multiple times, or go back using the edge it just used. Here, in this problem, the length of a path is NOT the sum of the lengths of its edges themselves, but the length of the first edge used in the path gets multiplied by 1 , the second edge gets multiplied by 2 , the third edge gets multiplied by 4 , and so on. (More formally, let L_1,...,L_k be the lengths of the edges used, in this order. The length of that path is the sum of L_i \times 2^{i-1} .) Constraints 1 \leq N,M,Q \leq 50000 3 \leq MOD \leq 10^{6} MOD is odd. 1 \leq A_i,B_i\leq N 0 \leq C_i \leq MOD-1 1 \leq S_i,T_i \leq N 0 \leq R_i \leq MOD-1 The given graph is connected. (It may contain self-loops or multiple edges.) Input Input is given from Standard Input in the following format: N M Q MOD A_1 B_1 C_1 \vdots A_M B_M C_M S_1 T_1 R_1 \vdots S_Q T_Q R_Q Output Print the answers to the i -th query in the i -th line. Sample Input 1 3 2 2 2019 1 2 1 2 3 2 1 3 5 1 3 4 Sample Output 1 YES NO The answer to each query is as follows: The first query: If we take the path 1,2,3 , its length is 1 \times 2^0 + 2 \times 2^1 = 5 , so there exists a path whose length is 5 modulo 2019 . The answer is YES . The second query: No matter what path we take from Vertex 1 to Vertex 3 , its length will never be 4 modulo 2019 . The answer is NO . Sample Input 2 6 6 3 2019 1 2 4 2 3 4 3 4 4 4 5 4 5 6 4 6 1 4 2 6 1110 3 1 1111 4 5 1112 Sample Output 2 YES NO NO Sample Input 3 1 2 3 25 1 1 1 1 1 2 1 1 13 1 1 6 1 1 14 Sample Output 3 YES YES YES Sample Input 4 10 15 10 15 1 2 1 2 3 6 3 4 6 2 5 1 5 6 1 4 7 6 1 8 11 2 9 6 5 10 11 9 10 11 3 6 1 2 5 1 2 7 11 9 10 11 5 6 11 1 3 5 9 8 3 7 7 7 7 10 13 4 1 10 9 3 12 10 10 14 9 2 1 6 6 5 8 8 4 Sample Output 4 YES NO NO NO NO NO NO YES YES NO
37,259
3D Printing We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction. First, using a CAD system, we prepare n ( n ≥ k ) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied. Each cube may overlap zero, one or two other cubes, but not three or more. When a cube overlap two other cubes, those two cubes do not overlap. Two non-overlapping cubes do not touch at their surfaces, edges or corners. Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area. Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones. Figure E1. A polyhedron formed with connected identical cubes. Input The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format. n k s x 1 y 1 z 1 ... x n y n z n In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i -th line, there are three integers x i , y i and z i that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied. The parameters satisfy the following conditions: 1 ≀ k ≀ n ≀ 2000, 3 ≀ s ≀ 100, and -4×10 7 ≀ x i , y i , z i ≀ 4×10 7 . The end of the input is indicated by a line containing three zeros separated by a space. Output For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1. Sample Input 1 1 100 100 100 100 6 4 10 100 100 100 106 102 102 112 110 104 104 116 102 100 114 104 92 107 100 10 4 10 -100 101 100 -108 102 120 -116 103 100 -124 100 100 -132 99 100 -92 98 100 -84 100 140 -76 103 100 -68 102 100 -60 101 100 10 4 10 100 100 100 108 101 100 116 102 100 124 100 100 132 102 100 200 100 103 192 100 102 184 100 101 176 100 100 168 100 103 4 4 10 100 100 100 108 94 100 116 100 100 108 106 100 23 6 10 100 100 100 96 109 100 100 118 100 109 126 100 118 126 100 127 118 98 127 109 104 127 100 97 118 91 102 109 91 100 111 102 100 111 102 109 111 102 118 111 102 91 111 102 82 111 114 96 111 114 105 102 114 114 93 114 114 84 114 105 84 114 96 93 114 87 102 114 87 10 3 10 100 100 100 116 116 102 132 132 104 148 148 106 164 164 108 108 108 108 124 124 106 140 140 104 156 156 102 172 172 100 0 0 0 Output for the Sample Input 60000 1856 -1 1632 1856 2796 1640
37,260
Score : 100 points Problem Statement Given is a three-digit integer N . Does N contain the digit 7 ? If so, print Yes ; otherwise, print No . Constraints 100 \leq N \leq 999 Input Input is given from Standard Input in the following format: N Output If N contains the digit 7 , print Yes ; otherwise, print No . Sample Input 1 117 Sample Output 1 Yes 117 contains 7 as its last digit. Sample Input 2 123 Sample Output 2 No 123 does not contain the digit 7 . Sample Input 3 777 Sample Output 3 Yes
37,262
Score : 100 points Problem Statement Alice and Bob are playing One Card Poker . One Card Poker is a two-player game using playing cards. Each card in this game shows an integer between 1 and 13 , inclusive. The strength of a card is determined by the number written on it, as follows: Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong One Card Poker is played as follows: Each player picks one card from the deck. The chosen card becomes the player's hand. The players reveal their hands to each other. The player with the stronger card wins the game. If their cards are equally strong, the game is drawn. You are watching Alice and Bob playing the game, and can see their hands. The number written on Alice's card is A , and the number written on Bob's card is B . Write a program to determine the outcome of the game. Constraints 1≩A≩13 1≩B≩13 A and B are integers. Input The input is given from Standard Input in the following format: A B Output Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn. Sample Input 1 8 6 Sample Output 1 Alice 8 is written on Alice's card, and 6 is written on Bob's card. Alice has the stronger card, and thus the output should be Alice . Sample Input 2 1 1 Sample Output 2 Draw Since their cards have the same number, the game will be drawn. Sample Input 3 13 1 Sample Output 3 Bob
37,263
Where's Your Robot? You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as ( i, j ), where 1 <= i <= m and 1 <= j <= n. Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following. FORWARD k Go forward by k tiles to its facing direction (1 <= k < 100). BACKWARD k Go backward by k tiles, without changing its facing direction (1 <= k < 100). RIGHT Turn to the right by ninety degrees. LEFT Turn to the left by ninety degrees. STOP Stop. While executing either a " FORWARD " or a " BACKWARD " command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction. After finishing or giving up execution of a given command, your robot will stand by for your next command. Input The input consists of one or more command sequences. Each input line has at most fifty characters. The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input. Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument. A command sequence is terminated by a line containing a " STOP " command. The next command sequence, if any, starts from the next line. Output The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair ( i, j ), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces. Sample Input 6 5 FORWARD 3 RIGHT FORWARD 5 LEFT BACKWARD 2 STOP 3 1 FORWARD 2 STOP 0 0 Output for the Sample Input 6 2 1 1
37,264
F 01 文字列ず窓 (Binary String with Slit) 問題 文字の皮類が 0 ず 1 のみからなる文字列 S が䞎えられたす。以䞋の操䜜を繰り返すこずで、 S を T に倉えたいです。 文字列 S 䞭の最も右偎に登堎する 1 を含むように、幅 2 のスリットを眮く。スリット内には連続した 2 文字を必ず含たなければならない。぀たり、文字列の端 1 文字のみを含むように眮くこずはできない。 2 通り眮ける堎合も考えられるが、この堎合はどちらの方法で眮いおも構わない。 スリット䞭の 2 文字を 2 桁の二進数ず捉えるずき、元の数倀ずの差の絶察倀が 1 になるようにスリット䞭の文字を倉曎する。ただし、スリット䞭の文字の䞡方が 0 になっおはならない。぀たり、倉曎埌のスリット内の数倀は 1 から 3 たでのいずれかずなる。 ク゚リが Q 回䞎えられたす。 i 番目のク゚リで 1 を少なくずも䞀぀含む文字列 S_i, T_i が䞎えられるので、 S_i を T_i に倉えるために必芁な操䜜回数の最小倀を、それぞれのク゚リに぀いお求めおください。 入力圢匏 Q S_1 T_1 ... S_Q T_Q 1 行目では、ク゚リの個数 Q が䞎えられる。 2 行目以降 Q 行は、ク゚リが䞎えられる。 i+1 行目では S_i ず T_i が空癜区切りで䞎えられる。 制玄 1 \leq Q \leq 10^5 2 \leq |S_i| = |T_i| \leq 50 S_i, T_i は 0 ず 1 のみからなる文字列である。 S_i, T_i はずもに 1 を少なくずも 1 ぀含む文字列である。 出力圢匏 出力は Q 行からなる。 i 行目には、 i 番目のク゚リに察する結果を出力せよ。 入力䟋1 4 101 110 101 101 1010 1101 11011001 10010101 出力䟋1 1 0 3 12 1 個目のク゚リでは、 S = 101 を T = 110 ず䞀臎させるために必芁な操䜜回数の最小倀を求める必芁がありたす。以䞋の画像のように、 S の䞭に登堎する 1 の䞭で最も右にあるものが含たれるようにスリットを眮き、スリット䞭の文字列を曞き換えるこずで 1 回の操䜜で S ず T を䞀臎させるこずができたす。 2 個目のク゚リでは、はじめから S ず T が䞀臎しおいるため、操䜜の必芁がありたせん。 3 個目のク゚リに぀いお、以䞋の画像のように文字列を倉曎させるず 3 回の操䜜で S ず T を䞀臎させるこずができたす。
37,265
Balls and Boxes 11 Balls Boxes Any way At most one ball At least one ball Distinguishable Distinguishable 1 2 3 Indistinguishable Distinguishable 4 5 6 Distinguishable Indistinguishable 7 8 9 Indistinguishable Indistinguishable 10 11 12 Problem You have $n$ balls and $k$ boxes. You want to put these balls into the boxes. Find the number of ways to put the balls under the following conditions: Each ball is not distinguished from the other. Each box is not distinguished from the other. Each ball can go into only one box and no one remains outside of the boxes. Each box can contain at most one ball. Note that you must print this count modulo $10^9+7$. Input $n$ $k$ The first line will contain two integers $n$ and $k$. Output Print the number of ways modulo $10^9+7$ in a line. Constraints $1 \le n \le 1000$ $1 \le k \le 1000$ Sample Input 1 5 10 Sample Output 1 1 Sample Input 2 200 100 Sample Output 2 0
37,266
Score : 700 points Problem Statement There is an H \times W grid ( H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i -th row and j -th column is equal to the j -th character in the string S_i . Snuke can apply the following operation to this grid any number of times: Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric . That is, for any 1 \leq i \leq H and 1 \leq j \leq W , the letter in the square at the i -th row and j -th column and the letter in the square at the (H + 1 - i) -th row and (W + 1 - j) -th column should be equal. Determine if Snuke can achieve this objective. Constraints 1 \leq H \leq 12 1 \leq W \leq 12 |S_i| = W S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print YES ; if he cannot, print NO . Sample Input 1 2 3 arc rac Sample Output 1 YES If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below: Sample Input 2 3 7 atcoder regular contest Sample Output 2 NO Sample Input 3 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Sample Output 3 YES
37,267
Problem K Black and White Boxes Alice and Bob play the following game. There are a number of straight piles of boxes. The boxes have the same size and are painted either black or white. Two players, namely Alice and Bob, take their turns alternately. Who to play first is decided by a fair random draw. In Alice's turn, she selects a black box in one of the piles, and removes the box together with all the boxes above it, if any. If no black box to remove is left, she loses the game. In Bob's turn, he selects a white box in one of the piles, and removes the box together with all the boxes above it, if any. If no white box to remove is left, he loses the game. Given an initial configuration of piles and who plays first, the game is a definite perfect information game . In such a game, one of the players has sure win provided he or she plays best. The draw for the first player, thus, essentially decides the winner. In fact, this seemingly boring property is common with many popular games, such as chess. The chess game, however, is complicated enough to prevent thorough analyses, even by supercomputers, which leaves us rooms to enjoy playing. This game of box piles, however, is not as complicated. The best plays may be more easily found. Thus, initial configurations should be fair, that is, giving both players chances to win. A configuration in which one player can always win, regardless of who plays first, is undesirable. You are asked to arrange an initial configuration for this game by picking a number of piles from the given candidate set. As more complicated configuration makes the game more enjoyable, you are expected to find the configuration with the maximum number of boxes among fair ones. Input The input consists of a single test case, formatted as follows. $n$ $p_1$ . . . $p_n$ A positive integer $n$ ($\leq 40$) is the number of candidate piles. Each $p_i$ is a string of characters B and W , representing the $i$-th candidate pile. B and W mean black and white boxes, respectively. They appear in the order in the pile, from bottom to top. The number of boxes in a candidate pile does not exceed 40. Output Output in a line the maximum possible number of boxes in a fair initial configuration consisting of some of the candidate piles. If only the empty configuration is fair, output a zero. Sample Input 1 4 B W WB WB Sample Output 1 5 Sample Input 2 6 B W WB WB BWW BWW Sample Output 2 10
37,268
Score : 100 points Problem Statement Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache. Constraints 1 ≀ X,A,B ≀ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache. Sample Input 1 4 3 6 Sample Output 1 safe He ate the food three days after the "best-by" date. It was not delicious or harmful for him. Sample Input 2 6 5 1 Sample Output 2 delicious He ate the food by the "best-by" date. It was delicious for him. Sample Input 3 3 7 12 Sample Output 3 dangerous He ate the food five days after the "best-by" date. It was harmful for him.
37,269
Problem K: Collector ※この物語はフィクションであり、実圚の人物、団䜓などずは䞀切関係がありたせん。 最近゜ヌシャルゲヌムがずおも人気で、 たくさんの䌚瀟が゜ヌシャルゲヌムを開発しおいたす。 あなたは競合しおいる゜ヌシャルゲヌム開発䌚瀟の䞀぀にスパむずしお朜入しおいたす。 問題 あなたの務めおいる䌚瀟では、゜ヌシャルゲヌムを開発しおいる。 開発しおいるゲヌムは、カヌドを集めお戊うゲヌムであり、 1回300円 の有料”ガチャガチャ”を収益の柱にしおいる。 このゲヌムのカヌドには、”ノヌマル”、”レア”、”Sレア”の3皮類がある。”Sレア”カヌドはN皮類存圚する。 有料”ガチャガチャ”では、1回行うごずに97の確率で1枚のレアカヌドが手に入り、3%の確率で1枚のSレアカヌドが手に入る。 有料”ガチャガチャ”における各Sレアカヌドの出珟割合(合蚈3%)を操䜜する暩限を持っおいるあなたは、 䌚瀟の利益を「枛少」させるこずにした。 たず、様々なプレむダヌの有料”ガチャガチャ”の利甚傟向を調べたずころ、 「各プレむダヌは、 各々の奜きなSレアカヌドを匕くず、 満足し、その埌は”ガチャガチャ”の利甚を行わない」ずいうこずが明らかになった。 以䞊の情報より、 あなたは ”各プレむダヌは、特定のSレアカヌドを奜み、そのSレアカヌドを匕くたでガチャガチャをし続ける” ずいう仮定の䞋で䌚瀟の利益をモデル化した。 すなわち䌚瀟の利益ずは、 Sレアカヌドiを奜む人の割合 × Sレアカヌドiが埗られるたでの消費金額の期埅倀 の総和である。 Sレアカヌドの出珟割合を適切に操䜜するこずで、 䌚瀟の利益を「最小化」せよ。 ただし、出珟割合は以䞋の条件を満たさなければならない。 Sレアカヌド党䜓の出珟割合の和は3%である 各Sレアカヌドの出珟割合は最䜎0.01%である 入力 N (Sレアカヌドの皮類) P_1 (カヌド1が奜きな人の割合) P_2 (カヌド2が奜きな人の割合) ... P_N (カヌドNが奜きな人の割合) 出力 問題文で定矩された䌚瀟の利益の最小倀を出力せよ。 制玄 \( N \)は敎数 \( 1 \leq N \leq 300 \) \( 0.0 \leq P_i (1 \leq i \leq N) \leq 1.0 \) \( P_1 + P_2 + \cdots + P_N = 1.0 \) \( P_i \)は小数点以䞋5桁たで䞎えられる 誀差は盞察\( 10 ^ {-6} \) たで蚱容される 入出力䟋 入力1 1 1.00000 出力1 10000 入力2 2 0.00001 0.99999 出力2 10063.3444816 カヌド1は\( 0.01% \)の割合で出珟し、カヌド2は\( 2.99% \)の割合で出珟するように蚭定する。 入力3 2 0.50000 0.50000 出力3 20000.0000000
37,270
Score : 400 points Problem Statement You are given N integers; the i -th of them is A_i . Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like. Constraints 2 \leq N \leq 10^5 1 \leq A_i \leq 10^9 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 maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like. Sample Input 1 5 6 8 1 2 3 Sample Output 1 21 When the integers are arranged as 3,8,1,6,2 , the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21 . This is the maximum possible sum. Sample Input 2 6 3 1 4 1 5 9 Sample Output 2 25 Sample Input 3 3 5 5 1 Sample Output 3 8
37,272
Problem H: Petoris You are playing a puzzle game named petoris . It is played with a board divided into square grids and square tiles each of which fits to a single grid. In each step of the game, you have a board partially filled with tiles. You also have a block consisting of several tiles. You are to place this block somewhere on the board or to discard it, under the following restrictions on placement: the block can be rotated, but cannot be divided nor flipped; no tiles of the block can collide with any tiles existing on the board; and all the tiles of the block need to be placed inside the board. Your task is to write a program to find the maximum score you can earn in this step. Here, the score is the number of the horizontal lines fully filled with tiles after the block is placed, or -1 in case of discard. Input The first line of the input is N , the number of data sets. Then N data sets follow. Each data set consists of lines describing a block and a board. Each description (both for a block and a board) starts with a line containing two integer H and W , the vertical and horizontal dimension. Then H lines follow, each with W characters, where a ‘#’ represents a tile and ‘.’ a vacancy. You can assume that 0 < H ≀ 64 and 0 < W ≀ 64. Each block consists of one or more tiles all of which are connected. Each board contains zero or more tiles, and has no horizontal line fully filled with tiles at the initial state. Output For each data set, print in a single line the maximum possible score. Sample Input 5 4 4 .... .... #### .... 12 8 ........ ........ ........ ........ ........ .......# ##.##..# .####### .####### .####### .####### .####.#. 4 4 .... .... .### ...# 12 8 ........ ........ ........ ........ ........ ........ ........ ##...### ##.##### #######. #######. #######. 4 4 #### #..# #..# #### 12 8 ........ ........ ........ ........ ........ .......# ##.##..# ##....## ##.##.## ##.##.## ##....## .####.#. 2 2 ## #. 3 3 .## .## ##. 4 4 .... .##. .##. .... 2 2 .. .. Output for the Sample Input 4 1 4 -1 2
37,273
Score : 1800 points Problem Statement This is an output-only problem. You shouldn't read anything from the input. In short, your task is to simulate multiplication by using only comparison (x < y) and addition (x + y) . There is no input in this problem, you just print a sequence of operations. Imagine that there is a big array a[0], a[1], ..., a[N-1] of length N . The first two values are initially two non-negative integers A and B (which are unknown to you), the other elements are zeros. Your goal is to get the product A \cdot B in a[2] at the end. You are allowed operations of two types, with the following format (where 0 \leq i, j, k < N ): + i j k — applies operation a[k] = a[i] + a[j] . < i j k — applies operation a[k] = a[i] < a[j] . That is, if a[i] < a[j] then a[k] becomes 1 , otherwise it becomes 0 . You can use at most Q operations. Elements of a can't exceed V . Indices (i, j, k) don't have to be distinct. It's allowed to modify any element of the array (including the first two). The actual checker simulates the process for multiple pairs (A, B) within a single test. Each time, the checker chooses values A and B , creates the array a = [A, B, 0, 0, \ldots, 0] , applies all your operations and ensures that a[2] = A \cdot B . Constraints 0 \leq A, B \leq 10^9 N = Q = 200\,000 V = 10^{19} = 10\,000\,000\,000\,000\,000\,000 Partial Score 800 points will be awarded for passing tests that satisfy A, B \leq 10 . Another 1000 points will be awarded for passing all tests. Input The Standard Input is empty. Output In the first line, print the number of operations. Each operation should then be printed in a single line of format + i j k or < i j k . Sample Input 1 Sample Output 1 4 < 0 1 8 + 0 1 2 + 2 8 2 + 0 0 0 In the first sample test, the checker checks your sequence only for a pair (A, B) = (2, 3) . The provided output is correct for this test: Initially, a[0] = 2 , a[1] = 3 , a[2] = a[3] = \ldots = a[N-1] = 0 . < 0 1 8 applies a[8] = 1 because a[0] < a[1] . + 0 1 2 applies a[2] = a[0] + a[1] = 5 . + 2 8 2 applies a[2] = a[2] + a[8] = 6 . + 0 0 0 applies a[0] = a[0] + a[0] = 4 . As required, at the end we have a[2] = 6 = A \cdot B .
37,274
数列 次のように定矩されおいる数列がありたす。 すべおの偶数番目の項は䞀぀前の項に 2 を掛けたものず等しい数である。 すべおの奇数番目の項は䞀぀前の項を 3 で割ったものず等しい数である。 この数列の初項 a を読み蟌み、初項から第 10 項たでの和 s(10) を出力するプログラムを䜜成しおください。 Input 入力は耇数のテストケヌスからなりたす。各テストケヌスずしお、数列の初項を衚す実数 a (1.0 ≀ a ≀ 10.0) が行に䞎えられたす。 テストケヌスの数は 50 を超えたせん。 Output テストケヌスごずに s(10) を行に出力したす。 出力は0.000001以䞋の誀差を含んでもよい。 Sample Input 1.0 2.0 3.0 Output for the Sample Input 7.81481481 15.62962963 23.44444444
37,275
Problem Statement You are given a positive integer sequence $A$ of length $N$. You can remove any numbers from the sequence to make the sequence “friendly". A sequence is called friendly if there exists an integer $k$ (>1) such that every number in the sequence is a multiple of $k$. Since the empty sequence is friendly, it is guaranteed that you can make the initial sequence friendly. You noticed that there may be multiple ways to make the sequence friendly. So you decide to maximize the sum of all the numbers in the friendly sequence. Please calculate the maximum sum of the all numbers in the friendly sequence which can be obtained from the initial sequence. Input The input consists of a single test case formatted as follows. $N$ $A_1$ $\vdots$ $A_N$ The first line consists of a single integer $N$ ($1 \le N \le 1000$). The $i+1$-st line consists of an integer $A_i$ ($1 \le A_i \le 10^9$ for $1 \le i \le N$). Output Print the maximum sum of all the numbers in the friendly sequence which can be obtained from the initial sequence. Examples Input Output 6 1 2 3 4 5 6 12 3 173 1733 111733 111733 4 1 1 1 1 0 10 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 9999999990 1 999999999 999999999 10 28851 8842 9535 2311 25337 26467 12720 10561 8892 6435 56898
37,276
アカベコ 「アカベコ」は、むヅア地方の専甚劇堎で公挔を開催しおいるグルヌプです。アカベコのそれぞれのメンバヌは、ある䞀定の日数ごずに公挔に参加するこずになっおいたす。 今日の公挔ではメンバヌが党員参加しおいたした。プロデュヌサヌであるあなたは、メンバヌから、今埌の公挔のメンバヌの組み合わせを教えおほしい、ず頌たれたした。あなたは、公挔に参加するメンバヌの組み合わせがいく぀あるのかを数えるこずにしたした。 アカベコのメンバヌ数ず、それぞれのメンバヌが公挔に参加する呚期が日単䜍で䞎えられたずき、参加するメンバヌの組み合わせが䜕通りあるかを数えるプログラムを䜜成せよ。このずき、グルヌプは同じメンバヌで氞遠に存続するず仮定する。ただし、だれも参加しない堎合は組み合わせに含めないこずずする。 入力 入力は以䞋の圢匏で䞎えられる。 $N$ $p_1$ $p_2$ ... $p_N$ 行目にアカベコのメンバヌ数$N$ ($1 \leq N \leq 20$)が䞎えられる。続く行に、それぞれのメンバヌが公挔に参加する呚期$p_i$ ($1 \leq p_i \leq 40$)が䞎えられる。 出力 参加するメンバヌの組み合わせの数を行に出力する。 入出力䟋 入力䟋 3 3 5 2 出力䟋 7 入力䟋 3 2 3 6 出力䟋 3 呚期が日のメンバヌのみが参加する公挔呚期が日のメンバヌのみが参加する公挔呚期が日、日、日のメンバヌが参加する公挔の通りずなる。
37,277
Splice For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. dump($t$): Print all elements in $L_t$. splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0 , 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Constraints $1 \leq n \leq 1,000$ $1 \leq q \leq 500,000$ For a splice operation, $s \ne t$ For a splice operation, $L_s$ is not empty The total number of elements printed by dump operations do not exceed 1,000,000 $-1,000,000,000 \leq x \leq 1,000,000,000$ Sample Input 1 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Sample Output 1 1 2 3 4 5 6
37,278
芆面算 数匏の䞀郚を隠しおしたっお、隠した数字を探す問題を芆面算ずいいたす。今回は、数匏の䞭のある数字のいく぀かを X で隠しおしたった匏を扱いたす。以䞋の数匏を入力しお、結果を出力するプログラムを䜜成しおください。 数匏 「数字列 + 数字列 = 数字列」の圢で、1 行の単玔な足し算匏です。 「数字列」は、数字 0 から 9 ず文字 X の䞊びです。 2 桁以䞊の「数字列」の巊端の数字は 0 ではないものずしたす。 X は、数匏党䜓の䞭で必ず 1 ぀以䞊含たれたす。 結果 芆面算の答えです。数匏が成り立぀様な X の倀で 0 ~ 9 のどれか 1 ぀です。2 ぀以䞊の答えはないものずしたす。 答えがない堎合、結果は“NA”ずしおください。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットずしお、X を 1 ぀以䞊含む足し算匏(空癜を含たない 126 文字以内の文字列) が行に䞎えられたす。デヌタセットの数は 150 を超えたせん。 Output 各デヌタセットに぀いお、芆面算の結果を行に出力しおください。数字 0 ~ 9 たたは NA を出力しおください。 Sample Input 123+4X6=X79 12X+4X6=X79 XX22+89=X2XX Output for the Sample Input 5 NA 1
37,279
舞螏䌚 (Ball) IOI 王囜では王女である JOI 姫の誕生日を祝っお舞螏䌚が開かれるこずになった 舞螏䌚には N 人の貎族が参加する予定である N は奇数である貎族には 1 から N たでの番号が付けられおいるそれぞれの貎族には螊りのうたさずいう敎数が定められおおり貎族 i (1 ≀ i ≀ N ) の螊りのうたさは D i である 舞螏䌚では JOI 姫を含む N + 1 人で 2 人ず぀組を䜜っお螊るIOI 王囜では䞊玚者が初玚者を補助できるように䌝統的に以䞋の方法で螊りの組を決定しおいる 最初に N 人の貎族が 1 列に䞊ぶ 列に䞊んでいる貎族が 1 人になるたで以䞋の操䜜を繰り返す - 列の先頭から 3 人の貎族の螊りのうたさを調べる - その 3 人の貎族の䞭で最も螊りのうたさが倧きい貎族を A ずおくただし耇数いる堎合は最も螊りのうたさが倧きい貎族の䞭で最も番号の小さい貎族 を A ずおく - その 3 人の貎族の䞭で最も螊りのうたさが小さい貎族を B ずおくただし耇数いる堎合は最も螊りのうたさが小さい貎族の䞭で最も番号の倧きい貎族 を B ずおく - A ず B が列から抜けお組になる - 残った 1 人は列の最埌尟に移動する 最終的に残った 1 人が JOI 姫ず組になる 貎族 1 から貎族 M (1 ≀ M ≀ N − 2) の M 人の貎族に぀いおはすでに初期状態で列の䜕番目に䞊ぶのかが決たっおいる残りの N − M 人の貎族の䞊び方は囜王が自由に決めるこずができる JOI 姫は螊りを孊んだばかりなので囜王は JOI 姫ず組になる貎族の螊りのうたさをできるだけ倧きくしたいず考えおいるJOI 姫ず組になる貎族の螊りのうたさずしお考えられる最倧倀を求めよ 課題 それぞれの貎族の螊りのうたさず M 人の貎族の初期状態で䞊ぶ堎所が䞎えられたずきJOI 姫ず組になる貎族の螊りのうたさずしお考えられる最倧倀を求めるプログラムを䜜成せよ 入力 暙準入力から以䞋のデヌタを読み蟌め 1 行目には2 個の敎数 N , M が空癜を区切りずしお曞かれおいるこれは舞螏䌚に貎族が N 人参加し列に䞊ぶ堎所がすでに決たっおいる貎族が M 人いるこずを衚す 続く M 行のうちの i 行目 (1 ≀ i ≀ M ) には2 個の敎数 D i , P i が空癜を区切りずしお曞かれおいるこれは貎族 i の螊りのうたさが D i で貎族 i が初期状態で列の先頭から P i 番目に䞊ぶこずを衚す 続く N − M 行のうちの i 行目 (1 ≀ i ≀ N − M ) には敎数 D i+M が曞かれおいるこれは貎族 ( i + M )の螊りのうたさが D i+M であるこずを衚す 出力 暙準出力にJOI 姫ず組になる貎族の螊りのうたさずしお考えられる最倧倀を衚す敎数を 1 行で出力せよ 制限 すべおの入力デヌタは以䞋の条件を満たす 3 ≀ N ≀ 99 999 N は奇数である 1 ≀ M ≀ N − 2 1 ≀ D i ≀ 1 000 000 000 (1 ≀ i ≀ N ) 1 ≀ P i ≀ N (1 ≀ i ≀ M ) P i ≠ P j (1 ≀ i < j ≀ M ) 入出力䟋 入力䟋 1 7 3 5 2 5 5 8 6 6 2 8 9 出力䟋 1 8 初期状態では 3 人の貎族の䞊ぶ堎所がすでに決たっおいる 括匧内の数字は螊りのうたさを衚す巊端が列の先頭である 䟋えば先頭から順に貎族 5貎族 1貎族 4貎族 6貎族 2貎族 3貎族 7 ずいう順番に䞊んだ堎 合を考える すべおの貎族が䞊んだあずの配眮 この堎合以䞋のように列が倉化しおいく 列の先頭の 3 人の貎族 (貎族 5貎族 1貎族 4) 䞭で最も螊りのうたさが倧きい貎族 4 ず最も螊りのうたさが小さい貎族 5 が組になり残った貎族 1 が最埌尟に移動する 次に列の先頭の 3 人の貎族 (貎族 6貎族 2貎族 3) の䞭で最も螊りのうたさが倧きい貎族は貎族 6 ず貎族 3 の 2 人でありこのうち番号の小さい貎族は貎族 3 であるたた列の先頭の 3 人の貎族のうち最も螊りのうたさが小さい貎族は貎族 2 である貎族 3 ず貎族 2 が組になり残った貎族 6 が最埌尟に移動する 次に列の先頭の 3 人の貎族 (貎族 7貎族 1貎族 6) の䞭で最も螊りのうたさが倧きい貎族 7 ず最も螊りのうたさが小さい貎族 1 が組になり残った貎族 6 が最埌尟に移動する 最終的に貎族 6 が残りJOI 姫ず組になる貎族 6 の螊りのうたさは 8 であるこの倀が JOI 姫ず組になる貎族の螊りのうたさずしお考えられる最倧倀である 列の倉化の様子 入力䟋 2 3 1 5 3 5 5 出力䟋 2 5 どのような順番で䞊んでも貎族 2 ず JOI 姫が組になる 入力䟋 3 7 2 32 4 27 6 37 41 41 30 27 出力䟋 3 37 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,280
J: 巊厎・右男 問題文 巊厎さんず右男さんは、長さ $N$ の数列 $a_1, a_2, \dots ,a_n$ を䜿ったゲヌムで遊ぶのが奜きです。 このゲヌムでは巊厎さんず右男さんで亀互に以䞋の操䜜を行いたす。 巊厎さんの手番では $1 \leq k \leq n$ を指定し、$a_1, a_2, \dots ,a_k$ の倀をそれぞれ $1$ ず぀枛らす。 右男さんの手番では $1 \leq k \leq n$ を指定し、$a_k, a_{k+1}, \dots ,a_n$ の倀をそれぞれ $1$ ず぀枛らす。 手番終了時に数列の䞭に負の数が存圚しおいた堎合、その操䜜をした人は負けたす。負けなかった方は勝ちたす。最初に操䜜をするのは巊厎さんです。 巊厎さんはこのゲヌムをプレむするために、$0, 1, \dots, S$ からなる長さ $N$ の数列を党皮類$(S+1)^N$ 通り買っおきたした。巊厎さんず右男さんがこれらの数列を䜿っお $1$ 回ず぀ゲヌムをしたした。巊厎さんが勝った回数を $998244353$ で割ったあたりを求めおください。ただし、䞡者ずもそれぞれが勝぀ために最適な操䜜をしたずしたす。 制玄 入力は党お敎数 $2 \leq N \leq 1000$ $1 \leq S \leq 10^9$ 入力 入力は以䞋の圢匏で暙準入力から䞎えられたす。 $N$ $S$ 出力 党おの数列を䜿っお $1$ 回ず぀ゲヌムをしたずきに、巊厎さんが勝った回数を $998244353$ で割ったあたりを出力しおください。 入出力䟋 入力䟋1 2 1 出力䟋1 2 巊厎さんが買っおきた数列は (0, 0), (0, 1), (1, 0), (1, 1) の $4$ ぀です。 (0, 0), (0, 1) を䜿ったゲヌムでは、巊厎さんは最初の手番でどの $k$ を遞んでも負けたす。 (1, 0) を䜿ったゲヌムでは、巊厎さんは $k = 1$ を遞べば、数列が (0, 0) ずなり、勝おたす。 (1, 1) を䜿ったゲヌムでは、巊厎さんは $k = 2$ を遞べば、数列が (0, 0) ずなり、勝おたす。 入力䟋2 3 2 出力䟋2 12 巊厎さんが勝぀数列は以䞋の $12$ 個です。 (1,0,0) (1,1,0) (1,1,1) (1,2,0) (1,2,1) (2,0,0) (2,0,1) (2,1,0) (2,1,1) (2,1,2) (2,2,0) (2,2,1) 入力䟋3 1000 1000000000 出力䟋3 972070366
37,281
Problem C: GIGA Universe Cup Following FIFA World Cup, a larger competition called ``GIGA Universe Cup'' is taking place somewhere in our universe. Both FIFA World Cup and GIGA Universe Cup are two rounds competitions that consist of the first round, also known as ``group league,'' and the second called ``final tournament.'' In the first round, participating teams are divided into groups of four teams each. Each team in a group plays a match against each of the other teams in the same group. For example, let's say we have a group of the following four teams, ``Engband, Swedon, Argontina, and Nigerua.'' They play the following six matches: Engband - Swedon, Engband - Argontina, Engband - Nigerua, Swedon - Argontina, Swedon - Nigerua, and Argontina - Nigerua. The result of a single match is shown by the number of goals scored by each team, like ``Engband 1 - 0 Argontina,'' which says Engband scored one goal whereas Argontina zero. Based on the result of a match, points are given to the two teams as follows and used to rank teams. If a team wins a match (i.e., scores more goals than the other), three points are given to it and zero to the other. If a match draws (i.e., the two teams score the same number of goals), one point is given to each. The goal difference of a team in given matches is the total number of goals it scored minus the total number of goals its opponents scored in these matches. For example, if we have three matches ``Swedon 1 - 2 Engband,'' ``Swedon 3 - 4 Nigerua,'' and ``Swedon 5 - 6 Argontina,'' then the goal difference of Swedon in these three matches is (1 + 3 + 5) - (2 + 4+ 6) = -3. Given the results of all the six matches in a group, teams are ranked by the following criteria, listed in the order of priority (that is, we first apply (a) to determine the ranking, with ties broken by (b), with ties broken by (c), and so on). (a) greater number of points in all the group matches; (b) greater goal difference in all the group matches; (c) greater number of goals scored in all the group matches. If two or more teams are equal on the basis of the above three criteria, their place shall be determined by the following criteria, applied in this order: (d) greater number of points obtained in the group matches between the teams concerned; (e) greater goal difference resulting from the group matches between the teams concerned; (f) greater number of goals scored in the group matches between the teams concerned; If two or more teams are stiIl equal, apply (d), (e), and (f) as necessary to each such group. Repeat this until those three rules to equal teams do not make any further resolution. Finally, teams that still remain equal are ordered by: (g) drawing lots by the Organizing Committee for the GIGA Universe Cup. The two teams coming first and second in each group qualify for the second round. Your job is to write a program which, given the results of matches played so far in a group and one team specified in the group, calculates the probability that the specified team will qualify for the second round. You may assume each team has played exactly two matches and has one match to play. In total, four matches have been played and two matches are to be played. Assume the probability that any team scores (exactly) p goals in any match is: for p ≀ 8, and zero for p > 8 . Assume the lot in the step (g) is fair. Input The first line of the input is an integer, less than 1000, that indicates the number of subsequent records. The rest of the input is the indicated number of records. A single record has the following format: In the above, <_> is a single underscore (_) and < empty > a sequence of exactly four underscores (____). Each of < team > 1 , ... , < team > 4 is either an asterisk character (*) followed by exactly three uppercase letters (e.g., *ENG), or an underscore followed by exactly three uppercase letters (e.g., _SWE). The former indicates that it is the team you are asked to calculate the probability of the second round qualification for. You may assume exactly one of < team > 1 , ... , < team > 4 is marked with an asterisk. Each < m > ij (1 ≀ i < j ≀ 4) is a match result between the < team > i and < team > j . Each match result is either __-_ (i.e., two underscores, hyphen, and another underscore) or of the form _ x - y where each of x and y is a single digit (≀ 8) . The former indicates that the corresponding match has not been played, whereas the latter that the result of the match was x goals by < team > i and y goals by < team > j . Since each team has played exactly two matches, exactly two match results are in the former format. Output The output should consist of n lines where n is the number of records in the input. The i th line should show the probability that the designated team (marked with an asterisk) will qualify for the second round in the i th record. Numbers should be printed with exactly seven digits after the decimal point. Each number should not contain an error greater than 10 -7 . Sample Input 5 _____*AAA__BBB__CCC__DDD *AAA_______0-0__0-0___-_ _BBB_____________-___0-0 _CCC_________________0-0 _DDD____________________ ______CHN__CRC__TUR_*BRA _CHN_______0-2___-___0-4 _CRC____________1-1___-_ _TUR_________________1-2 *BRA____________________ ______CMR_*KSA__GER__IRL _CMR_______1-0___-___1-1 *KSA____________0-8___-_ _GER_________________1-1 _IRL____________________ ______TUN__JPN_*BEL__RUS _TUN________-___1-1__0-2 _JPN____________2-2__1-0 *BEL__________________-_ _RUS____________________ ______MEX__CRO_*ECU__ITA _MEX_______1-0__2-1___-_ _CRO_____________-___2-1 *ECU_________________0-2 _ITA____________________ Output for the Sample Input 0.5000000 1.0000000 0.0000000 0.3852746 0.0353304
37,282
Problem Statement We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north and west, and northnorthwest is between north and northwest. In this problem, we describe more detailed direction between north and west as follows. " north " means $0$ degrees. " west " means $90$ degrees. If the direction $dir$ means $a$ degrees and the sum of the occurrences of " north " and " west " in $dir$ is $n$ ($\geq$ 1), " north "$dir$ (the concatenation of " north " and $dir$) means $a - \frac{90}{2^n}$ degrees and " west "$dir$ means $a + \frac{90}{2^n}$ degrees. Your task is to calculate the angle in degrees described by the given direction. Input The input contains several datasets. The number of datasets does not exceed $100$. Each dataset is described by a single line that contains a string denoting a direction. You may assume the given string can be obtained by concatenating some " north " and " west ", the sum of the occurrences of " north " and " west " in the given string is between $1$ and $20$, inclusive, and the angle denoted by the given direction is between $0$ and $90$, inclusive. The final dataset is followed by a single line containing only a single " # ". Output For each dataset, print an integer if the angle described by the given direction can be represented as an integer, otherwise print it as an irreducible fraction. Follow the format of the sample output. Sample Input north west northwest northnorthwest westwestwestnorth # Output for the Sample Input 0 90 45 45/2 315/4
37,283
Problem C: Usagitobi m × n マスの盀がある. i 行 j 列のマスを( i , j ) (0 ≀ i < m , 0 ≀ j < n ) で衚す. うさぎは( x , y ) にいるずき, (( x + a ) mod m , ( y + b ) mod n ) たたは(( x + c ) mod m , ( y + d ) mod n ) ぞ跳ぶこずができる. いた, うさぎが(0, 0) にいる. 䞀床跳び立ったマスぞ再び行くこずはできないずするずき, うさぎは最倧䜕回跳ぶこずができるか. Input 入力は䞀行に m , n , a , b , c , d がスペヌス区切りで䞎えられる. 1 ≀ m , n , a , b , c , d ≀ 100 000 Output うさぎが跳べる最倧回数を䞀行に出力せよ. Sample Input 1 6 6 2 2 2 4 Sample Output 1 8
37,284
Score : 400 points Problem Statement There are N mountains in a circle, called Mountain 1 , Mountain 2 , ... , Mountain N in clockwise order. N is an odd number. Between these mountains, there are N dams, called Dam 1 , Dam 2 , ... , Dam N . Dam i ( 1 \leq i \leq N ) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1 ). When Mountain i ( 1 \leq i \leq N ) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N ). One day, each of the mountains received a non-negative even number of liters of rain. As a result, Dam i ( 1 \leq i \leq N ) accumulated a total of A_i liters of water. Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem. Constraints All values in input are integers. 3 \leq N \leq 10^5-1 N is an odd number. 0 \leq A_i \leq 10^9 The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print N integers representing the number of liters of rain Mountain 1 , Mountain 2 , ... , Mountain N received, in this order. Sample Input 1 3 2 2 4 Sample Output 1 4 0 4 If we assume Mountain 1 , 2 , and 3 received 4 , 0 , and 4 liters of rain, respectively, it is consistent with this input, as follows: Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water. Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water. Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water. Sample Input 2 5 3 8 7 5 5 Sample Output 2 2 4 12 2 8 Sample Input 3 3 1000000000 1000000000 0 Sample Output 3 0 2000000000 0
37,285
Score : 400 points Problem Statement You are given a string S of length N consisting of ( and ) . Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence . Here, a correct bracket sequence is defined as follows: () is a correct bracket sequence. If X is a correct bracket sequence, the concatenation of ( , X and ) in this order is also a correct bracket sequence. If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence. Every correct bracket sequence can be derived from the rules above. Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one. Constraints The length of S is N . 1 ≀ N ≀ 100 S consists of ( and ) . Input Input is given from Standard Input in the following format: N S Output Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S . Sample Input 1 3 ()) Sample Output 1 (()) Sample Input 2 6 )))()) Sample Output 2 (((()))()) Sample Input 3 8 ))))(((( Sample Output 3 (((())))(((())))
37,286
Problem E: Hide-and-seek Hide-and-seek is a children’s game. Players hide here and there, and one player called it tries to find all the other players. Now you played it and found all the players, so it’s turn to hide from it . Since you have got tired of running around for finding players, you don’t want to play it again. So you are going to hide yourself at the place as far as possible from it . But where is that? Your task is to find the place and calculate the maximum possible distance from it to the place to hide. Input The input contains a number of test cases. The first line of each test case contains a positive integer N ( N ≀ 1000). The following N lines give the map where hide-and-seek is played. The map consists of N corridors . Each line contains four real numbers x 1 , y 1 , x 2 , and y 2 , where ( x 1 , y 1 ) and ( x 2 , y 2 ) indicate the two end points of the corridor. All corridors are straight, and their widths are negligible. After these N lines, there is a line containing two real numbers sx and sy , indicating the position of it . You can hide at an arbitrary place of any corridor, and it always walks along corridors. Numbers in the same line are separated by a single space. It is guaranteed that its starting position ( sx , sy ) is located on some corridor and linked to all corridors directly or indirectly. The end of the input is indicated by a line containing a single zero. Output For each test case, output a line containing the distance along the corridors from ‘it”s starting position to the farthest position. The value may contain an error less than or equal to 0.001. You may print any number of digits below the decimal point. Sample Input 2 0 0 3 3 0 4 3 1 1 1 0 Output for the Sample Input 4.243
37,287
Score : 1000 points Problem Statement You are given a tree with N vertices. The vertices are numbered 0 through N-1 , and the edges are numbered 1 through N-1 . Edge i connects Vertex x_i and y_i , and has a value a_i . You can perform the following operation any number of times: Choose a simple path and a non-negative integer x , then for each edge e that belongs to the path, change a_e by executing a_e ← a_e ⊕ x (⊕ denotes XOR). Your objective is to have a_e = 0 for all edges e . Find the minimum number of operations required to achieve it. Constraints 2 ≀ N ≀ 10^5 0 ≀ x_i,y_i ≀ N-1 0 ≀ a_i ≀ 15 The given graph is a tree. All input values are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 a_1 x_2 y_2 a_2 : x_{N-1} y_{N-1} a_{N-1} Output Find the minimum number of operations required to achieve the objective. Sample Input 1 5 0 1 1 0 2 3 0 3 6 3 4 4 Sample Output 1 3 The objective can be achieved in three operations, as follows: First, choose the path connecting Vertex 1, 2 , and x = 1 . Then, choose the path connecting Vertex 2, 3 , and x = 2 . Lastly, choose the path connecting Vertex 0, 4 , and x = 4 . Sample Input 2 2 1 0 0 Sample Output 2 0
37,288
Score : 200 points Problem Statement We have N weights indexed 1 to N . The mass of the weight indexed i is W_i . We will divide these weights into two groups: the weights with indices not greater than T , and those with indices greater than T , for some integer 1 \leq T < N . Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group. Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2 . Constraints 2 \leq N \leq 100 1 \leq W_i \leq 100 All values in input are integers. Input Input is given from Standard Input in the following format: N W_1 W_2 ... W_{N-1} W_N Output Print the minimum possible absolute difference of S_1 and S_2 . Sample Input 1 3 1 2 3 Sample Output 1 0 If T = 2 , S_1 = 1 + 2 = 3 and S_2 = 3 , with the absolute difference of 0 . Sample Input 2 4 1 3 1 1 Sample Output 2 2 If T = 2 , S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2 , with the absolute difference of 2 . We cannot have a smaller absolute difference. Sample Input 3 8 27 23 76 2 3 5 62 52 Sample Output 3 2
37,289
Problem G: Walking Ant Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest. The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates ( x , y ) at time t , she will be on one of the five flagstones with the following coordinates at time t + 1: ( x , y ), ( x + 1, y ), ( x - 1, y ), ( x , y + 1), ( x , y - 1). The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once. Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants. When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home. If there is a puddle on a flagstone, the ant cannot move there. Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible. Input The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format. w h d 11 d 12 d 13 ... d 1 w d 2 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 numbers of flagstones in the x - and y -directions, respectively. w and h are less than or equal to 8. The integer d yx represents the state of the flagstone with coordinates ( x , y ) as follows. 0: There is a puddle on the flagstone, and the ant cannot move there. 1, 2: Nothing exists on the flagstone, and the ant can move there. '2' indicates where the ant initially stands. 3: The hole to the nest is on the flagstone. 4: Some food is on the flagstone. There is one and only one flagstone with a hole. Not more than five flagstones have food on them. The end of the input is indicated by a line with two zeros. Integer numbers in an input line are separated by at least one space character. Output for each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time. Sample Input 3 3 2 1 1 1 1 0 1 1 3 8 4 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 8 5 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1 0 0 Output for the Sample Input 4 -1 13
37,290
Share the Ruins Preservation Two organizations International Community for Preservation of Constructions (ICPC) and Japanese Archaeologist Group (JAG) engage in ruins preservation. Recently, many ruins were found in a certain zone. The two organizations decided to share the preservation of the ruins by assigning some of the ruins to ICPC and the other ruins to JAG. Now, ICPC and JAG make a rule for assignment as follows: Draw a vertical straight line from the north to the south, avoiding to intersect ruins. Ruins located to the west of the line are preserved by ICPC. On the other hand, ruins located to the east of the line are preserved by JAG. (It is possible that no ruins are located to the east/west of the line; in this case, ICPC/JAG will preserve no ruins.) A problem is where to draw a straight line. For each organization, the way to preserve its assigned ruins is to make exactly one fence such that all the assigned ruins are in the region surrounded by the fence. Furthermore, they should minimize the length of such a fence for their budget. If the surrounded areas are vast, expensive costs will be needed to maintain the inside of areas. Therefore, they want to minimize the total preservation cost, i.e. the sum of the areas surrounded by two fences. Your task is to write a program computing the minimum sum of the areas surrounded by two fences, yielded by drawing an appropriate straight line. Input The input consists of a single test case. $N$ $x_1$ $y_1$ $x_2$ $y_2$ ... $x_N$ $y_N$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of founded ruins. The following $N$ lines represent the location of the ruins. The $i$-th line of them consists of two integers $x_i$ and $y_i$, which indicate the location of the $i$-th ruin is $x_i$ east and $y_i$ north from a certain location in the zone. You can assume the following things for the ruins: $-10^9 \leq x_i, y_i \leq 10^9$ You can ignore the sizes of ruins. That is, you can assume ruins are points. No pair of ruins has the same location. Output Print the minimum total preservation cost yielded by drawing an appropriate straight line. You should round off the cost to the nearest integer. Sample Input 1 8 -10 0 -10 5 -5 5 -5 0 10 0 10 -5 5 -5 5 0 Output for the Sample Input 1 50 Sample Input 2 5 0 0 0 1 0 2 1 0 1 1 Output for the Sample Input 2 0 Sample Input 3 6 1 5 1 6 0 5 0 -5 -1 -5 -1 -6 Output for the Sample Input 3 6 Sample Input 4 10 2 5 4 6 9 5 8 8 1 3 6 4 5 9 7 3 7 7 3 9 Output for the Sample Input 4 17
37,291
Activity Selection Problem There are $n$ acitivities with start times $\{s_i\}$ and finish times $\{t_i\}$. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person. Input $n$ $s_1$ $t_1$ $s_2$ $t_2$ : $s_n$ $t_n$ The first line consists of the integer $n$. In the following $n$ lines, the start time $s_i$ and the finish time $t_i$ of the activity $i$ are given. 出力 Print the maximum number of activities in a line. Constraints $1 \le n \le 10^5$ $1 \le s_i \lt t_i \le 10^9 (1 \le i \le n)$ Sample Input 1 5 1 2 3 9 3 5 5 9 6 8 Sample Output 1 3 Sample Input 2 3 1 5 3 4 2 5 Sample Output 2 1 Sample Input 3 3 1 2 2 3 3 4 Sample Output 3 2
37,292
Score: 400 points Problem Statement To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives. He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows: A_1 yen on the 1 -st day, A_2 yen on the 2 -nd day, ..., A_N yen on the N -th day. In the i -th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time . Buy stock: Pay A_i yen and receive one stock. Sell stock: Sell one stock for A_i yen. What is the maximum possible amount of money that M-kun can have in the end by trading optimally? Constraints 2 \leq N \leq 80 100 \leq A_i \leq 200 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N Output Print the maximum possible amount of money that M-kun can have in the end, as an integer. Sample Input 1 7 100 130 130 130 115 115 150 Sample Output 1 1685 In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows: Initially, he has 1000 yen and no stocks. Day 1 : Buy 10 stocks for 1000 yen. Now he has 0 yen. Day 2 : Sell 7 stocks for 910 yen. Now he has 910 yen. Day 3 : Sell 3 stocks for 390 yen. Now he has 1300 yen. Day 4 : Do nothing. Day 5 : Buy 1 stock for 115 yen. Now he has 1185 yen. Day 6 : Buy 10 stocks for 1150 yen. Now he has 35 yen. Day 7 : Sell 11 stocks for 1650 yen. Now he has 1685 yen. There is no way to have 1686 yen or more in the end, so the answer is 1685 . Sample Input 2 6 200 180 160 140 120 100 Sample Output 2 1000 In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen. Sample Input 3 2 157 193 Sample Output 3 1216 In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2 , after which we will have 1216 yen.
37,293
Score : 400 points Problem Statement On a planet far, far away, M languages are spoken. They are conveniently numbered 1 through M . For CODE FESTIVAL 20XX held on this planet, N participants gathered from all over the planet. The i -th (1≩i≩N) participant can speak K_i languages numbered L_{i,1}, L_{i,2}, ..., L_{i,{}K_i} . Two participants A and B can communicate with each other if and only if one of the following conditions is satisfied: There exists a language that both A and B can speak. There exists a participant X that both A and B can communicate with. Determine whether all N participants can communicate with all other participants. Constraints 2≩N≩10^5 1≩M≩10^5 1≩K_i≩M ( The sum of all K_i)≩10^5 1≩L_{i,j}≩M L_{i,1}, L_{i,2}, ..., L_{i,{}K_i} are pairwise distinct. Partial Score 200 points will be awarded for passing the test set satisfying the following: N≩1000 , M≩1000 and ( The sum of all K_i)≩1000 . Additional 200 points will be awarded for passing the test set without additional constraints. Input The input is given from Standard Input in the following format: N M K_1 L_{1,1} L_{1,2} ... L_{1,{}K_1} K_2 L_{2,1} L_{2,2} ... L_{2,{}K_2} : K_N L_{N,1} L_{N,2} ... L_{N,{}K_N} Output If all N participants can communicate with all other participants, print YES . Otherwise, print NO . Sample Input 1 4 6 3 1 2 3 2 4 2 2 4 6 1 6 Sample Output 1 YES Any two participants can communicate with each other, as follows: Participants 1 and 2 : both can speak language 2 . Participants 2 and 3 : both can speak language 4 . Participants 1 and 3 : both can communicate with participant 2 . Participants 3 and 4 : both can speak language 6 . Participants 2 and 4 : both can communicate with participant 3 . Participants 1 and 4 : both can communicate with participant 2 . Note that there can be languages spoken by no participant. Sample Input 2 4 4 2 1 2 2 1 2 1 3 2 4 3 Sample Output 2 NO For example, participants 1 and 3 cannot communicate with each other.
37,294
Knapsack Problem You have N kinds of items that you want to put them into a knapsack. Item i has value v i and weight w i . You want to find a subset of items to put such that: The total value of the items is as large as possible. The items have combined weight at most W , that is capacity of the knapsack. You can select as many items as possible into a knapsack for each kind. Find the maximum total value of items in the knapsack. Input N W v 1 w 1 v 2 w 2 : v N w N The first line consists of the integers N and W . In the following lines, the value and weight of the i -th item are given. Output Print the maximum total values of the items in a line. Constraints 1 ≀ N ≀ 100 1 ≀ v i ≀ 1000 1 ≀ w i ≀ 1000 1 ≀ W ≀ 10000 Sample Input 1 4 8 4 2 5 2 2 1 8 3 Sample Output 1 21 Sample Input 2 2 20 5 9 4 10 Sample Output 2 10 Sample Input 3 3 9 2 1 3 1 5 2 Sample Output 3 27
37,295
Score : 600 points Problem Statement For a non-negative integer K , we define a fractal of level K as follows: A fractal of level 0 is a grid with just one white square. When K > 0 , a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids: The central subgrid consists of only black squares. Each of the other eight subgrids is a fractal of level K-1 . For example, a fractal of level 2 is as follows: In a fractal of level 30 , let (r, c) denote the square at the r -th row from the top and the c -th column from the left. You are given Q quadruples of integers (a_i, b_i, c_i, d_i) . For each quadruple, find the distance from (a_i, b_i) to (c_i, d_i) . Here the distance from (a, b) to (c, d) is the minimum integer n that satisfies the following condition: There exists a sequence of white squares (x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions: (x_0, y_0) = (a, b) (x_n, y_n) = (c, d) For every i (0 \leq i \leq n-1) , (x_i, y_i) and (x_{i+1}, y_{i+1}) share a side. Constraints 1 \leq Q \leq 10000 1 \leq a_i, b_i, c_i, d_i \leq 3^{30} (a_i, b_i) \neq (c_i, d_i) (a_i, b_i) and (c_i, d_i) are white squares. All values in input are integers. Input Input is given from Standard Input in the following format: Q a_1 \ b_1 \ c_1 \ d_1 : a_Q \ b_Q \ c_Q \ d_Q Output Print Q lines. The i -th line should contain the distance from (a_i, b_i) to (c_i, d_i) . Sample Input 1 2 4 2 7 4 9 9 1 9 Sample Output 1 5 8
37,296
E: 28 問題文 トランプゲヌムの倧富豪においおランクが $2$, $8$ のカヌドは匷力です そこで$10$ 進数衚蚘で数字の $2$, $8$ のみからなる敎数を 良い敎数 ず呌ぶこずにしたす 良い敎数を小さいものから列挙するず $2, 8, 22, 28, 82, 88, \cdots$ ずなりたす $n$ を正の敎数ずしたす$n$ が良い敎数の積の圢で衚珟できるずき 最倧でいく぀の積になるか求めおくださいできないなら $-1$ ず出力しおください 入力 $n$ 制玄 $1 \leq n \leq 10^{18}$ 出力 答えを $1$ 行で出力しおください サンプル サンプル入力1 1 サンプル出力1 -1 サンプル入力2 2 サンプル出力2 1 サンプル入力3 88 サンプル出力3 3 $2 \times 2 \times 22$ ず衚せたす サンプル入力4 100 サンプル出力4 -1 サンプル入力5 173553147234869248 サンプル出力5 11 $2^6 \times 28 \times 2222^3 \times 8828$ ず衚せたす
37,297
芪方の絊料蚈算 ワシはパむプ぀なぎ組合の芪方じゃ。職人を工事珟堎に掟遣し、珟堎でパむプを぀ながせおおる。去幎は工事が増えお倧儲けするかず思ったのじゃが、ちょっず絊料の出し方がたずくおのぅ。りチずしおは倧赀字になっおしたったのじゃよ 。そこで、今幎は職人たちぞの絊料の出し方を工倫したいのじゃ。 職人たちの絊料は、工事の皮類ずこなした回数で決めおおる。぀たり、 職人の絊料  皮類 1 の単䟡 × 皮類 1 をこなした回数 + 皮類 2 の単䟡 × 皮類 2 をこなした回数 .... + 皮類 M の単䟡 × 皮類 M をこなした回数 ずなるのじゃ。これを蚈算するために、りチでは、どの職人がどの皮類の工事を䜕回こなしたかを次のような衚に蚘録しおおる。 䟋えば、䞊の衚では、職人が工事を回、工事を回こなしたこずを瀺しおおる。 職人たちがこなした仕事の回数はもう倉えられんが、や぀らは工事の単䟡を知らんので、単䟡をいろいろず倉えながら皆の䞍満が出ぬよう絊料を調敎しようず思うおおる。じゃが、ワシがこしらえたプログラムが今もっお動かなくおのぅ。ちょっずみおくれんかね。 //省略 int i, j; for ( i = 0; i < N; i++ ){ c[i] = 0; for ( j = 0; j < M; j++ ){ c[i] = c[i] + a[i][j]*b[j]; } } //省略 N は職人の数で M は工事の皮類の数じゃ。倉数 a[i][j] に職人iが工事 j をこなした回数を、 b[j] に工事 j の単䟡をいれお、 c[i] に職人 i の絊料を栌玍しおおる。合っおいるはずなのに、うんずもすんずも蚀わんそろそろ今幎の絊料を職人たちに払わないずたずいのじゃが・・・・・なんずかならんかのぅ。 それでは、職人のこなした仕事の回数ず各工事の単䟡の情報が䞎えられたずき、各職人の絊料を蚈算するプログラムを䜜成しおください。 入力 入力は1぀のデヌタセットからなる。入力デヌタは以䞋の圢匏で䞎えられる。 N M s 1 t 1 e 1 s 2 t 2 e 2 : 0 0 0 L b 11 b 12 ... b 1M b 21 b 22 ... b 2M : b L1 b L2 ... b LM 1行目は職人の数 N (1 ≀ N ≀ 10000)ず工事の皮類の数 M (1 ≀ M ≀ 10000)。 続いお、工事の蚘録ずしお、職人の番号 s i (1 ≀ s i ≀ N ) ず工事の皮類の番号 t i (1 ≀ t i ≀ M )、および職人 s i が工事 t i をこなした回数 e i (1 ≀ e i ≀ 50000) からなる行が行以䞊䞎えられる。工事の蚘録はれロ぀の行で終わる。ただし、 e i の合蚈は 1 以䞊 50000 以䞋である。たた、工事の蚘録には、どの職人ず工事の皮類の組も床以䞊珟れない。工事をこなした回数が 0 である職人ず工事の皮類の組は䞎えられない。 続く 1 行は絊料の算出を行う回数 L (1 ≀ L ≀ 100) 。 続く L 行は、 i 回目の絊料の算出に必芁な、工事 j の単䟡 b ij (0 ≀ b ij ≀ 10000) の䞊び。 出力 以䞋の圢匏で、 i 回目の絊料の算出によっお埗られた職人 j の絊料 c ij を順番に出力する。各絊料の間は空癜぀で区切る。 c 11 c 12 ... c 1N c 21 c 22 ... c 2N : c L1 c L2 ... c LN 入出力䟋 入力䟋 4 6 1 2 1 2 3 2 2 4 3 3 1 5 4 2 4 4 3 2 4 6 10 0 0 0 3 1 3 2 4 0 10 6 5 4 3 2 1 5 1 1 5 5 1 出力䟋 3 16 5 116 5 17 30 38 1 17 25 16
37,299
Score : 100 points Problem Statement In your garden, there is a long and narrow flowerbed that stretches infinitely to the east. You have decided to plant N kinds of flowers in this empty flowerbed. For convenience, we will call these N kinds of flowers Flower 1, 2, 
, N . Also, we will call the position that is p centimeters from the west end of the flowerbed Position p . You will plant Flower i (1 ≀ i ≀ N) as follows: first, plant one at Position w_i , then plant one every d_i centimeters endlessly toward the east. That is, Flower i will be planted at the positions w_i, w_i + d_i, w_i + 2 d_i, 
 Note that more than one flower may be planted at the same position. Find the position at which the K -th flower from the west is planted. If more than one flower is planted at the same position, they are counted individually. Constraints 1 ≀ N ≀ 10^5 1 ≀ K ≀ 10^9 1 ≀ w_i ≀ 10^{18} 1 ≀ d_i ≀ 10^9 All input values are integers. Input Input is given from Standard Input in the following format: N K w_1 d_1 : w_N d_N Output When the K -th flower from the west is planted at Position X , print the value of X . (The westmost flower is counted as the 1 -st flower.) Sample Input 1 2 6 20 10 25 15 Sample Output 1 50 Two kinds of flowers are planted at the following positions: Flower 1 : Position 20, 30, 40, 50, 60, 
 Flower 2 : Position 25, 40, 55, 70, 85, 
 The sixth flower from the west is the Flower 1 planted at Position 50 . Note that the two flowers planted at Position 40 are counted individually. Sample Input 2 3 9 10 10 10 10 10 10 Sample Output 2 30 Three flowers are planted at each of the positions 10, 20, 30, 
 Thus, the ninth flower from the west is planted at Position 30 . Sample Input 3 1 1000000000 1000000000000000000 1000000000 Sample Output 3 1999999999000000000
37,300
Score : 300 points Problem Statement There is a grid with H rows and W columns, where each square is painted black or white. You are given H strings S_1, S_2, ..., S_H , each of length W . If the square at the i -th row from the top and the j -th column from the left is painted black, the j -th character in the string S_i is # ; if that square is painted white, the j -th character in the string S_i is . . Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition: There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white... Constraints 1 \leq H, W \leq 400 |S_i| = W ( 1 \leq i \leq H ) For each i ( 1 \leq i \leq H ), the string S_i consists of characters # and . . Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output Print the answer. Sample Input 1 3 3 .#. ..# #.. Sample Output 1 10 Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)) , where (i, j) denotes the square at the i -th row from the top and the j -th column from the left. Sample Input 2 2 4 .... .... Sample Output 2 0 Sample Input 3 4 3 ### ### ... ### Sample Output 3 6
37,301
Problem E: HullMarathon うさぎはフルマラ゜ンずいう競技が奜きである. この競技はチヌムで行う. チヌムメンバヌは競技開始前に原点に集たる. 競技開始ず同時に走り出し, 1 分埌に立ち止たるこのずきチヌムメンバヌの䜍眮の凞包の面積が最も倧きなチヌムが勝ちずなる. あなたは $N$ 匹のうさぎからなるチヌムの監督である. $i$ 匹目のうさぎは 1 分で $r_i$ 移動するこずができる. このチヌムが最適な戊略をずった堎合の, 1 分埌の凞包の面積の最倧倀を求めよ. Constraints $N$ will be between 3 and 8, inclusive. $r_i$ will be an integer between 1 and 1,000, inclusive. Input 入力は以䞋の圢匏で䞎えられる: $N$ $r_1$ ... $r_N$ Output 凞包の面積の最倧倀を衚す実数を 1 行に出力せよ. 小数点以䞋䜕桁出力しおもよいが, 絶察誀差たたは盞察誀差が $10^{-6}$ 以䞋のずき Accepted になる. Sample Input 1 4 5 8 58 85 Sample Output 1 2970.000000000 Sample Input 2 6 1 1 1 1 1 1 Sample Output 2 2.598076211
37,302
ダヌツ 問題 あなたは以䞋のルヌルでダヌツゲヌムをするこずになった. あなたは,矢を的(たず)に向かっお 4 本たで投げるこずができる.必ずしも 4 本党おを投げる必芁はなく,1 本も投げなくおもかたわない.的は N 個の郚分に区切られおいお,各々の郚分に点数 P 1 ,..., P N が曞か れおいる.矢が刺さった堎所の点数の合蚈 S があなたの埗点の基瀎ずなる.S があらかじめ決められたある点数 M 以䞋の堎合は S がそのたたあなたの埗点ずなる.しかし,S が M を超えた堎合はあなたの埗点は 0 点ずなる. 的に曞かれおいる点数ず M の倀が䞎えられたずき,あなたが埗るこずのできる点数の最倧倀を求めるプログラムを䜜成せよ. 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる 1 行目には,敎数 N (1 ≀ N ≀ 1000) ず M (1 ≀ M ≀ 200000000 = 2 × 10 8 )がこの順に空癜区切りで曞かれおいる.2 行目以降の第 1 + i 行目 (1 ≀ i ≀ N ) には, P i (1 ≀ P i ≀ 100000000 = 10 8 ) が曞かれおいる. 採点甚デヌタのうち, 配点の 20% 分は N ≀ 100 を満たし,配点の 50% 分はN ≀ 300 を満たす. N, M がずもに 0 のずき入力の終了を瀺す. デヌタセットの数は 10 を超えない 出力 デヌタセットごずにあなたが埗るこずのできる点数の最倧倀を行に出力する. 入出力䟋 入力䟋 4 50 3 14 15 9 3 21 16 11 2 0 0 出力䟋 48 20 1぀目の䟋では,15 点の郚分に 3 本,3 点の郚分に 1 本の矢が刺さった堎合にあなたの埗点は最倧になり,その埗点は 48 点である. 2぀目の䟋では,16 点の堎所に 1 本,2 点の堎所に 2 本の矢が刺さった堎合にあなたの埗点は最倧になり,その埗点は 20 点である. 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,303
Bichrome Tree Connectivity 朚が䞎えられたす。 はじめ、すべおの頂点は癜いです。 癜い頂点の色を反転するず黒になり、黒い頂点の色を反転するず癜になりたす。 二皮類のク゚リを凊理しおください。 䞀皮類目のク゚リでは、頂点 v の色を反転したす。 二皮類目のク゚リでは、癜い頂点 v から癜い頂点ずそれらを結ぶ蟺だけを䜿っおたどり着ける頂点の個数を答えたす。 入力 N Q a_1 b_1 a_2 b_2 : a_{n-1} b_{n-1} t_1 v_1 t_2 v_2 : t_q v_q t_i が 1 のずき䞀皮類目のク゚リ、 2 のずき二皮類目のク゚リであるこずを衚す。 出力 ans_1 ans_2 : ans_k 二皮類目のク゚リに察する答えを順に出力せよ。 制玄 1 \leq N,Q \leq 10^5 1 \leq a_i,b_i \leq N 1 \leq t_i \leq 2 1 \leq v_i \leq N 䞎えられるグラフは朚である。 t_i=2 のずき、頂点 v_i は必ず癜である。 入力䟋 10 3 1 2 2 5 2 6 1 4 1 3 3 7 3 8 3 9 9 10 1 3 2 1 2 8 出力䟋 5 1
37,304
Digit Number Write a program which computes the digit number of sum of two integers a and b . Input There are several test cases. Each test case consists of two non-negative integers a and b which are separeted by a space in a line. The input terminates with EOF. Constraints 0 ≀ a , b ≀ 1,000,000 The number of datasets ≀ 200 Output Print the number of digits of a + b for each data set. Sample Input 5 7 1 99 1000 999 Output for the Sample Input 2 3 4
37,305
Problem J: Castle Wall A new lord assumed the position by the death of the previous lord in a Far Eastern province. The new greedy lord hates concave polygons, because he believes they need much wasted area to be drawn on paper. He always wants to modify them to convex ones. His castle is currently surrounded by a wall forming a concave polygon, when seen from the above. Of course he hates it. He believes more area could be obtained with a wall of a convex polygon. Thus he has ordered his vassals to have new walls built so they form a convex polygon. Unfortunately, there is a limit in the budget. So it might be infeasible to have the new walls built completely. The vassals has found out that only up to r meters of walls in total can be built within the budget. In addition, the new walls must be built in such a way they connect the polygonal vertices of the present castle wall. It is impossible to build both of intersecting walls. After long persuasion of the vassals, the new lord has reluctantly accepted that the new walls might not be built completely. However, the vassals still want to maximize the area enclosed with the present and new castle walls, so they can satisfy the lord as much as possible. Your job is to write a program to calculate, for a given integer r , the maximum possible area of the castle with the new walls. Input The input file contains several test cases. Each case begins with a line containing two positive integers n and r . n is the number of vertices of the concave polygon that describes the present castle wall, satisfying 5 ≀ n ≀ 64. r is the maximum total length of new castle walls feasible within the budget, satisfying 0 ≀ r ≀ 400. The subsequent n lines are the x - and y -coordinates of the n vertices. The line segments ( x i , y i ) - ( x i +1 , y i +1 ) (1 ≀ i ≀ n - 1) and ( x n , y n ) - ( x 1 , y 1 ) form the present castle wall of the concave polygon. Those coordinates are given in meters and in the counterclockwise order of the vertices. All coordinate values are integers between 0 and 100, inclusive. You can assume that the concave polygon is simple, that is, the present castle wall never crosses or touches itself. The last test case is followed by a line containing two zeros. Output For each test case in the input, print the case number (beginning with 1) and the maximum possible area enclosed with the present and new castle walls. The area should be printed with exactly one fractional digit. Sample Input 5 4 0 0 4 0 4 4 2 2 0 4 8 80 45 41 70 31 86 61 72 64 80 79 40 80 8 94 28 22 0 0 Output for the Sample Input case 1: 16.0 case 2: 3375.0
37,306
Score : 2000 points Problem Statement For each of the K^{NM} ways to write an integer between 1 and K (inclusive) in every square in a square grid with N rows and M columns, find the value defined below, then compute the sum of all those K^{NM} values, modulo D . For each of the NM squares, find the minimum among the N+M-1 integers written in the square's row or the square's column. The value defined for the grid is the product of all these NM values. Constraints 1 \leq N,M,K \leq 100 10^8 \leq D \leq 10^9 N,M,K, and D are integers. D is prime. Input Input is given from Standard Input in the following format: N M K D Output Print the sum of the K^{NM} values, modulo D . Sample Input 1 2 2 2 998244353 Sample Output 1 35 We have 1 way to write integers such that the product of the NM values is 16 , 4 ways such that the product is 2 , and 11 ways such that the product is 1 . Sample Input 2 2 3 4 998244353 Sample Output 2 127090 Sample Input 3 31 41 59 998244353 Sample Output 3 827794103
37,307
Score : 700 points Problem Statement Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order. Iroha is looking for X,Y,Z -Haiku (defined below) in integer sequences. Consider all integer sequences of length N whose elements are between 1 and 10 , inclusive. Out of those 10^N sequences, how many contain an X,Y,Z -Haiku? Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z -Haiku if and only if there exist four indices x, y, z, w (0 ≩ x < y < z < w ≩ N) such that all of the following are satisfied: a_x + a_{x+1} + ... + a_{y-1} = X a_y + a_{y+1} + ... + a_{z-1} = Y a_z + a_{z+1} + ... + a_{w-1} = Z Since the answer can be extremely large, print the number modulo 10^9+7 . Constraints 3 ≩ N ≩ 40 1 ≩ X ≩ 5 1 ≩ Y ≩ 7 1 ≩ Z ≩ 5 Input The input is given from Standard Input in the following format: N X Y Z Output Print the number of the sequences that contain an X,Y,Z -Haiku, modulo 10^9+7 . Sample Input 1 3 5 7 5 Sample Output 1 1 Here, the only sequence that contains a 5,7,5 -Haiku is [5, 7, 5] . Sample Input 2 4 5 7 5 Sample Output 2 34 Sample Input 3 37 4 2 3 Sample Output 3 863912418 Sample Input 4 40 5 7 5 Sample Output 4 562805100
37,308
Score : 1300 points Problem Statement This is an interactive task. AtCoDeer the deer came across N people. For convenience, the people are numbered 0 through N-1 . Among them, A are honest and the remaining B(=N-A) are unkind . All of these N people know who are honest and who are unkind, but AtCoDeer only knows that there are A honest and B unkind people. He is trying to identify all of the honest people by asking questions to these N people. For one question, AtCoDeer selects a and b (0≀a,b≀N-1) , and asks person a the following question: "Is person b honest?" An honest person will always answer correctly by "Yes" or "No". An unkind person, however, will answer by selecting "Yes" or "No" arbitrarily . That is, the algorithm used by an unkind person may not be simple one such as always lying or giving random fifty-fifty answers. AtCoDeer can ask at most 2N questions. He will ask questions one by one, and the responses to the previous questions can be used when deciding the next question to ask. Identify all of the honest people. If it is impossible (more formally, if, for any strategy of asking 2N questions, there exists a strategy for unkind people to answer the questions so that there are two or more possible sets of the honest people), report that fact. Constraints 1≀A,B≀2000 Input and Output First, A and B are given from Standard Input in the following format: A B If identifying the honest people is impossible, the program must immediately print the following output and terminate itself: Impossible Otherwise, the program shall ask questions. Each question must be written to Standard Output in the following format: ? a b Here, a and b must be integers between 0 and N-1 (inclusive). The response to the question will be given from Standard Input in the following format: ans Here, ans is either Y or N . Y represents "Yes"; N represents "No". Finally, the answer must be written to Standard Output in the following format: ! s_0s_1...s_{N-1} Here, s_i must be 1 if person i is honest, and 0 if person i is unkind. Judgement 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 ). Samples In the following sample, A = 2 , B = 1 , and the answer is 101 . Input Output 2 1 ? 0 1 N ? 0 2 Y ? 1 0 Y ? 2 0 Y ? 2 2 Y ! 101 In the following sample, A = 1 , B = 2 , and the answer is Impossible . Input Output 1 2 Impossible
37,309
Problem H: ASCII Expression Mathematical expressions appearing in old papers and old technical articles are printed with typewriter in several lines, where a fixed-width or monospaced font is required to print characters (digits, symbols and spaces). Let us consider the following mathematical expression. It is printed in the following four lines: 4 2 ( 1 - ---- ) * - 5 + 6 2 3 where “- 5” indicates unary minus followed by 5. We call such an expression of lines “ASCII expression”. For helping those who want to evaluate ASCII expressions obtained through optical character recognition (OCR) from old papers, your job is to write a program that recognizes the structure of ASCII expressions and computes their values. For the sake of simplicity, you may assume that ASCII expressions are constructed by the following rules. Its syntax is shown in Table H.1. (1) Terminal symbols are ‘ 0 ’, ‘ 1 ’, ‘ 2 ’, ‘ 3 ’, ‘ 4 ’, ‘ 5 ’, ‘ 6 ’, ‘ 7 ’, ‘ 8 ’, ‘ 9 ’, ‘ + ’, ‘ - ’, ‘ * ’, ‘ ( ’, ‘ ) ’, and ‘ ’. (2) Nonterminal symbols are expr , term , factor , powexpr , primary , fraction and digit . The start symbol is expr . (3) A “cell” is a rectangular region of characters that corresponds to a terminal or nonterminal symbol (Figure H.1). In the cell, there are no redundant rows and columns that consist only of space characters. A cell corresponding to a terminal symbol consists of a single character. A cell corresponding to a nonterminal symbol contains cell(s) corresponding to its descendant(s) but never partially overlaps others. (4) Each cell has a base-line, a top-line, and a bottom-line. The base-lines of child cells of the right-hand side of rules I, II, III, and V should be aligned. Their vertical position defines the base-line position of their left-hand side cell. Table H.1: Rules for constructing ASCII expressions (similar to Backus-Naur Form) The box indicates the cell of the terminal or nonterminal symbol that corresponds to a rectan- gular region of characters. Note that each syntactically-needed space character is explicitly indicated by the period character denoted by, here. (5) powexpr consists of a primary and an optional digit . The digit is placed one line above the base-line of the primary cell. They are horizontally adjacent to each other. The base-line of a powexpr is that of the primary . (6) fraction is indicated by three or more consecutive hyphens called “vinculum”. Its dividend expr is placed just above the vinculum, and its divisor expr is placed just beneath it. The number of the hyphens of the vinculum, denoted by w h , is equal to 2 + max( w 1 , w 2 ), where w 1 and w 2 indicate the width of the cell of the dividend and that of the divisor, respectively. These cells are centered, where there are ⌈( w h − w k )/2⌉ space characters to the left and ⌊( w h − w k )/2⌋ space characters to the right, ( k = 1, 2). The base-line of a fraction is at the position of the vinculum. (7) digit consists of one character. For example, the negative fraction is represented in three lines: 3 - --- 4 where the left-most hyphen means a unary minus operator. One space character is required between the unary minus and the vinculum of the fraction. The fraction is represented in four lines: 3 + 4 * - 2 ------------- 2 - 1 - 2 where the widths of the cells of the dividend and divisor are 11 and 8 respectively. Hence the number of hyphens of the vinculum is 2 + max(11, 8) = 13. The divisor is centered by ⌈(13−8)/2⌉ = 3 space characters (hyphens) to the left and ⌊(13−8)/2⌋ = 2 to the right. The powexpr (4 2 ) 3 is represented in two lines: 2 3 ( 4 ) where the cell for 2 is placed one line above the base-line of the cell for 4, and the cell for 3 is placed one line above the base-line of the cell for a primary (4 2 ). Input The input consists of multiple datasets, followed by a line containing a zero. Each dataset has the following format. n str 1 str 2 . . . str n n is a positive integer, which indicates the number of the following lines with the same length that represent the cell of an ASCII expression. str k is the k -th line of the cell where each space character is replaced with a period. You may assume that n ≀ 20 and that the length of the lines is no more than 80. Output For each dataset, one line containing a non-negative integer less than 2011 should be output. The integer indicates the value of the ASCII expression in modular arithmetic under modulo 2011. The output should not contain any other characters. There is no fraction with the divisor that is equal to zero or a multiple of 2011. Note that powexpr x 0 is defined as 1, and x y (y is a positive integer) is defined as the product x×x× ...× x where the number of x 's is equal to y . A fraction is computed as the multiplication of x and the inverse of y , i.e., x × inv( y ), under y modulo 2011. The inverse of y (1 ≀ y < 2011) is uniquely defined as the integer z (1 ≀ z < 2011) that satisfies z × y ≡ 1 (mod 2011), since 2011 is a prime number. Sample Input 4 ........4...2.......... (.1.-.----.)..*.-.5.+.6 ........2.............. .......3............... 3 ...3. -.--- ...4. 4 .3.+.4.*.-.2. ------------- ..........2.. ...-.1.-.2... 2 ...2..3 (.4..). 1 2.+.3.*.5.-.7.+.9 1 (.2.+.3.).*.(.5.-.7.).+.9 3 .2....3. 4..+.--- ......5. 3 .2......-.-.3. 4..-.-.------- ..........5... 9 ............1............ ------------------------- ..............1.......... .1.+.-------------------. ................1........ ......1.+.-------------.. ..................1...... ...........1.+.-------... ................1.+.2.... 15 .................2...... ................---..... .......2.........5....3. .(.---------.+.-----.).. .....7...........3...... ....---.+.1............. .....4.................. ------------------------ .......2................ ......---............... .......5.......2....2... ...(.-----.+.-----.).... .......3.......3........ ..............---....... ...............4........ 2 .0....2.... 3..+.4..*.5 20 ............2............................2...................................... ...........3............................3....................................... ..........----.........................----..................................... ............4............................4...................................... .....2.+.------.+.1...............2.+.------.+.1................................ ............2............................2...................................... ...........2............................2........................2.............. ..........----.........................----.....................3............... ............2............................2.....................----............. ...........3............................3........................4.............. (.(.----------------.+.2.+.3.).*.----------------.+.2.).*.2.+.------.+.1.+.2.*.5 ............2............................2.......................2.............. ...........5............................5.......................2............... ..........----.........................----....................----............. ............6............................6.......................2.............. .........------.......................------....................3............... ............3............................3...................................... ..........----.........................----..................................... ............2............................2...................................... ...........7............................7....................................... 0 Output for the Sample Input 501 502 1 74 19 2010 821 821 1646 148 81 1933
37,310
A: Grid Mori / グリッド森 ずある富豪の森さんがグリッド状に区分けされた土地の n 区画を買っお 4぀の工堎 A, B, C, D を建おようずしおいる たず土地党䜓の幅 w を1以䞊 n 以䞋の倀で決定する この土地の 1 番巊䞊の区画を (0, 0) ずする そしお (0, 0) の区画を1番目に (1, 0) の区画を2番目に賌入する このように i 番目に ( x , y ) の区画を賌入した埌に i + 1 番目に ( x + 1, y ) の区画を賌入しおいく もし ( w , y ) の区画を賌入しようずしたずきは 代わりに (0, y + 1) の区画を賌入する さらに森さんは土地を賌入する前に占い垫に次のようなこずを蚀われおいた 「 a 番目に賌入するグリッドの土地に工堎Aを b 番目に賌入するグリッドに工堎Bを c 番目のグリッドに工堎Cを d 番目のグリッドに工堎Dを建おるずよいぞ」 森さんは非垞に占い奜きであり必ず占い垫の助蚀を守りたいず考えおいる しかしAずBCずDの2぀のペアは関連のある工堎であるためできるだけ近くにあった方がよい 自分の土地の買い方ず占い垫の助蚀を䞡方守り぀぀関連する工堎同士をなるべく近くしたいのである ここで2぀の土地が ( x 1 , y 1 )( x 2 , y 2 ) にあるずき 距離は| x 1 - x 2 |+ | y 1 - y 2 | で求めるこずができる | x | は x の絶察倀を衚しおいる あなたの仕事は (工堎AずBの距離)  (工堎CずDの距離) の倀が最小になるように土地を賌入したずきの距離の合蚈を求めるプログラムを䜜成するこずである 図1はサンプルの1番目におけるすべおの工堎の配眮の組み合わせである幅3で土地を賌入したずき距離の合蚈が最小ずなる 図1: サンプル1の工堎の配眮 Input 入力は次の圢匏で衚される n a b c d 入力は党お敎数倀である n (4 ≩ n ≩ 100) はこれから賌入する土地のグリッド数を衚す a , b , c , d (1 ≩ a , b , c , d ≩ n ) はそれぞれ工堎A, B, C, Dを䜕番目に賌入したグリッドの土地に建おるかを衚す このずき a , b , c , d は党お異なる数が入力される Output 森さんが a, b 間ず c, d 間の距離の合蚈が最小ずなるように土地を賌入したずきの合蚈倀を1行で出力せよ Sample Input 1 5 1 4 2 5 Sample Output 1 2 Sample Input 2 16 2 9 14 8 Sample Output 2 3
37,311
Problem D: 単䜍倉換噚 囜際単䜍系(SI)では、kilo, mega, giga などの接頭蟞を䜿っお、「数倀 + 接頭蟞 + 単䜍」ずいう圢でさたざたな物理量を衚蚘する。たずえば、「3.5 kilometers」, 「5.1 milligrams」 ずいった具合である。 䞀方、これらの物理量は指数衚蚘を䜿っお「3.5 * 10^3 meters」、「5.1 * 10^-3 grams」のように衚すこずも可胜である。 物理量の枬定は必ず誀差を含む。そこで、枬定された物理量がどれだけの粟床を持぀かを衚すために、有効数字ずいう抂念がある。有効数字を考慮した衚蚘では、最埌の桁は誀差を含みうるが、それ以倖の桁は信頌できるものず考える。たずえば、「1.23」 ず衚蚘した堎合、真の倀は 1.225 以䞊 1.235 未満であり、小数第1䜍以䞊の桁は信頌できるが、小数第2䜍には誀差を含む。このように「信頌できる桁 + 誀差を含む1桁」ずいう圢で物理量を衚したずきの桁数を有効桁数ず呌ぶ。たずえば、「1.23」の有効桁数は3桁である。 0でない最䞊䜍の桁より前に0がある堎合、その0は有効桁数には含めない。たずえば、「0.45」の有効桁数は2桁である。 0でない最䞋䜍の桁より埌に0がある堎合、その0を有効桁数に含むかどうかは小数点の䜍眮により異なる。小数点より右たで0があるずきは、その0は有効桁数に含める。たずえば、「12.300」の有効桁数は5桁である。䞀方、「12300」のように、小数点より右に0がないずきは、右偎の0を有効桁数に含めるかどうかは明確ではないが、この問題においおは含めるこずずする。すなわち、「12300」の有効桁数は5桁である。 な぀めは孊校の課題で物理の問題を出された。その䞭で有効数字や単䜍が関わる蚈算をしなければならないのだが、困ったこずにな぀めには有効数字や単䜍の䜿い方がただよくわかっおいない。そこで、自動でそれらの蚈算をするプログラムを䜜っお、な぀めを助けおあげおほしい。 あなたが曞くのは、接頭蟞を䜿った衚蚘が䞎えられたずき、それを有効桁数が等しい指数衚蚘に倉換するプログラムである。䜿われる接頭蟞は以䞋の20個である。 yotta = 10^24 zetta = 10^21 exa = 10^18 peta = 10^15 tera = 10^12 giga = 10^9 mega = 10^6 kilo = 10^3 hecto = 10^2 deca = 10^1 deci = 10^-1 centi = 10^-2 milli = 10^-3 micro = 10^-6 nano = 10^-9 pico = 10^-12 femto = 10^-15 ato = 10^-18 zepto = 10^-21 yocto = 10^-24 Input 入力は1行のみからなり、その唯䞀の行には、数倀、もしあれば単䜍接頭蟞、単䜍が含たれおいる。それぞれの間は空癜1個で区切られおいる。単䜍接頭蟞がない堎合もあり、その堎合は数倀ず単䜍のみが行に含たれる。単䜍接頭蟞ず同じ名前の単䜍は珟れない。小数が䞎えられる堎合の1の䜍を陀き、最䞊䜍の桁が0になるこずはない。䞎えられる数倀は正である。䞎えられる数倀は小数点を含め1000桁以䞋で、単䜍の名前は50文字以䞋である。 Output a * 10^ b [単䜍] の圢で指数衚蚘で衚珟された量を出力せよ。ただし、1 <= a < 10 である。単䜍名の単数圢・耇数圢の違いは考慮しない。入力に䞎えられた単䜍名をそのたた出力せよ。 Notes on Submission 䞊蚘圢匏で耇数のデヌタセットが䞎えられたす。入力デヌタの 1 行目にデヌタセットの数が䞎えられたす。各デヌタセットに察する出力を䞊蚘圢匏で順番に出力するプログラムを䜜成しお䞋さい。 Sample Input 7 12.3 kilo meters 0.45 mega watts 0.000000000000000000000001 yotta grams 1000000000000000000000000 yocto seconds 42 amperes 0.42 joules 1234.56789012345678901234567890 hecto pascals Output for the Sample Input 1.23 * 10^4 meters 4.5 * 10^5 watts 1 * 10^0 grams 1.000000000000000000000000 * 10^0 seconds 4.2 * 10^1 amperes 4.2 * 10^-1 joules 1.23456789012345678901234567890 * 10^5 pascals
37,312
Score : 400 points Problem Statement The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions: 1 ≀ i < j < k ≀ |T| ( |T| is the length of T .) T_i = A ( T_i is the i -th character of T from the beginning.) T_j = B T_k = C For example, when T = ABCBC , there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5) . Thus, the ABC number of T is 3 . You are given a string S . Each character of S is A , B , C or ? . Let Q be the number of occurrences of ? in S . We can make 3^Q strings by replacing each occurrence of ? in S with A , B or C . Find the sum of the ABC numbers of all these strings. This sum can be extremely large, so print the sum modulo 10^9 + 7 . Constraints 3 ≀ |S| ≀ 10^5 Each character of S is A , B , C or ? . Input Input is given from Standard Input in the following format: S Output Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7 . Sample Input 1 A??C Sample Output 1 8 In this case, Q = 2 , and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A , B or C . The ABC number of each of these strings is as follows: AAAC : 0 AABC : 2 AACC : 0 ABAC : 1 ABBC : 2 ABCC : 2 ACAC : 0 ACBC : 1 ACCC : 0 The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8 , so we print 8 modulo 10^9 + 7 , that is, 8 . Sample Input 2 ABCBC Sample Output 2 3 When Q = 0 , we print the ABC number of S itself, modulo 10^9 + 7 . This string is the same as the one given as an example in the problem statement, and its ABC number is 3 . Sample Input 3 ????C?????B??????A??????? Sample Output 3 979596887 In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924 , and we should print this number modulo 10^9 + 7 , that is, 979596887 .
37,313
Problem I: Shy Polygons You are given two solid polygons and their positions on the xy -plane. You can move one of the two along the x -axis (they can overlap during the move). You cannot move it in other directions. The goal is to place them as compactly as possible, subject to the following condition: the distance between any point in one polygon and any point in the other must not be smaller than a given minimum distance L . We define the width of a placement as the difference between the maximum and the minimum x -coordinates of all points in the two polygons. Your job is to write a program to calculate the minimum width of placements satisfying the above condition. Let's see an example. If the polygons in Figure 13 are placed with L = 10.0, the result will be 100. Figure 14 shows one of the optimal placements. Input The input consists of multiple datasets. Each dataset is given in the following format. L Polygon 1 Polygon 2 L is a decimal fraction, which means the required distance of two polygons. L is greater than 0.1 and less than 50.0. The format of each polygon is as follows. n x 1 y 1 x 2 y 2 . . . x n y n n is a positive integer, which represents the number of vertices of the polygon. n is greater than 2 and less than 15. Remaining lines represent the vertices of the polygon. A vertex data line has a pair of nonneg- ative integers which represent the x - and y-coordinates of a vertex. x - and y -coordinates are separated by a single space, and y -coordinate is immediately followed by a newline. x and y are less than 500. Edges of the polygon connect vertices given in two adjacent vertex data lines, and vertices given in the last and the first vertex data lines. You may assume that the vertices are given in the counterclockwise order, and the contours of polygons are simple, i.e. they do not cross nor touch themselves. Also, you may assume that the result is not sensitive to errors. In concrete terms, for a given pair of polygons, the minimum width is a function of the given minimum distance l . Let us denote the function w ( l ). Then you can assume that | w ( L ± 10 -7 ) - w ( L )| < 10 -4 . The end of the input is indicated by a line that only contains a zero. It is not a part of a dataset. Output The output should consist of a series of lines each containing a single decimal fraction. Each number should indicate the minimum width for the corresponding dataset. The answer should not have an error greater than 0.0001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 10.5235 3 0 0 100 100 0 100 4 0 50 20 50 20 80 0 80 10.0 4 120 45 140 35 140 65 120 55 8 0 0 100 0 100 100 0 100 0 55 80 90 80 10 0 45 10.0 3 0 0 1 0 0 1 3 0 100 1 101 0 101 10.0 3 0 0 1 0 0 100 3 0 50 100 50 0 51 0 Output for the Sample Input 114.882476 100 1 110.5005
37,314
Score : 100 points Problem Statement Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation. Given are two integers A and B . If Takahashi can calculate A \times B , print the result; if he cannot, print -1 instead. Constraints 1 \leq A \leq 20 1 \leq B \leq 20 All values in input are integers. Input Input is given from Standard Input in the following format: A B Output If Takahashi can calculate A \times B , print the result; if he cannot, print -1 . Sample Input 1 2 5 Sample Output 1 10 2 \times 5 = 10 . Sample Input 2 5 10 Sample Output 2 -1 5\times 10 = 50 , but Takahashi cannot do this calculation, so print -1 instead. Sample Input 3 9 9 Sample Output 3 81
37,315
文字列 S ず m 個のク゚リが䞎えられる i 番目のク゚リは二぀の文字列 x i , y i で䞎えられる 各ク゚リに぀いお文字列 S の郚分文字列であり x i で始たり y i で終わるものの䞭で最長の長さを答えよ 文字列 S に぀いお |S| は S の長さを衚すたた文字列 T が文字列 S の郚分文字列であるずはある敎数 i が存圚しお 1 ≀ j ≀ |T| に察しお T j = S i+j を満たすこずを蚀うただし T j は T の j 番目の文字を衚す Input 入力は以䞋の圢匏で䞎えられる S m x 1 y 1 x 2 y 2 : x m y m 1 行目には文字列 S が䞎えられる 2 行目にはク゚リの個数 m が䞎えられる 3 行目からの m 行のうち i 行目には i 番目のク゚リ文字列 x i , y i が空癜区切りで䞎えられる Constraints 1 ≀ |S| ≀ 2 × 10 5 1 ≀ m ≀ 10 5 1 ≀ |x i |, |y i | $\sum^m_{i=1}$ ( |x i | + |y i | ) ≀ 2 × 10 5 S 及び x i , y i は半角の英小文字のみからなる Output 以䞋の圢匏で最倧の郚分文字列の長さを答えよ len 1 len 2 : len m 1 行目からの m 行のうち i 行目には i 番目のク゚リに぀いお条件を満たす最長の郚分文字列の長さ len i を出力せよそのような郚分文字列がない堎合0 を出力せよ Sample Input 1 abracadabra 5 ab a a a b c ac ca z z Output for the Sample Input 1 11 11 4 3 0 文字列 S ずしおabracadabra が䞎えられる "ab" で始たり "a" で終わる郚分文字列は"abra" や "abraca", "abracada", "abracadabra" の4 皮類があるが最長の郚分文字列は "abracadabra" で長さは 11 である "a" で始たり "a" で終わる最長の郚分文字列も同様に "abracadabra" で長さは 11 である "b" で始たり "c" で終わる最長の郚分文字列は "brac" で長さは 4 である "ac" で始たり "ca" で終わる最長の郚分文字列は "aca" で長さは 3 である "z" で始たり "z" で終わる郚分文字列は存圚しないよっお0 を出力する Sample Input 2 howistheprogress 4 ist prog s ss how is the progress Output for the Sample Input 2 9 12 5 11 Sample Input 3 icpcsummertraining 9 mm m icpc summer train ing summer mm i c i i g g train i summer er Output for the Sample Input 3 2 10 8 0 4 16 1 6 6
37,316
問題 3: 超郜芳光 (Super Metropolis) 問題 JOI 君はIOI 囜にある超郜ずいう郜垂の芳光ツアヌを蚈画するこずになった 超郜は南北方向にたっすぐに䌞びる W 本の道路ず東西方向にたっすぐに䌞びる H 本の道路により碁盀の目の圢に区分けされおいる 南北方向の W 本の道路には西から順に 1, 2, ... , W の番号が付けられおいるたた東西方向の H 本の道路には南から順に 1, 2, ... , H の番号が付けられおいる西から i 番目の南北方向の道路ず南から j 番目の東西方向の道路ずの亀差点を (i, j) で衚す さらに䞋図のように各亀差点からは぀北東の亀差点ぞの道がある最も北の道路䞊の亀差点ず最も東の道路䞊の亀差点を陀く たた぀南西の亀差点ぞの道もある最も南の道路䞊の亀差点ず最も西の道路䞊の亀差点を陀く すなわち亀差点 (i, j) からはもし亀差点 (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1) があるずきはそれらの亀差点ぞ本の道を䜿っお行くこずが出来るそれに加えもし亀差点 (i - 1, j - 1), (i + 1, j + 1) があるずきはそれらの亀差点ぞも本の道を䜿っお行くこずが出来る JOI 君はツアヌの蚈画ずしお既に N 個の芳光スポットをどのような順番で蚪れるかを決めおいる i 番目 (1 ≩ i ≩ N) に蚪れる芳光スポットは亀差点 (X i , Y i ) にある JOI 君はツアヌにかかる時間をできるだけ短くするために通らなければならない道の本数を少なくしたい 芳光スポットを事前に決めた順番で蚪れるために通らなければならない道の本数の合蚈の最小倀を求めるプログラムを䜜成せよ ただしツアヌの開始地点は亀差点 (X 1 , Y 1 ) であるたたツアヌの途䞭で超郜の倖ぞ移動しおはならないものずする たたJOI 君は芳光スポットのある亀差点を芳光スポットを蚪れずに通過するこずもできる 「道の本数の合蚈」に぀いおの補足ツアヌの途䞭で同じ道を回以䞊通るこずもできるその堎合「道の本数の合蚈」ずしおはその道に぀いおは通った回数だけ重耇しお数えるものずする 入力 入力は 1 + N 行からなる 1 行目には空癜を区切りずしお 3 ぀の敎数 W, H, N (2 ≀ W ≀ 10000, 2 ≀ H ≀ 10000, 1 ≀ N ≀ 1000) が曞かれおいる 続く N 行のうちの i 行目 (1 ≀ i ≀ N) には2 ぀の敎数 X i , Y i (1 ≀ X i ≀ W, 1 ≀ Y i ≀ H) が空癜を区切りずしお曞かれおいるこれはi 番目に蚪れる芳光スポットのある亀差点が (X i , Y i ) であるこずを衚す 出力 芳光スポットを順番に蚪れるために通る道の本数の合蚈の最小倀を 1 行で出力せよ 入出力䟋 入力䟋 1 4 3 3 1 1 3 3 4 1 出力䟋 1 5 入出力䟋 1 では䟋えば (1, 1), (2, 2), (3, 3), (3, 2), (4, 2), (4, 1) の順で亀差点を蚪れれば良い 入力䟋 2 4 3 5 1 3 4 3 2 2 2 2 1 3 出力䟋 2 7 入出力䟋 2 のように同じ亀差点に耇数回蚪れるこずもある 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
37,317
犏瞞軒 「犏瞞軒」は行列のできる人気のラヌメン屋です。でも最近、お客さんの間から「埅ち時間が長いのに、店に入ったら空垭があるのは蚱せない」ずいう声が聞こえるようになっおきたした。どうしおそんな䞍満が出るのか調べたいのですが、お店が開いおいるあいだは忙しくお、実際の行列の様子を調べるこずができたせん。しかし、長幎の経隓からお客さんが来る間隔や人数は分かっおいるので、それをもずに埅ち時間の分析をするこずにしたした。 店内にはカりンタヌに向かっお 17 の垭がありたす。開店時間は正午で、お客さんは次のようにやっおきたす。 0 番から 99 番たでの 100 組のグルヌプが来たす。 i 番目のグルヌプは正午から 5 i 分埌にお店に到着したす。 i 番目のグルヌプの人数は i % 5 が 1 のずき 5 人、それ以倖のずきは 2 人です。 ( x % y は x を y で割ったずきの䜙りを衚わしたす。) i 番目のグルヌプは、垭に着くず 17( i % 2) + 3( i % 3) + 19 分間で食事を枈たせたす。 最初の 10 グルヌプの到着時刻、人数、食事時間は次のようになりたす。 グルヌプ番号 0 1 2 3 4 5 6 7 8 9 到着時刻(分埌) 0 5 10 15 20 25 30 35 40 45 人数(人) 2 5 2 2 2 2 5 2 2 2 食事時間(分) 19 39 25 36 22 42 19 39 25 36 たた、お客さんを垭に案内するずきには、次のようにしおいたす。 垭には 0 から 16 たでの番号が付いおいたす。 x 人のグルヌプは連続しお x 個あいおいる垭があった時だけ着垭できたす。 たた、座れる堎所が耇数あった堎合は、垭の番号が最も小さくなるずころに座りたす。䟋えば、0、1、2、4、5番の垭だけが空いおいた堎合、5 人のグルヌプは着垭できたせん。2 人のグルヌプであれば 0、1 番に着垭したす。 䞀床着垭したら、垭を移動しおもらうこずはしたせん。 お客さんは 1 分単䜍で出入りしたす。各時刻には次の順序でお客さんを案内したす。 前のグルヌプの離垭ず同時に次のグルヌプの着垭が可胜ずなりたす。 お客さんを着垭させる際には、行列の先頭にいるグルヌプから順に、できる限り倚くのグルヌプを同じ時刻に着垭させたす。行列の順序を远い越すこずはしたせん。぀たり、先頭のグルヌプが着垭できなければ、行列内の他のグルヌプが着垭できたずしおも、着垭させたせん。 その時刻に到着したグルヌプは、行列が残っおいる堎合は行列の最埌尟に䞊びたす。行列が無く、着垭できる堎合は着垭し、できない堎合は䞊んで埅ちたす。䟋ずしお最初の 10 グルヌプが到着するたでの様子を瀺すず以䞋のようになりたす。各行の3぀の欄は、巊から時刻、座垭の様子、行列の様子を瀺しおいたす。座垭は「_」が空垭で、番号はその垭にその番号のグルヌプが座っおいるこずを瀺しおいたす。 時刻: 座垭 行列 0: 00_______________: 5: 0011111__________: 10: 001111122________: 15: 00111112233______: 18: 00111112233______: 19: __111112233______: 20: 44111112233______: 25: 4411111223355____: 30: 4411111223355____: 66666 グルヌプ6が到着 34: 4411111223355____: 66666 35: 4411111__3355____: 6666677 グルヌプ7が到着 40: 4411111__3355____: 666667788 グルヌプ8が到着 41: 4411111__3355____: 666667788 42: __11111__3355____: 666667788 43: __11111__3355____: 666667788 44: 6666677883355____: グルヌプ6、7、8が着垭 45: 666667788335599__: グルヌプ9が到着、着垭 䟋えば、時刻 40 では 8 番目のグルヌプが到着したすが、着垭できないので行列に加わりたす。4 番目のグルヌプは時刻 41 たで食事をしたす。時刻 42 では、4 番目のグルヌプの垭が空きたすが、連続した垭数が足りないので 6 番目のグルヌプはただ着垭できたせん。1 番目のグルヌプは時刻 43 たで食事をしたす。時刻 44 で 1 番目のグルヌプの垭が空くので、6 番目のグルヌプが着垭し、同時に 7 番目、8 番目のグルヌプも着垭したす。9 番目のグルヌプは時刻 45 で到着し、垭が空いおいるのでそのたた着垭したす。 これらの情報を基にしお、0 以䞊 99 以䞋の敎数 n を入力ずしお、 n 番目グルヌプのお客さんが埅぀時間(分単䜍)を出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットは぀の敎数 n からなりたす。 デヌタセットの数は 20 を超えたせん。 Output 各デヌタセットに぀いお、 n 番目のお客の分単䜍の埅ち時間(0 以䞊の敎数)を行に出力しおください。 Sample Input 5 6 7 8 Output for the Sample Input 0 14 9 4
37,318
Problem P02: Yes, I have a number 博士 ペヌタヌ君「Yes, I have a number」っお知っおるかい ぺヌタヌ このたえテレビでやっおたしたよ。文章に含たれる各単語の文字数で䜕かを芚えるずいうものですよね。 「Yes, I have a number」 だから、「3.14ずいう数字」ずいう意味になっお円呚率を芚えるためのキヌワヌドになっおいるんですよね。 博士 ペヌタヌ君、それは違うんじゃ。 これは、3.1416 ず解釈せねばならぬのじゃ。円呚率は3.14159...だからのう。 ペヌタヌ それじゃ、あの番組は日本が円呚率を3.14ず教えおいるからずいっお無理矢理省略したんですか 博士 、、、。小孊校で教える円呚率がやっず3から3.14に戻っただけでもよしずしよう。 ペヌタヌ そもそも、これで本圓に芚えられるんですか 博士 日本人には難しいかもしれん。英語圏の人たちは䜿うようじゃよ、英語でゎロ合わせをするのは難しいからのぉ。 ペヌタヌ それにしおも、チェックするのが面倒くさそうですね。 博士 そこでじゃ、文章を単語の文字数の数列に倉換するプログラムを䜜っおほしいのじゃ。 ペヌタヌ 了解したした。詳しい仕様を教えおください博士。 博士 1行の文章を入力ずしよう。簡単のためにアルファベットず空癜のみを含む文章でよいぞ。 この文章に぀いお、空癜・文頭・文末に挟たれた文字列の長さを順番に出力しおくれ。 文字列の文字数は9を超えないず過皋しおよいぞ。 䟋えば、"Yes I have" の Yes は文頭ず最初の空癜で区切られおいるから文字数は 3 、 I は 1 ぀目ず 2 ぀目の空癜に区切られおいお文字数は 1 じゃ。 博士 ・・・そうじゃ、空癜が連続した堎合の、文字数が 0 になる文字列も忘れずにな。 Input 耇数のデヌタセットが入力ずしお䞎えられたす。各デヌタセットで、アルファベットず空癜を含む文字列が行に䞎えられたす。 文字列が "END OF INPUT" のずき、入力の終わりずしたす。この入力に察する出力を行っおはいけたせん。 Output 各デヌタセットに぀いお、文字列に察する文字数の数列を行に出力しお䞋さい。 Sample Input Yes I have a number How I wish I could calculate an unused color for space Thank you END OF INPUT Output for the Sample Input 31416 31415926535 53
37,320
Score: 400 points Problem Statement Quickly after finishing the tutorial of the online game ATChat , you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N , and the friendliness of Player i is A_i . The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere. When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0 . What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into? Constraints All values in input are integers. 2 \leq N \leq 2 \times 10^5 1 \leq A_i \leq 10^9 Input Input is given from Standard Input in the following format: N A_1 A_2 \dots A_N Output Print the maximum total comfort the N players can get. Sample Input 1 4 2 2 1 3 Sample Output 1 7 By arriving at the place in the order Player 4, 2, 1, 3 , and cutting into the circle as shown in the figure, they can get the total comfort of 7 . They cannot get the total comfort greater than 7 , so the answer is 7 . Sample Input 2 7 1 1 1 1 1 1 1 Sample Output 2 6
37,321
勉匷䌚 プログラマヌ逊成校のアカベ高校には、生埒自身で運営するナニヌクな勉匷䌚がありたす。プログラマヌは新しい技術を垞に取り入れるこずが倧切なので、この勉匷䌚を通しお自孊自習の習慣を身に぀けるこずがこの掻動のねらいです。 生埒は党郚で N 人おり、それぞれが入孊時のプログラミングコンテストの結果で埗られたスコアを持っおいたす。勉匷䌚では N 人の生埒のうち䜕人かがリヌダヌになり、各リヌダヌがそれぞれのグルヌプを運営するずずもに、自らの運営するグルヌプに参加したす。 リヌダヌ以倖の生埒は、自分のスコアよりも䜎いスコアのリヌダヌが運営するグルヌプには参加できたせん。たた、0以䞊のある倀 r を぀決め、グルヌプに参加する生埒ずリヌダヌのスコアの差が r 以内ずなるようにしおいたす。぀たり、グルヌプのリヌダヌのスコアが s のずき、自分のスコアが s を超えおいるか、あるいは s - r 未満ならば、そのグルヌプには参加できたせん。 あなたは勉匷䌚の実行委員長であり、運営準備のためにシミュレヌションを行うこずにしたした。シミュレヌションでは、リヌダヌが誰もいない状態から始め、以䞋の操䜜を䜕回か繰り返したす。 生埒をリヌダヌに加える。 生埒をリヌダヌから倖す。 芁求時点でのリヌダヌの組み合わせに぀いお、どのグルヌプにも参加できない生埒が x 人以䞋になるような、最小の r を求める。 このようなシミュレヌションを行うプログラムを䜜成しおください。 入力 入力は1぀のデヌタセットからなる。入力は以䞋の圢匏で䞎えられる。 N Q s 1 s 2 : s N QUERY 1 QUERY 2 : QUERY Q 1行目に生埒の数 N (1 ≀ N ≀ 1000000)、凊理芁求の数 Q (0 ≀ Q ≀ 1000)が䞎えられる。 続く N 行に i 番目の生埒のスコアを瀺す敎数 s i (0 ≀ s i ≀ 1,000,000,000)が䞎えられる。生埒は1,2,..., N で番号付けされおいるものずする。 続く Q 行に凊理芁求 QUERY i が䞎えられる。凊理芁求は時系列順に䞎えられる。凊理芁求はADD, REMOVE, CHECKの3皮類あり、各 QUERY i は以䞋のいずれかの圢匏で䞎えられる。 ADD a たたは REMOVE a たたは CHECK x ADD a は番号 a (1 ≀ a ≀ N )の生埒をリヌダヌに加えるこずを衚す。 REMOVE a は番号 a (1 ≀ a ≀ N )の生埒をリヌダヌから倖すこずを衚す。 CHECK x は出力芁求を衚す。どのグルヌプにも参加できない生埒の数の䞊限 x (0 ≀ x ≀ N )が䞎えられる。 なお、入力は以䞋の条件を満たすものずする。 どの時点でも、リヌダヌの人数が100人を超えるこずはない。 その時点でリヌダヌである生埒をリヌダヌに加えるこずはない。 その時点でリヌダヌでない生埒をリヌダヌから倖すこずはない。 出力 時系列順に各出力芁求の時点で、どのグルヌプにも参加できない生埒がx人以䞋になるような最小の r を1行に出力する。ただし、どのような r を遞んでも x 人以䞋にするこずが䞍可胜であればNAず出力する。 入力䟋 1 5 8 5 10 8 7 3 ADD 1 ADD 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 出力䟋 1 NA 2 1 0 0 0 入力䟋 2 5 28 5 10 8 7 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 ADD 1 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 REMOVE 1 ADD 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 ADD 1 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 出力䟋 2 NA NA NA NA NA 0 NA NA NA 2 0 0 NA 5 3 1 0 0 NA 2 1 0 0 0
37,322
8 Puzzle The goal of the 8 puzzle problem is to complete pieces on $3 \times 3$ cells where one of the cells is empty space. In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 8 as shown below. 1 3 0 4 2 5 7 8 6 You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps). 1 2 3 4 5 6 7 8 0 Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle. Input The $3 \times 3$ integers denoting the pieces or space are given. Output Print the fewest steps in a line. Constraints There is a solution. Sample Input 1 3 0 4 2 5 7 8 6 Sample Output 4
37,323
Non-redundant Drive The people of JAG kingdom hate redundancy. For example, the N cities in JAG kingdom are connected with just $N - 1$ bidirectional roads such that any city is reachable from any city through some roads. Under the condition, the number of paths from a city to another city is exactly one for all pairs of the cities. This is a non-redundant road network :) One day, you, a citizen of JAG kingdom, decided to travel as many cities in the kingdom as possible with a car. The car that you will use has an infinitely large tank, but initially the tank is empty. The fuel consumption of your car is 1 liter per 1 km, i.e. it consumes 1 liter of gasoline to move 1 km. Each city has exactly one gas station, and you can supply $g_x$ liters of gasoline to your car at the gas station of the city $x$. Of course, you have a choice not to visit some of the gas stations in your travel. But you will not supply gasoline twice or more at the same gas station, because it is redundant. Each road in the kingdom has a distance between two cities: the distance of $i$-th road is $d_i$ km. You will not pass the same city or the same road twice or more, of course, because it is redundant. If a quantity of stored gasoline becomes zero, the car cannot move, and hence your travel will end there. But then, you may concern about an initially empty tank. Don't worry. You can start at any gas station of the cities in the kingdom. Furthermore, each road directly connects the gas stations of the its two ends (because the spirit of non-redundancy avoids redundant moves in a city), you therefore can supply gasoline to your car even if your car tank becomes empty just when you arrive the city. Your task is to write a program computing the maximum number of cities so that you can travel under your non-redundancy policy. Input The input consists of a single test case. $N$ $g_1$ $g_2$ ... $g_N$ $a_1$ $b_1$ $d_1$ $a_2$ $b_2$ $d_2$ ... $a_{N-1}$ $b_{N-1}$ $d_{N-1}$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of cities in JAG kingdom. The second line contains $N$ integers: the $i$-th of them is $g_i$ ($1 \leq g_i \leq 10,000$), the amount of gasoline can be supplied at the gas station of the city $i$. The following $N - 1$ lines give information of roads: the $j$-th line of them contains $a_j$ and $b_j$ , which indicates that the $j$-th road bidirectionally connects the cities $a_j$ and $b_j$ ($1 \leq a_j, b_j \leq N, a_j \ne b_j$) with distance $d_j$ ($1 \leq d_j \leq 10,000$). You can assume that all cities in the kingdom are connected by the roads. Output Print the maximum number of cities you can travel from any city under the constraint such that you can supply gasoline at most once per a gas station. Sample Input 1 5 5 8 1 3 5 1 2 4 2 3 3 2 4 3 1 5 7 Output for the Sample Input 1 4 Sample Input 2 2 10 1 1 2 10 Output for the Sample Input 2 2 Sample Input 3 5 1 3 5 1 1 1 2 5 2 3 3 2 4 3 1 5 5 Output for the Sample Input 3 3
37,324
Problem C: Mobile Phone Coverage A mobile phone company ACMICPC (Advanced Cellular, Mobile, and Internet-Connected Phone Corporation) is planning to set up a collection of antennas for mobile phones in a city called Maxnorm. The company ACMICPC has several collections for locations of antennas as their candidate plans, and now they want to know which collection is the best choice. for this purpose, they want to develop a computer program to find the coverage of a collection of antenna locations. Each antenna A i has power r i , corresponding to "radius". Usually, the coverage region of the antenna may be modeled as a disk centered at the location of the antenna ( x i , y i ) with radius r i . However, in this city Maxnorm such a coverage region becomes the square [ x i − r i , x i + r i ] × [ y i − r i , y i + r i ]. In other words, the distance between two points ( x p , y p ) and ( x q , y q ) is measured by the max norm max{ | x p − x q |, | y p − y q |}, or, the L ∞ norm, in this city Maxnorm instead of the ordinary Euclidean norm √ {( x p − x q ) 2 + ( y p − y q ) 2 }. As an example, consider the following collection of 3 antennas 4.0 4.0 3.0 5.0 6.0 3.0 5.5 4.5 1.0 depicted in the following figure where the i -th row represents x i , y i r i such that ( x i , y i ) is the position of the i -th antenna and r i is its power. The area of regions of points covered by at least one antenna is 52.00 in this case. Write a program that finds the area of coverage by a given collection of antenna locations. Input The input contains multiple data sets, each representing a collection of antenna locations. A data set is given in the following format. n x 1 y 1 r 1 x 2 y 2 r 2 . . . x n y n r n The first integer n is the number of antennas, such that 2 ≀ n ≀ 100. The coordinate of the i -th antenna is given by ( x i , y i ), and its power is r i . x i , y i and r i are fractional numbers between 0 and 200 inclusive. The end of the input is indicated by a data set with 0 as the value of n . Output For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the coverage region. The area should be printed with two digits to the right of the decimal point, after rounding it to two decimal places. The sequence number and the area should be printed on the same line with no spaces at the beginning and end of the line. The two numbers should be separated by a space. Sample Input 3 4.0 4.0 3.0 5.0 6.0 3.0 5.5 4.5 1.0 2 3.0 3.0 3.0 1.5 1.5 1.0 0 Output for the Sample Input 1 52.00 2 36.00
37,325
Score : 600 points Problem Statement There are N points in a two-dimensional plane. The initial coordinates of the i -th point are (x_i, y_i) . Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x - or y - axis. You are given a character d_i that represents the specific direction in which the i -th point moves, as follows: If d_i = R , the i -th point moves in the positive x direction; If d_i = L , the i -th point moves in the negative x direction; If d_i = U , the i -th point moves in the positive y direction; If d_i = D , the i -th point moves in the negative y direction. You can stop all the points at some moment of your choice after they start moving (including the moment they start moving). Then, let x_{max} and x_{min} be the maximum and minimum among the x -coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y -coordinates of the N points, respectively. Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it. Constraints 1 \leq N \leq 10^5 -10^8 \leq x_i,\ y_i \leq 10^8 x_i and y_i are integers. d_i is R , L , U , or D . Input Input is given from Standard Input in the following format: N x_1 y_1 d_1 x_2 y_2 d_2 . . . x_N y_N d_N Output Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) . The output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-9} . Sample Input 1 2 0 3 D 3 0 L Sample Output 1 0 After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment. Sample Input 2 5 -7 -10 U 7 -6 U -8 7 D -3 3 D 0 -6 R Sample Output 2 97.5 The answer may not be an integer. Sample Input 3 20 6 -10 R -4 -9 U 9 6 D -3 -2 R 0 7 D 4 5 D 10 -10 U -1 -8 U 10 -6 D 8 -5 U 6 4 D 0 3 D 7 9 R 9 -4 R 3 10 D 1 9 U 1 -6 U 9 -8 R 6 7 D 7 -3 D Sample Output 3 273
37,326
Problem C: Manhattan Warp Machine 1 Problem ずある宇宙では、1次元の敎数座暙点䞊に星が存圚し、宇宙人達はマンハッタンワヌプ装眮を䜿い、星間を移動しおいる。 このワヌプ装眮には、 N 個のボタンが付いおおり、ボタン i を抌すず、珟圚いる星からのマンハッタン距離が d i である任意の星にコスト c i でワヌプするこずができる。 今、点0の星にいるある宇宙人が点 x の星に行きたいず考えおいる。 点 x の星に蟿り着くたでの最小のコストを答えよ。 蟿り着けない堎合は-1を出力せよ。 1次元䞊の敎数座暙点には必ず星が存圚する。 点 x 1 ず点 x 2 間のマンハッタン距離は| x 1 - x 2 |で衚される。 Input N x d 1 c 1 ... d N c N 入力は党お敎数で䞎えられる。 1行目に N ず行きたい星の座暙 x が空癜区切りで䞎えられる。 続く N 行に、移動可胜なマンハッタン距離 d i ずコスト c i が1行ず぀空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 1 ≀ N ≀ 15 0 ≀ x ≀ 10 5 1 ≀ d i ≀ 10 5 1 ≀ c i ≀ 100 䞎えられるマンハッタン距離 d i は党お異なる。 Output 点 x の星に蟿り着くたでにかかる最小コストを1行に出力せよ。蟿り着くこずが䞍可胜な堎合は-1を出力せよ。 Sample Input 1 2 5 1 1 2 1 Sample Output 1 3 Sample Input 2 2 12 9 1 3 2 Sample Output 2 3 Sample Input 3 1 3 4 1 Sample Output 3 -1
37,327
Score : 900 points Problem Statement For a positive integer n , let us define f(n) as the number of digits in base 10 . You are given an integer S . Count the number of the pairs of positive integers (l, r) ( l \leq r ) such that f(l) + f(l + 1) + ... + f(r) = S , and find the count modulo 10^9 + 7 . Constraints 1 \leq S \leq 10^8 Input Input is given from Standard Input in the following format: S Output Print the answer. Sample Input 1 1 Sample Output 1 9 There are nine pairs (l, r) that satisfies the condition: (1, 1) , (2, 2) , ... , (9, 9) . Sample Input 2 2 Sample Output 2 98 There are 98 pairs (l, r) that satisfies the condition, such as (1, 2) and (33, 33) . Sample Input 3 123 Sample Output 3 460191684 Sample Input 4 36018 Sample Output 4 966522825 Sample Input 5 1000 Sample Output 5 184984484
37,328