group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_2200_cpp
Problem D: Mr. Rito Post Office あなたは離島の郵便局に勤めるプログラマである.あなたの住んでいる地域は,複数の島々からなる.各島には一つ以上の港町がある.それらに加えて他の町や村があるかもしない.ある島から別の島に向かうためには船を使わなければならない.一つの島の中を回るには陸路が使えるが,海路を利用した方が速いこともある. 近年行われた郵便局の民営化をきっかけに,経費削減に向けて郵便配達員の人員整理が全国的に行われた.離島の郵便局もその例外ではなく,結果として郵便配達員は利藤さんただ一人となってしまった.その郵便局が集配を担当する地域は非常に広いため,一人で集配するのは大変な作業である.なので,どのようにすれば効率的に集配を行えるのか,利藤さんがあなたに助けを求めてきた. あなたの仕事は,利藤さんがたどるべき町や村の集配順序が与えられたときに,最短巡回経路を求めるプログラムを書くことである. 利藤さんは,決して指定された順序以外で集配業務を行うことができない.しかし,ある町や村から別の町や村へ移動する際に,他の町や村を経由して移動することは許可されている.また,利藤さんは島々を巡るための一隻の船を持っている. 例えば,町A,町B,村Cという集配順序が与えられた場合,町Aから町Bへ向かう際にどの町や村を経由してもかまわない.このとき,村Cを経由しても構わないが,集配順序を守るためには,一度町Bに行って集配をおこなってから,改めて村Cを訪れて集配をおこなわなければならない.また,町Aから海路を用いて町Bに向かい,町Bから陸路を用いて村Cに向かった場合,船を町Bに置いたままになる.したがって,次に海路を使いたい場合は町Bに戻る必要がある. 一つの町や村において複数回集配をしなければならない場合もある.たとえば,町A,村B,町C,村Bという集配順序が与えられるかもしれない.このとき,町Aから村Bをたどらずに町Cに向かった場合は,町Cでいきなり集配をおこなうことはできない.最初の村Bでの集配が終わっていないからである.町Cで集配を済ませた後で村Bを訪れて集配しても,一回目の村Bの集配を終わらせたことにはならない. 利藤さんは,はじめに必ずある港町に船とともにいる.利藤さんはベテランであるため,移動時間以外の集配作業にかかる時間は無視してよい.また,最後の町または村での集配業務が完了するまでの時間だけが問題であり,船をもとの位置に戻して郵便局に帰るまでの時間は考慮しなくてよい. Input 入力は複数のデータセットから構成される.各データセットの形式は次に示すとおりである. N M x 1 y 1 t 1 sl 1 x 2 y 2 t 2 sl 2 ... x M y M t M sl M R z 1 z 2 ... z R データセットの中の入力項目は,すべて非負の整数である.行中の入力項目の区切りは空白 1 個である. 最初の行は,陸路及び海路網の大きさを規定する. N (2 ≤ N ≤ 200) は,町または村の数である. それぞれの町または村には,1 から N までの固有の番号を割り当てる. M (1 ≤ M ≤ 10000) は,陸路と海路の合計本数である. 2 行目から 1 + M 行目は,陸路または海路の記述である. x i と y i (1 ≤ x i , y i ≤ N ) は両端の町または村の番号を表す. t i (1 ≤ t i ≤ 1000) はその陸路または海路の移動時間を表す. sl i は ‘L’ または ‘S’ のいずれかであり,Lは陸路を,Sは海路をそれぞれ表す. ある2つの町や村を直接結ぶ陸路または海路が2本以上存在することがある. それぞれの陸路または海路は双方向であり,すなわちどちらの向きにも移動できる. M + 2 行目の R (1 ≤ R ≤ 1000)は,利藤さんが担当する集配先の数を表す. M + 3 行目には,集配先の町や村の番号 z i (1 ≤ z i ≤ N ) が集配順に R 個並んでいる. 初期状態では利藤さんと船はともに港町 z 1 に存在する. 初期状態から集配先の町や村へは,必ず何らかの方法で移動することができる. 入力の終わりは,空白で区切られた2つの0を含む1行で示される. Output 入力の各データセットに対して,与えられた集配順序で利藤さんが町と村を巡回するために必要な最短移動時間を求め,1行に出力せよ. Sample Input 3 3 1 2 5 L 1 2 7 S 2 3 11 S 3 1 2 3 5 5 1 2 15 L 2 3 10 L 4 5 7 L 1 3 30 S 3 4 100 S 5 1 3 5 4 1 0 0 Output for the Sample Input 18 269
[ { "submission_id": "aoj_2200_11057001", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF=0x3f3f3f3f;\nint main(){\n int n,m;\n while(scanf(\"%d%d\",&n,&m)!=EOF&&(n||m)){\n int d[2010][202];\n int l[202][202];\n int s[202][202];\n memset(d,0x3f,...
aoj_2199_cpp
Problem C: Differential Pulse Code Modulation 差分パルス符号変調は主に音声信号を圧縮する際に用いられる圧縮手法の一つである. 音声信号は計算機上では整数列(インパルス列)として扱われる.整数列は入力信号を一定時間間隔で標本化(サンプリング)し,振幅を記録したものである.一般にこの整数列は前後の値が近いという傾向がある.これを利用し,前後の値の差分を符号化し,圧縮率を向上させるのが差分パルス符号変調である. 本問題では差分の値をあらかじめ定められた値の集合から選ぶことを考える.この値の集合をコードブックと呼ぶことにする.復号化後の音声信号 y n は以下の式で定められる. y n = y n - 1 + C [ k n ] ここで k n はプログラムによって出力される出力系列, C [ j ] はコードブックの j 番目の値である.ただし y n は加算によって0未満の値となった場合は0に,255より大きい値となった場合は255にそれぞれ丸められる.また, y 0 の値は128とする. あなたの仕事は,入力信号とコードブックが与えられたときに,元の入力信号と復号化後の出力信号との差の二乗和が最小となるように出力系列を選んで,そのときの差の二乗和を出力するプログラムを書くことである. 例えば,コードブックとして {4, 2, 1, 0, -1, -2, -4} という値のセットを使って 131, 137 という列を圧縮する場合, y 0 = 128 , y 1 = 128 + 4 = 132 , y 2 = 132 + 4 = 136 という列に圧縮すると 二乗和が (131 - 132)^2 + (137 - 136)^2 = 2 と最小になる. また,同じくコードブックとして {4, 2, 1, 0, -1, -2, -4} という値のセットを使って 131, 123 という列を圧縮する場合, y 0 = 128 , y 1 = 128 + 1 = 129 , y 2 = 129 - 4 = 125 と,先程の例とは違って 131 により近づく +2 を採用しない方が (131 - 129) ^ 2 + (123 - 125) ^ 2 = 8 というより小さな二乗和が得られる. 上記 2つの例は sample input の最初の 2例である. Input 入力は複数のデータセットから構成される.各データセットの形式は次に示すとおりである. N M C 1 C 2 ... C M x 1 x 2 ... x N 最初の行は,入力データセットの大きさを規定する. N は圧縮する入力信号の長さ(サンプル数)である. M はコードブックに含まれる値の個数である. N 及び M は1 ≤ N ≤ 20000,1 ≤ M ≤ 16を満たす. これに続く M 行は,コードブックの記述である. C i はコードブックに含まれる i 番目の値を表す. C i は-255 ≤ C i ≤ 255を満たす. これに続く N 行は,入力信号の記述である. x i は入力信号を表す整数列の i 番目の値である. x i は0 ≤ x i ≤ 255を満たす. データセットの中の入力項目は,すべて整数である.入力の終りは,空白文字1個で区切られた2個のゼロのみからなる行で表される. Output 入力の各データセットに対して, 元の入力信号と復号化後の出力信号との差の二乗和の最小値を一行で出力せよ. Sample Input 2 7 4 2 1 0 -1 -2 -4 131 137 2 7 4 2 1 0 -1 -2 -4 131 123 10 7 -4 -2 -1 0 1 2 4 132 134 135 134 132 128 124 122 121 122 5 1 255 0 0 0 0 0 4 1 0 255 0 255 0 0 0 Output for the Sample Input 2 8 0 325125 65026
[ { "submission_id": "aoj_2199_11034201", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <climits>\n\nusing namespace std;\n\nint main(){\n\n while(1){\n \n int N, M;\n cin >> N >> M;\n if (N==0 && M==0) break;\n\n vector<int> C(M);\n ...
aoj_2201_cpp
Problem E: Immortal Jewels English text is not available in this practice contest. ある時ある貴族が,ある貧乏な国のおてんばで勇敢なお姫様に惚れ込み結婚を申し込んだ.お姫様は貴族にある条件を出した.その条件とは「不死の宝石」と呼ばれている宝石を大量に持ってくることであった.不死の宝石は,ある山の特定の場所でしか取ることができない非常に希少な宝石である.しかも大変壊れやすいため,採取するには特別な方法が必要だった. 不死の宝石は円形をしており,二次元空間上に複数存在している.この宝石を取るには,ある特別な金属の棒でそれらを吸着する必要がある.金属の棒は無限の長さを持つ直線であり,太さは無視することができる.宝石は一つ一つ異なる強さの磁力を持っており,金属がその磁力に反応するほど十分に近ければ宝石は吸着される.具体的には,金属と宝石の表面との距離を d ,宝石の磁力の強さを m としたとき, 0 ≤ d ≤ m であれば宝石は金属に吸着される.逆に,金属の棒と宝石がその磁力よりも離れている場合は吸着できない.また,棒が少しでも宝石を貫通してしまった場合にも,その宝石が壊れてしまうため吸着できない. 例を見てみよう.下の図は二次元空間上に置かれた宝石の例である.宝石1から宝石6まであり,磁力はそれぞれ1, 0, 1, 1, 1, 2であるとする. 図 E-1: 宝石の配置例 上の図に加えて金属の棒を配置した例が下の図である.宝石を吸着した結果も表に示してある.この例の場合,宝石3は磁力の届く範囲より離れており,また宝石4は棒に貫通されているため吸着できないが,残りの4つは全て吸着することができる. 図 E-2: 金属の棒の配置例 宝石名 磁力 金属との距離 吸着できるか 宝石1 1 約0.21 できる 宝石2 0 0 できる 宝石3 1 約5.37 できない 宝石4 1 貫通している できない 宝石5 1 約0.97 できる 宝石6 2 約0.53 できる 表 E-3: 吸着結果 貴族は全財産を注ぎ込み,特別な金属の棒を必死に探し求めた.しかしながら,この金属も非常に貴重であったため,結局たった一本しか入手することができなかった.したがって吸着のチャンスはただ一回しか無い. あなたはある貴族に仕えるプログラマーである.あなたの仕事は,与えられた二次元上の宝石の配置に対して,金属の棒をうまく配置したときに,最大で何個の宝石を吸着することができるかを求めるプログラムを書くことである. Input 入力は複数のデータセットから成り, 1つのデータセットは以下の形式で与えられる. N x 1 y 1 r 1 m 1 x 2 y 2 r 2 m 2 ... x N y N r N m N データセットの最初の行は宝石の数 N (1 ≤ N ≤ 50) を表している.続く N 行の各行には4つの整数 x i , y i , r i , m i (-1000 ≤ x i , y i ≤ 1000, 1 ≤ r i ≤ 100, 0 ≤ m i ≤ 100) が記述されており,宝石の位置,大きさ,および磁力を表す.すなわち,宝石 i は中心を( x i , y i )とし半径が r i であるような円形をしており,その磁力は m i である.宝石は互いに重なり合うことは無い. 入力の終わりは,0のみからなる行で表される. Output 各データセットについて, 一度に吸着することができる宝石の最大数を1行に出力せよ. Sample Input 6 -2 -2 1 1 2 2 2 0 5 7 1 1 8 0 3 1 13 4 1 1 16 1 1 2 3 0 0 2 1 10 0 2 1 0 10 2 1 3 0 0 2 1 10 0 2 1 0 6 2 1 3 0 0 2 1 10 0 2 1 0 4 2 1 1 0 0 1 1 0 Output for the Sample Input 4 2 3 3 1
[ { "submission_id": "aoj_2201_10848639", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\nusing namespace std;\nconst int maxn = 100 + 5;\nconst double PI = acos(-1);\n\nstruct Point\n{\n double x, y;\n Point(){}\n Point(double x_,...
aoj_2207_cpp
Problem D: 無矛盾な単位系 京、垓、{禾予}、穣、溝、澗、正、載、極、恒河沙、阿僧祇、那由他、不可思議、無量大数 分、厘、毛、糸、忽、微、繊、沙、塵、埃、渺、漠、模糊、逡巡、須臾、瞬息、弾指、刹那、六徳、虚空、清浄、阿頼耶、阿摩羅、涅槃寂静 これらが何か分かるだろうか。これらは全て10の累乗につけられた数詞である。 京 = 10 16 , 垓 = 10 20 , {禾予} = 10 24 , 穣 = 10 28 , 溝 = 10 32 , ... 分 = 10 -1 、厘 = 10 -2 、毛 = 10 -3 、糸 = 10 -4 、忽 = 10 -5 , ... ではこれらが実用的に使われているのを見たことはあるだろうか?無いのではなかろうか? 過去の先人達が熟考の末にこれらの命名を行ったにもかかわらず、これらの数詞が日の目を見られないとはなんと勿体無いことだろう。 この問題は、コンテスト参加者の皆様にこれらの数詞を知ってもらうために作られたものである。以下に問題の概要を示す。 1 km = 10 3 m 上記のような単位間の関係が入力として与えられる。この問題では、左辺における単位が右辺における単位の10の累乗倍となっているような単位間の関係だけが与えられる。 単位間の関係が矛盾しているとは、与えられた関係から以下の関係が同時に成り立つことをいう。 1 A = 10 x B 1 A = 10 y B ただし、 x≠yであり、 AとBは任意の単位。 入力として与えられた単位間の関係が矛盾しているかどうか判定せよ。 なお、入力は簡易のため指数表記で与えられる。 Input 入力の一行目に、単位間の関係の個数を示す整数 N (1≤ N ≤ 100) が与えられる。 続くN行に、単位間の関係が含まれる。 単位間の関係は以下のフォーマットで与えられる。 "1 A = 10^x B" A, B は単位を示す。AとBは相異なっており、それぞれ空白を含まないアルファベット小文字1〜16文字からなる。x は -100から100までの整数である。 "1 A = 10^x B" が定義されたとき、 "1 B = 10^-x A" も暗黙のうちに定義されているものとする。 任意の単位系のペアが2回以上定義されることはない。 すなわち、"1 kilobyte = 10^3 byte", "1 kilobyte = 10^1 byte" のような関係が、同時に入力に含まれることはない。 同様に、"1 kilobyte = 10^3 byte", "1 byte = 10^-3 kilobyte" のような関係が、同時に入力に含まれることはない。 Output 与えられた単位系が矛盾していなければ Yes を、 矛盾していれば No を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 3 1 km = 10^3 m 1 m = 10^2 cm 1 km = 10^5 cm 7 1 kilometre = 10^3 metre 1 megametre = 10^3 kilometre 1 metre = 10^-6 megametre 1 terametre = 10^3 gigametre 1 petametre = 10^3 terametre 1 gigametre = 10^-6 petametre 1 metre = 10^-15 petametre 4 1 a = 10^2 b 1 a = 10^3 c 1 b = 10^2 c 1 c = 10^1 d 4 1 acm = 10^2 icpc 1 icpc = 10^3 utpc 1 utpc = 10^4 topcoder 1 topcoder = 10^-1 acm 0 Output for Sample Input Yes Yes No No
[ { "submission_id": "aoj_2207_8526509", "code_snippet": "// #define _GLIBCXX_DEBUG 1\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n\n#define rep1(i, n) for (int i = 1; i <= n; i++)\n#define rep_0(i, n) for (int i = 0; i < n; ...
aoj_2210_cpp
Problem G: 天体観測 2012年のある日のこと。 北の田舎町に住むあなたは、愛娘を連れて展望台で星見をすることにした。 この町は都会と違って街明かりが少なく、空気も澄んでいるので、晴れた日は満天の星空が広がっている。 天文にそこそこ詳しいあなたは季節ごとの有名な星や星座、それにまつわる神話ぐらいは知っているのだが、 なにせここは星降る町、名前のわからない星座もたくさん見えている。 この前の星見のときにそういった星々に関する娘の質問に答えられなかったあなたは、 星見の時刻にいったいどの星座が見えるのかを調べたいと思った。 特定の日時にどの星が見えるのかを知るには、星座早見盤というアイテムが有用である。 最近では携帯端末のGPSと連携した星座早見盤アプリも提供されている。 だが、あいにくあなたの手元には星座早見盤がなく、携帯は七年前の機種で、最新のアプリが動くわけがなかった。 詰んだと思ったあなたが最後の望みをかけてハードディスクを漁ったところ、 なんと2012年1月1日の午前零時時点での各星座を構成する星々の位置を記したファイルを発見した。 これがあれば任意の日時の星の位置がわかるはず! あなたは急いでどの星座が見えるのかを出力するプログラムを書くことにした。 星々の情報は、観測者を中心とした天球上の座標群として与えられている。 座標は方位角と高度のペアで表されている。 方位角は真北を0度、真東を90度というように時計回りに測った0度以上360度未満の角度であり、 高度は地平線上を0度、天頂を90度度、天底を-90度とした-90度以上90度以下の角度である。 各天体は北極星と観測者を結ぶ直線を回転軸として、天球上を一定速度で反時計回りに回転している。 その回転速度は24時間あたり(1+1/365.24)×360度である。 北極星は真北方向の高度43.2度の点にある。 ある星座が見えるとは、その星座を構成するすべての星が地平線より上にあることをいう。 テストデータにおいて、当該時刻に地平線から高度±0.01度以内に星があることはないとしてよい。 なお、地軸のズレ、天体との距離などは考えなくてよい。 また、2012年は閏年であることに注意せよ。 Input 入力は以下のような形式で与えられる。 mon / day hh : mm n name 1 m 1 a 11 h 11 a 12 h 12 ... a 1m 1 h 1m 1 name 2 m 2 a 21 h 21 ... 一行目には星見をする2012年のある日時が24時制で与えられる。 二行目の n は見えるかどうか判定したい星座の数を表す。 残りの行はその n 個の星座の2012年1月1日 00:00時点での位置情報である。 各星座の情報の先頭行には、星座名 name i と星座を構成する星の数 m i が与えられる。 続く m i 行に、各星の方位角 a ij と高度 h ij が与えられる。 これらの二つの数値は小数点以下三桁まで記述された浮動小数点数である。 入力の数値は、1 ≤ n ≤ 1000、1 ≤ m i ≤ 1000、0.000 ≤ a ij < 360.000、-90.000 ≤ h ij ≤ 90.000を満たす。 また、星座名はアルファベットの大文字小文字からなり、長さが50文字を超えることはない。 サンプルにあるとおり、「夏の大三角」や「北斗七星」など、88星座に入っていないものが与えられることもある。 なお、時刻は日没後の時刻とはかぎらないが、気にせず星が見えるものとしてよい。 Output 入力に与えられた順に、指定された日時に見える星座の一覧を出力せよ。 一つも星座が見えないようなテストデータはないと仮定してよい。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。 入力の1行目にデータセットの数Tが与えられます。 各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 Sample Input 3 7/7 18:30 3 Aquila 8 359.216 -32.936 358.624 -49.913 352.155 -43.404 346.852 -35.279 345.110 -36.796 342.448 -44.407 343.088 -38.981 336.070 -43.369 Cygnus 11 357.838 6.615 355.808 5.107 353.475 -18.553 348.868 -10.771 347.949 1.298 344.907 -4.637 342.909 1.163 338.483 -9.293 338.854 -1.786 338.515 1.304 331.752 -10.737 Lyra 4 5.063 -7.812 2.708 -13.386 1.327 -2.841 0.815 -14.109 9/3 02:40 3 SevenStars 7 40.483 50.729 48.968 50.637 50.421 42.796 44.855 40.960 43.493 35.637 42.111 31.430 44.997 25.256 SummerTriangle 3 342.909 1.163 345.110 -36.796 5.063 -7.812 WinterTriangle 3 165.368 51.176 207.400 51.262 184.960 31.381 7/7 19:00 91 Andromeda 25 300.224 10.559 313.114 22.235 304.593 17.311 305.145 18.508 299.557 18.169 295.825 15.561 296.928 16.703 290.573 13.699 304.020 24.766 300.872 24.287 296.636 24.636 304.470 32.153 303.783 32.893 298.697 32.261 306.181 35.986 296.619 37.096 301.005 41.728 319.394 10.944 323.303 16.879 313.853 10.187 313.295 13.504 317.463 18.294 315.012 16.124 315.502 17.165 316.369 19.322 Antila 5 150.199 5.822 143.663 11.282 144.808 2.868 138.069 2.334 136.680 -6.080 Apus 6 159.498 -45.403 165.358 -47.196 157.404 -51.717 166.529 -52.133 167.065 -52.812 162.765 -60.430 Aquarius 28 318.790 -48.408 321.209 -45.287 318.270 -46.509 310.269 -47.366 305.6 ...(truncated)
[ { "submission_id": "aoj_2210_3086590", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e18;\nconst double PI = acos(-1);\n\nbool epsleq(const double &a, const double &b){\...
aoj_2209_cpp
Problem F: UTF-8 UTF-8はマルチバイト文字を符号化する手法の一つである。 文字は計算機上ではバイト列として扱われる。英語だけであればラテンアルファベットおよび数字、記号を合わせても 1byteで表現できるが、残念ながら世界中で利用されている文字を1byteで表現することはできないため、複数のバイトを用いて文字を表す必要がある。 ここで例えば"あ"という文字に12354(0x3042)という2バイト列を割り当てたとき、そのままバイト列を並べると0x30,0x42と2文字なのか0x3042で1文字なのかが区別がつかない。 このため、マルチバイト文字符号化方式のUTF-8では始めの1バイト目で続くバイト列の長さが分かるようにすることにより、この問題を克服している。具体的なビットパターンは以下のようになる。 バイト長 ビットパターン 1 0xxxxxxx 2 110yyyyx 10xxxxxx 3 1110yyyy 10yxxxxx 10xxxxxx 4 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx ここでxは0/1の任意のビットとする。またyも0/1の任意のビットとなるがどれか一つは1である必要がある。また全ての文字は1,2,3,4バイト文字のいずれかであるとする。 ここでyのビットが少なくとも一つは1であるという制約は以下のような理由による。 例えば'/'(0x2f)をバイト列にエンコードする際に0x2fと1バイトで符号化するないしは0xc0 0xafと2バイトで符号化する方法の二つがあるが、このような曖昧性を許すとセキュリティ上の脅威のもととなる可能性があるためである。 以上の話を聞いたあなたはUTF-8がどの程度の自由度があるのかが気になった。具体的には与えられたバイト列の一部のビットが不明な時にUTF-8として可能なバイト列の組み合わせがどの程度あるかということである。 このとき可能なバイト列の組み合わせの個数を求め、1,000,000で割った余りを出力せよ。 Input N b 1 b 2 ... b N Nはバイト列の個数, b i はバイト列である。 1 ≤ N ≤ 1000 を満たしている。 b i は記号{0/1/x}が8個並んでいる。xはビットが不明であることを示している。 Output 可能なバイト列の組み合わせの個数を1,000,000で割った余りを出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 1 xxxxxxxx 3 11100000 10x00000 10111111 3 11100000 10000000 10111111 4 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 0 Output for Sample Input 128 1 0 778240
[ { "submission_id": "aoj_2209_3699896", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define int long long\nusing namespace std;\ntypedef pair<int,int>P;\ntypedef pair<int,P>P2;\n#define F first\n#define S second\n\nint calc1(string s){\n\tif(s[0]=='1')return 0;\n\tint x=1;...
aoj_2208_cpp
Problem E: トーマス・ライトの憂鬱 昨日の夕方の話。 普段通り大学での講義が終わり、日課であるキャンパスでの猫への餌付けを済ませた後、家に帰り着くと、作業着を着た人たちが僕の家の扉を取り替える工事をしていた。 それだけなら特に僕の頭痛の種を増やすような出来事ではないのだが、取り外されて去っていく慣れ親しんだ鍵とレバーがついている茶色の扉が非常に平凡であるのに対して、今からまさに取り付けられようとしている黒色の扉が非常に奇妙なもので、ああこれはまたうちの親父が何かを思いついてしまったのだなと容易に想像がついてしまうのであった。 その偏屈親父が言うには、鍵は無くす可能性があるので信用することが出来ない。手元に鍵があったとしても、目を離している隙に鍵の複製が作られてしまったかもしれない。物にセキュリティーを頼る時代はもう終わりだ。これからはパスワードを家族の各人が覚え、そうすることによって世界征服を目論む悪の科学者から我輩の発明品を守るのだ、ということらしい。 新しく設置された扉には縦にN個ずつ、横にN個ずつの正方形をかたちどるように、縦にN+1本、横にN+1本の線が薄青色で引かれていた。 一行目の正方形の左にはA、二行目の正方形の左にはB、というように、ご丁寧に行番号が書かれている。 一列目の正方形の上には1、二列目の正方形の上には2、と、列番号はどうやら数字のようだ。 N 2 個の正方形には各々スイッチが一つずつ配置されており、OFFの状態だと真っ黒だが、ONの状態だと赤い丸が浮かび上がる、というセキュリティーを考えるならもう少しやりようがあるだろうというギミックが付いている。 そして全てのスイッチのON/OFFを正しく合わせたときのみ、扉が開く、という派手好きの親父が大満足な仕様となっているようだ。 さて、ここでの僕の問題は、このパスワードをきちんと覚えられる自信が無い、ということだ。 自分で言うのも何なのだが、僕は頭がそんなに悪くないつもりなので、パスワードの大体は覚えていられるだろうと思うのだが、機械という物は融通が効かないものだからほんのちょっと間違っただけで、寒空の下で夜を過ごすことになりかねない。 しかし、パスワードを書き写して持ち運べば、親父に見つかったときに小遣い抜きなどの理不尽な制裁が加えられることが予想される。 そこで僕はある作戦を思いついたんだ。 各行、各列の正方形に含まれる赤丸の個数をメモしてそれを持ち運ぼう。 一行目のN個の正方形に含まれる赤丸の個数、二行目のN個の....、 一列目のN個の正方形に含まれる赤丸の個数、二行目のN個の....、 という風に2N個の数字をメモしておけば、もし他の人に見られてもただの数字の羅列だ。何が何だかきっと分からないだろう。 やあわざわざここまで読んでくれた君。 ああそうだ、この作戦にはもしかしたら致命的な欠陥があるかもしれない。 複数のパスワードが、同じメモになってしまうかもしれないのだ。 そうなると、寒空(略)ということになりかねない。我が身の健康を揺るがす一大問題だ。 そこで君には、このメモの数字を入力すると、復元可能なパスワードが一つに定まるかどうかを判定するプログラムを書いてほしい。 一つに定まるなら、Yes、そうでないなら、Noと出力するだけのプログラムさ。簡単だろう? もしかしたらメモをとった時に赤丸を数え間違ったかもしれない。その場合は復元できるパスワードが1つも存在しないかもしれないが、その時は迷わずNoと返してくれ。 ああそうそう、間違っても僕の家に勝手に入ろうとはしないでくれよ。親父の趣味の危ない機械や罠が並んでいるから、命の保証が出来ないからね。 Input 入力は次のような形式で与えられる。 N 1行目の和 2行目の和 ... 1列目の和 2列目の和 ... ただし、N は 1 ≤ N ≤ 10000 を満たす整数である。 Output 問題文に従って、YesもしくはNoを出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 2 1 2 2 1 3 2 1 2 2 1 2 10 0 1 1 2 5 5 5 8 8 9 0 1 3 3 3 6 6 6 7 9 0 Output for Sample Input Yes No Yes
[ { "submission_id": "aoj_2208_10852901", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <iostream>\nusing namespace std;\nint main() {\n\tint n,t;\n\tfor(;(cin>>n),n;){\n\t\tvector<int> r,l;\n\t\tbool ok=true;\n\t\tfor(int i=0;i<n;i++)(cin>>t),r.push_back(t);\n\t\tfor(int i=0;i<n;i++)(cin...
aoj_2213_cpp
Problem J: 多項式の解の個数 きたまさ君は大学でさまざまな変換について研究している。最近、きたまさ君が興味を持っている変換は次のようなものである。 N+1個の整数 a 0 , ..., a N を固定し、整数 z を入力とする。また P を素数とする。 t = ( a N z N + a N-1 z N-1 + ... + a 2 z 2 + a 1 z + a 0 ) mod P きたまさ君はこの変換によって t = 0 になってしまう z がいくつもあることに気がついた。 そこで、きたまさ君は友人でありスーパープログラマーでもあるあなたに、変換後に t = 0 になる z が 0〜P-1 にいくつあるかを計算してもらうことにした。 Input 入力は次の形式で与えられる。 N P a 0 a 1 ... a N 入力の1行目には整数Nと素数Pがスペース文字で区切られて与えられる。これらは、問題文で与えられたとおりである。 0 ≤ N ≤ 100, 2 ≤ P ≤ 10 9 を満たす。 次の1行には a 0 〜a N がスペース文字で区切られて与えられる。これらは |a i | ≤ 10 9 を満たす整数である。また a N ≠ 0 である。 Output 変換後に0になるような z (0 ≤ z < P) の個数を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, P がともに 0 のとき入力の終わりを示します。 Sample Input 2 3 1 2 1 2 3 1 2 6 0 0 Output for Sample Input 1 1
[ { "submission_id": "aoj_2213_10806496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// min non-negative i such that n <= 2^i\nint ceil_pow2(int n) {\n int i = 0;\n while ((1U << i) < (unsigned int)(n)) i++;\n return i;\n}\n\n// min non-negative i such that (x & (1 << i)) != 0...
aoj_2211_cpp
Problem H: 迷い猫、走った 洋菓子専門店ストレイキャッツではたくさんの猫が飼われている。猫の数があまりにも多いため、この店のスタッフである希は餌やりに困っていた。 いくつかの場所に餌を置いても、猫は貪欲なので最も近い餌に向かってしまい、一つの餌入れにたくさんの猫が集中してしまう。 あなたは希のために、できるだけ猫が集中しない餌の配置を求める問題を解くことにした。 問題を簡単にするため、猫は平面上の y>0 の部分にいて餌を置くまで動かないとする。 猫は N 個の集団に分かれていて、一箇所に何匹もいるかもしれない。 餌入れは y=0 上の M 点に固定してあり、この中からいくつか選んで餌を入れることになる。 餌を入れると、猫は(ユークリッド距離で)一番近い餌の入った餌入れに向かうが、同じ距離に二つあるときはx座標が小さいほうに向かうものとする。 この条件の下で、最も猫が集まっている餌入れの猫の数を最小化しなさい。 Input 入力は以下のような形式で与えられる。 N M x 1 y 1 C 1 ... x N y N C N x 1 ... x M 入力の1行目には、2つの整数 N, M がスペース文字で区切られて与えられる。これらは、問題文で指定したとおりである。0 ≤ N ≤ 1000 および 1 ≤ M ≤ 1000 を満たす。 続く N 行には、それぞれの猫の集団について1行に1つずつ、集団の位置の x, y 座標を表す整数の組 (-10000 ≤ x ≤ 10000, 0 < y ≤ 10000)と何匹集まっているかを表す整数C(1 ≤ C ≤ 100000)が与えられる。 その後のM行には、それぞれの餌入れについて1行に1つずつ、餌入れの位置のx座標を表す整数(-10000 ≤ x ≤ 10000)が与えられる。 同じ点に、二つ以上の餌入れが存在することはない。 Output 最も猫が集まっている餌入れの猫の数の最小値を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, M がともに 0 のとき入力の終わりを示します。 Sample Input 3 3 -1 1 1 0 1 1 1 1 1 -3 0 3 5 5 7 3 5 -9 2 7 4 9 6 10 5 9 -9 7 9 0 -1 -2 -9 -8 0 0 Output for Sample Input 2 20
[ { "submission_id": "aoj_2211_10215478", "code_snippet": "// AOJ #2211\n// Stray Cats, Run... 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nstruct State { int i, j; };\n \nbool feasible(ll L, const vector<int>& xGroups, const vector<ll>& weights,\n const vector<ll>...
aoj_2212_cpp
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
[ { "submission_id": "aoj_2212_10853818", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cctype>\n#include<iostream>\n#include<algorithm>\n#include<queue>\n#include<set>\n#include<map>\n#define ULL unsigned long long\n#define LL long long\nusing namespace std;\nconst int MAX...
aoj_2203_cpp
Problem G: Magical Island 2 まだ魔法があたりまえのように存在した時代である.ある魔導士の一族は,魔力によって作られた正方形の形の島に住んでいた. ある時,この島に危機が訪れた.帝国が大陸間弾道魔導ミサイルを開発し,この島を照準に合わせてきたのである.この世界の魔法は地属性,水属性,火属性,風属性などに分類でき,魔導ミサイルもその四つの属性のいずれかに属していた.島の四隅にはそれぞれ地の水晶,水の水晶,火の水晶,風の水晶が配置されていて,水晶に魔力を送り込むことで対応する属性の魔法攻撃を防ぐことがきでる. 魔導士達は帝国が開発した大陸間弾道魔導ミサイルによる攻撃をかろうじて退けることができた.しかし,帝国は新たに闇属性に属する大陸間弾道魔導ミサイルを開発し,再びこの島に攻撃を仕掛けてきた.闇属性に属する魔法はこの島に設置されている水晶で防ぐことはできない.そこで魔導士達は秘術としてこの島に伝えられる光属性の防御陣を用いてミサイルを防ぐ事にした. この光属性の防御陣はある特定の形状をした魔法陣を大地に描くことにより発動する. 魔法陣は半径 R の円周を M 等分し K 個おきに直線で結んだ図形である.魔法陣は任意の大きさで描くことができるが魔法の発動には方角が重要なので,一つの角が真北を向くように描かなければならない.防御陣の効果が及ぶ範囲は描かれた魔法陣の内部(境界を含む)と一致する.またこの魔法の詠唱に必要なエネルギーは描いた魔法陣の半径 R と等しい. 図G-1は M = 4, K = 1 の場合である.灰色の部分が魔方陣の効果が及ぶ領域である. 図G-2は M = 6, K = 2 の場合である.図のように複数の図形が生じる場合は,それらの図形のいずれかに含まれていれば防御陣としての効果が及ぶ. 図G-1: M = 4 , K = 1 の場合 小さい丸が集落を表す 図G-2: M = 6 , K = 2 の場合 あなたの仕事は,集落の位置,および M と K の値が与えられたときに,全ての集落をカバーするために必要となるエネルギーの最低量を求めるプログラムを書くことである. Input 入力は複数のデータセットからなる. 各データセットは以下の形式で与えられる. N M K x 1 y 1 x 2 y 2 ... x N y N 最初の一行は 3 つの整数 N , M , K からなる. N は集落の数を表す. M , K については問題文に記したとおりである. 2 ≤ N ≤ 50, 3 ≤ M ≤ 50, 1 ≤ K ≤ ( M - 1) / 2を仮定してよい. 続く N 行は集落の位置を表す. 各行は二つの整数 x i と y i からなる. x i と y i は i 番目の集落のx座標とy座標を表す. -1000 ≤ x i , y i ≤ 1000 を仮定して良い.y軸の正方向が北を指す. 入力の終わりはスペースで区切られた3個のゼロからなる. Output 各データセットに対し,魔法陣の最小の半径 R を1行に出力せよ. 出力する値は10 -6 以下の誤差を含んでいても構わない.値は小数点以下何桁表示しても構わない. Sample Input 4 4 1 1 0 0 1 -1 0 0 -1 5 6 2 1 1 4 4 2 -4 -4 2 1 5 0 0 0 Output for the Sample Input 1.000000000 6.797434948
[ { "submission_id": "aoj_2203_8216177", "code_snippet": "#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostrea...
aoj_2216_cpp
Problem A: Summer of KMC Description KMCはコミックマーケットと呼ばれる同人即売会で毎年CDを販売している。 コミックマーケットでCDを売ることになったFであったが、Fの人気により、KMCの売り場には人が殺到し、お釣りの計算が追いつかない状態となった。そこでFは金額を入力すると即座にお釣りを出力するプログラムを書くことにした。 KMCが釣り銭として用意しているのは、100円玉と500円玉と1000円札のみである。これらの硬貨・札は無限にあるものと考えて良い。お釣りは、硬貨・札の個数が最小になるように選ぶ。また、KMCが販売するCDの値段は100の倍数であり、購入者が支払う金額も100の倍数である。 Input 入力は複数のテストケースから成る。 各テストケースは2つの正整数A, Bから成る。AはCDの値段であり、Bは購入者が支払った金額である。A, Bは100の倍数であり、100000000を超えない。また、A ≦ B である。 入力は0 0で終わる。 Output 各テストケースに対して、お釣りとして差し出すべき 100円玉、500円玉、1000円札の個数・枚数を一行でこの順番に出力する。各数字の間にはスペースを1つ入れる。 Sample Input 500 1000 100 10000 400 700 600 5000 10000 10000 0 0 Output for Sample Input 0 1 0 4 1 9 3 0 0 4 0 4 0 0 0
[ { "submission_id": "aoj_2216_3702261", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <functional>\n#include <vector>\n#include <utility>\n#include <cstring>\n#include <iomanip>\n#include <numeric>\n#include <limits>\n#include <cmath>\n#include <cassert>\n\nusing nam...
aoj_2214_cpp
Problem K: ワープホール 20XX年、遠く離れた場所に対する効率的な物質転送方式が確立され、人類の宇宙への進出はますます加速されることになった。この転送方式は、より遠くに物質を転送しようとすると、原則的により大きな物質の転送が可能になる点で革新的であった。転送方式の詳細を次に記す。 まず転送対象となる物質は、スタート座標(1, 1)にて単位質量毎のパーティクルに分割される。各パーティクルには、各々異なる波動エネルギーが与えられなければならない。波動エネルギーは、 V と H からなる任意の長さの文字列で表現され、パーティクルの宇宙空間における漂い方を規定する。 V は座標上(+1, 0)の移動を意味し、 H は(0, +1)の移動を意味する。例えば、 VHHV という波動エネルギーが与えられたパーティクルは、(2, 1), (2, 2), (2, 3) という軌跡で宇宙を漂った後、座標(3, 3)に辿り着く。 また、宇宙空間には複数のワープホールが存在し、パーティクルの軌跡に影響を与える。ワープホールには入口と出口があり、パーティクルがワープホールの入口に移動した場合、必ず出口の座標にワープすることになる。i番目のワープホールの入口と出口の座標を(a i , b i ), (c i , d i )とすると、a i ≤ c i , b i ≤ d i , (a i , b i ) ≠ (c i , d i )の条件が満たされており、ワープホールの入口は、他のワープホールの入口と同じ座標や、(1, 1)には存在しないことが保証されている。ただし、複数の出口が同じ場所にあったり、あるワープホールの出口に別のワープホールの入口がある場合はある(この場合は連続でワープする)。 例えば、(1, 2)を入口とし、(3, 2)を出口とするワープホールが存在した場合、 HH と波動エネルギーを与えられたパーティクルは、(1, 2)へ移動後、(3, 2)へワープし、 (3, 3)へと辿り着く。 パーティクルは波動エネルギーを与えられると一瞬でそれに従って移動する。全てのパーティクルが同時に目的地点へ辿り着くと、自動的に元の物質へと再構成される。 あなたは宇宙開発機構のプログラマである。転送の目的座標 (N, M) と K 個のワープホールの座標対が与えられるので、一度に転送可能な物質の最大質量を求めるプログラムを書いてほしい。答えは非常に大きい数になる可能性があるため、1,000,000,007で割った余りを出力せよ。 Input N M K a 1 b 1 c 1 d 1 ... a K b K c K d K 1 ≤ N,M ≤ 10 5 , 0 ≤ K ≤ 10 3 を満たす。また、 任意の 1 ≤ i ≤ K に対して a i ≤ c i , b i ≤ d i , (a i , b i ) ≠ (c i , d i ) を満たす。 Output 最大質量を 1,000,000,007 で割ったあまりを1行で出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, M, K がすべて 0 のとき入力の終わりを示します。 Sample Input 4 4 1 2 2 3 3 1 4 1 1 2 1 3 5 5 2 2 2 3 4 3 3 5 3 5 5 3 4 4 5 5 2 2 3 3 3 3 4 4 100000 100000 1 2 2 99999 99999 1 1 0 0 0 0 Output for Sample Input 12 1 26 18 615667476 1
[ { "submission_id": "aoj_2214_10866029", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, j, k) for (int i = j; i < k; ++i)\n#define irep(i, j, k) for (int i = j - 1; i >= k; --i)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pa...
aoj_2217_cpp
Problem B: Let's JUMPSTYLE Description 踊りの好きなFは,とあるダンスホールでJUMPSTYLEと呼ばれるハードコアな踊りの練習をすることにした. フロアはN×Nの格子状にタイルが敷き詰められている. 踊りの練習をサポートするため,各タイルには次のステップに飛び移るべきタイルの座標が書かれている.Fは日々の練習によって強靭な運動神経を持っており,フロア内の任意のタイルへ飛び移ることができる. Fは,十分に長い時間この踊りを続けていけば,いつか定常状態に入り,同じルートをただループする状態になることに気付いた.Fはこのようなループがこのフロアにいくつ存在するのか疑問に思い,プログラマであるあなたに相談することにした. Input 入力は複数のテストケースから成る. 各テストケースはフロアの1辺の長さNを含む1行で始まる.(1 ≦ N ≦ 100) 続くN行は,フロアの各タイルに書いてあるジャンプ先の座標を以下のように表している. は,それぞれ座標(i, j)にあるタイルに書いてあるジャンプ先のx座標およびy座標を表す.座標は0-originであり,すべての座標の値は0以上N未満の整数である. 入力は0のみから成る行で終わる. Output 各テストケースに対し,異なるループの個数を1行で出力する. Sample Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output for Sample Input 1 2 1 2 3
[ { "submission_id": "aoj_2217_3778511", "code_snippet": "#include <iostream>\n\nusing namespace std;\n\nint w[101][101],h[101][101];\nbool b[101][101];\n\nint dfs(int x,int y)\n{\n \n if(x == -1 || y == -1) return 0;\n if(b[x][y]){\n w[x][y] = -1;\n h[x][y] = -1;\n return 1;\n ...
aoj_2219_cpp
Problem D: THE BYDOLM@STER Description THE BYDOLM@STER(バイドルマスター)とは1rem社より2010/4/1にEXIDNAで発売が予定されている育成シミュレーションゲームである。一応断っておくが、今月初めにネットワーク接続サービスが停止した某アーケードゲームとはたぶん関係が無い。 このゲームはバイドルたちからプロデュースするユニット(編隊)のメンバーを選択し、メンバーたちとのレッスンやコミュニケーションを通じて、彼女(彼)らをバイドルの頂点、トップバイドルに育て上げるゲームである。 各バイドルには能力値としてボーカル、ダンス、ルックスの3つのパラメータを持ち、ユニットの能力値はユニットに属している全てのバイドルのパラメータの合計値となる。ユニットの3つの能力値のうち最大の物がユニットのランクとなる。 ユニットに人数制限は無く、メンバーの人数が1体でも、3体でも、100体でもユニットとして活動できる。もちろん同じバイドルを複数雇うこと出来るが、バイドルを雇うためには費用がかかるのでこれを考慮に入れなければならない。 プロデューサーであるあなたは最高のユニットを作るためにプログラムを書いて計算することにした。 Input 入力は複数のテストケースからなる。 各テストケースの1行目にはバイドルの数Nと使用可能な費用Mが与えられる。(1<=N,M<=300) 次の2*N行には各バイドルに関しての情報が書かれている。 バイドルの情報の1行目にはバイドルの名前。バイドルの名前はアルファベットと空白から成り、30文字を以下である。また同一名のバイドルは存在しない。 2行目には整数C、V、D、Lが与えられる。Cはそのバイドルを1体雇用するコスト、Vはボーカル、Dはダンス、Lはルックスの能力値を表す。(1<=C,V,D,L<=300) 入力はEOFで終わる。 Output 与えられた費用の中で作れるユニットのランクの最大値を答えよ。 ユニットが作れない場合は0を出力せよ。 Sample Input 3 10 Dobkeradops 7 5 23 10 PataPata 1 1 2 1 dop 5 3 11 14 2 300 Bydo System Alpha 7 11 4 7 Green Inferno 300 300 300 300 Output for Sample Input 29 462
[ { "submission_id": "aoj_2219_8860773", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\...
aoj_2218_cpp
Problem C: K Poker Description 時は200X年、K大学で活動する謎のサークルKはその大学の文化祭の最中にK東4Fで彼らが作成した新たなゲーム、Kポーカーを発表した。 このゲームは一見すると普通のポーカーであるが、カードに基本点という要素をつけてポーカーをより奥の深いゲームにしたものである。 カードの基本点はそれぞれのカードに書かれている絵柄によって決まり、手札の点数は手札にある5枚の基本点の合計に手札の役の倍率を掛けたものになる。 この基本点を導入した事により、例えばフルハウスなどの強い役を作ったとしても基本点が0であればブタと同様に扱われてしまい、ワンペアに負けることさえある。 それ以外のルールは通常のポーカーと同じである。 このKポーカーをPCに移植しようと思ったあなたは手札の点数を計算するプログラムを書く事にした。 なおこのゲームは18歳未満の人はプレイ出来ない。 Input 入力は複数のテストケースからなる。 各テストケースの最初の行にはチェックする手札の数Nが書かれている。 次の4行には13個の整数が並びカードの基本点が1-13の順に並んで書かれている。4行のスーツは上から順にスペード、クローバー、ハート、ダイヤの順に並んでいる。 次の行には9個の整数が並び、順にワンペア、ツーペア、スリーカード、ストレート、フラッシュ、フルハウス、フォーカード、ストレートフラッシュ、ロイヤルストレートフラッシュの倍率を表している。役の倍率は必ず昇順となっている。ブタ(役無し、ノーペア)の倍率は常に0である。 次のN行には手札を表す5個の長さ2の異なる文字列が並んでいる。1文字目はカードの数値を表し,A,2,3,4,5,6,7,8,9,T,J,Q,Kのいずれかである。2文字目はカードのスーツを表し、S,C,H,Dのいずれかである。各アルファベットはAはエース、Tは10、Jはジャック、Qはクイーン、Kはキング、Sはスペード、Cはクローバー、Hはハート、Dはダイヤを表す。 入力の数値は全て[0,10000]の範囲に収まっている。 入力はEOFで終わる。 Output 各手札の点数を1行ずつ出力せよ。 連続する2つのテストケースの間には空行を1つ入れること。 役については wikipediaのポーカー のページを参照のこと。 なお、ストレートはエースとキングをまたぐ場合があるが、手札が10,ジャック、クイーン、キング、エースの場合にのみストレートとみなされる。 Sample Input 3 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 2 0 1 1 0 1 1 1 0 0 0 0 5 5 3 1 1 1 0 1 0 0 1 0 3 0 2 1 1 2 4 5 10 20 50 100 7H 6H 2H 5H 3H 9S 9C 9H 8H 8D KS KH QH JD TS 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 AS 3D 2C 5D 6H 6S 6C 7D 8H 9C TS TD QC JD JC KD KH KC AD 2C 3D 4H 6D 5H 7C 2S KS QS AS JS TS TD JD JC TH 8H 8D TC 8C 8S 4S 5S 3S 7S 6S KD QD JD TD AD Output for Sample Input 25 0 14 0 5 10 15 20 25 30 35 40 45
[ { "submission_id": "aoj_2218_8139138", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (u...
aoj_2221_cpp
Problem F: KULASIS Description KULASISとは,全学共通科目に関する情報をWeb化し,学生・教員への支援やサービスの向上を目的に,高等教育研究開発推進機構で開発・運用しているシステムの名称です。 KULASISは2003年度のオンラインシラバス開発から始まり,Web掲示板・履修登録・成績関係(採点登録・学生からの採点確認)と順次システムを拡充してきました。 学生はパソコン・携帯電話から学内外を問わず,教務情報(休講・授業変更・レポート)の確認・履修登録・採点確認等の機能を利用することができます。ログイン件数は多い日には10,000件を超え,京都大学の教務情報システムとして浸透し,全学共通科目を履修するためには欠かせないものとなっています。このKULASISを全学共通科目のみにとどまらず,学部専門課程や大学院にも適用できるよう 開発を進めています。 http://www.z.k.kyoto-u.ac.jp/introduction_kulasis.html 京大生のQは後期のシラバスを組むためにKULASISにログインしていた. どの科目を入れようとしているか悩んでいると,突然KULASISが眩い光を放ち,別のページへと遷移した. 遷移した先のページは下図のようなものであった.5x5のマスには科目名と評価(不可,可,良,優)が書かれており,格子上には ● のボタンが16個配置されている. いったいなにが起きたのか分からなかったQだったが,どうやら格子上にある ● のボタンを押すと,その右上,右下,左上,左下にあるマスの科目の評価がそれぞれ 不可→可,可→良,良→優,優→不可 と入れ替わるようであった. ● のボタンは何度でも押すことができる. KULASISが何者かによって書き換えられてしまったのか自分が夢を見ているのかよく分からないが,これで成績を確定できるならば,出来るだけ良い成績を得たいとQは考えた. 単位を多く集めていることに自信があったQは,単位の数そのものよりも半年後の研究室配属に備えて 戦闘力 を最大にさせることにした. 戦闘力 とは各科目の評価を,不可→0点,可→60点,良→70点,優→80点と換算して合計した値のことで,これは研究室配属の際に自分がどの程度優位なのかを表すと(Qの学科では)思われている. いま,KULASISの画面の表示されている科目とその評価が与えられるので,得ることが出来る 戦闘力 の最大値を出力するプログラムを記してほしい. Input 入力の1行目にはテストケースの個数が与えられる.テストケースの数は100個以下であることが保障されている. 2行目以降は5x5の数字が並び,KULASISの画面に表示されている科目の評価が与えられ,1,2,3,4がそれぞれ不可,可,良,優に対応する. 0ならばそのコマには科目は登録されていない. テストケース同士の間は空行で区切られている. Output 各テストケースに対し,得ることの出来る 戦闘力 の最大値を出力せよ. Sample Input 5 1 1 0 3 3 1 1 0 3 3 0 0 0 0 0 2 2 0 4 4 2 2 0 4 4 1 1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 2 2 2 2 2 0 0 0 2 0 2 2 0 2 0 2 2 0 2 0 0 0 0 2 0 Output for Sample Input 1280 1420 0 1920 1020
[ { "submission_id": "aoj_2221_10850489", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nvector<int> reverse_hash(int x, int b, int l) {\n\tvector<int> ret(l);\n\tint mul = 1;\n\tfor (int i = 0; i < l; i++) {\n\t\tret[i] = x / mul % b;\n\t\tmul *= b;\n\t}\...
aoj_2223_cpp
Problem B: Kaeru Jump There is a frog living in a big pond. He loves jumping between lotus leaves floating on the pond. Interestingly, these leaves have strange habits. First, a leaf will sink into the water after the frog jumps from it. Second, they are aligned regularly as if they are placed on the grid points as in the example below. Figure 1: Example of floating leaves Recently, He came up with a puzzle game using these habits. At the beginning of the game, he is on some leaf and faces to the upper, lower, left or right side. He can jump forward or to the left or right relative to his facing direction, but not backward or diagonally. For example, suppose he is facing to the left side, then he can jump to the left, upper and lower sides but not to the right side. In each jump, he will land on the nearest leaf on his jumping direction and face to that direction regardless of his previous state. The leaf he was on will vanish into the water after the jump. The goal of this puzzle is to jump from leaf to leaf until there is only one leaf remaining. See the example shown in the figure below. In this situation, he has three choices, namely, the leaves A, B and C. Note that he cannot jump to the leaf D since he cannot jump backward. Suppose that he choose the leaf B. After jumping there, the situation will change as shown in the following figure. He can jump to either leaf E or F next. After some struggles, he found this puzzle difficult, since there are a lot of leaves on the pond. Can you help him to find out a solution? Input H W c 1,1 ... c 1, W . . . c H ,1 ... c H , W The first line of the input contains two positive integers H and W (1 ≤ H , W ≤ 10). The following H lines, which contain W characters each, describe the initial configuration of the leaves and the frog using following characters: '.’ : water ‘o’ : a leaf ‘U’ : a frog facing upward (i.e. to the upper side) on a leaf ‘D’ : a frog facing downward (i.e. to the lower side) on a leaf ‘L’ : a frog facing leftward (i.e. to the left side) on a leaf ‘R’ : a frog facing rightward (i.e. to the right side) on a leaf You can assume that there is only one frog in each input. You can also assume that the total number of leaves (including the leaf the frog is initially on) is at most 30. Output Output a line consists of the characters ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right) that describes a series of movements. The output should not contain any other characters, such as spaces. You can assume that there exists only one solution for each input. Sample Input 1 2 3 Uo. .oo Output for the Sample Input 1 RDR Sample Input 2 10 10 .o....o... o.oo...... ..oo..oo.. ..o....... ..oo..oo.. ..o...o.o. o..U.o.... oo......oo oo........ oo..oo.... Output for the Sample Input 2 URRULULDDLUURDLLLURRDLDDDRRDR Sample Input 3 10 1 D . . . . . . . . o Output for the Sample Input 3 D
[ { "submission_id": "aoj_2223_10684773", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#incl...
aoj_2224_cpp
Problem C: Save your cats Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them. One day, an evil witch visited the village. She envied the cats for being loved by everyone. She drove magical piles in his garden and enclosed the cats with magical fences running between the piles. She said “Your cats are shut away in the fences until they become ugly old cats.” like a curse and went away. Nicholas tried to break the fences with a hummer, but the fences are impregnable against his effort. He went to a church and asked a priest help. The priest looked for how to destroy the magical fences in books and found they could be destroyed by holy water. The Required amount of the holy water to destroy a fence was proportional to the length of the fence. The holy water was, however, fairly expensive. So he decided to buy exactly the minimum amount of the holy water required to save all his cats. How much holy water would be required? Input The input has the following format: N M x 1 y 1 . . . x N y N p 1 q 1 . . . p M q M The first line of the input contains two integers N (2 ≤ N ≤ 10000) and M (1 ≤ M ). N indicates the number of magical piles and M indicates the number of magical fences. The following N lines describe the coordinates of the piles. Each line contains two integers x i and y i (-10000 ≤ x i , y i ≤ 10000). The following M lines describe the both ends of the fences. Each line contains two integers p j and q j (1 ≤ p j , q j ≤ N ). It indicates a fence runs between the p j -th pile and the q j -th pile. You can assume the following: No Piles have the same coordinates. A pile doesn’t lie on the middle of fence. No Fences cross each other. There is at least one cat in each enclosed area. It is impossible to destroy a fence partially. A unit of holy water is required to destroy a unit length of magical fence. Output Output a line containing the minimum amount of the holy water required to save all his cats. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 0.001 or less. Sample Input 1 3 3 0 0 3 0 0 4 1 2 2 3 3 1 Output for the Sample Input 1 3.000 Sample Input 2 4 3 0 0 -100 0 100 0 0 100 1 2 1 3 1 4 Output for the Sample Input 2 0.000 Sample Input 3 6 7 2 0 6 0 8 2 6 3 0 5 1 7 1 2 2 3 3 4 4 1 5 1 5 4 5 6 Output for the Sample Input 3 7.236 Sample Input 4 6 6 0 0 0 1 1 0 30 0 0 40 30 40 1 2 2 3 3 1 4 5 5 6 6 4 Output for the Sample Input 4 31.000
[ { "submission_id": "aoj_2224_10848478", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <vector>\nusing namespace std;\nconst int maxn=1e4+5;\ntypedef long long LL;\nstruct Point\n{\n int x;\n int y;\n};\n\nstruct edge\n{\n int to;\n doubl...
aoj_2231_cpp
Problem J: Cruel Bingo Bingo is a party game played by many players and one game master. Each player is given a bingo card containing N 2 different numbers in a N × N grid (typically N = 5). The master draws numbers from a lottery one by one during the game. Each time a number is drawn, a player marks a square with that number if it exists. The player’s goal is to have N marked squares in a single vertical, horizontal, or diagonal line and then call “Bingo!” The first player calling “Bingo!” wins the game. In ultimately unfortunate cases, a card can have exactly N unmarked squares, or N ( N -1) marked squares, but not a bingo pattern. Your task in this problem is to write a program counting how many such patterns are possible from a given initial pattern, which contains zero or more marked squares. Input The input is given in the following format: N K x 1 y 1 . . . x K y K The input begins with a line containing two numbers N (1 ≤ N ≤ 32) and K (0 ≤ K ≤ 8), which represent the size of the bingo card and the number of marked squares in the initial pattern respectively. Then K lines follow, each containing two numbers x i and y i to indicate the square at ( x i , y i ) is marked. The coordinate values are zero-based (i.e. 0 ≤ x i , y i ≤ N - 1). No pair of marked squares coincides. Output Count the number of possible non-bingo patterns with exactly N unmarked squares that can be made from the given initial pattern, and print the number in modulo 10007 in a line (since it is supposed to be huge). Rotated and mirrored patterns should be regarded as different and counted each. Sample Input 1 4 2 0 2 3 1 Output for the Sample Input 1 6 Sample Input 2 4 2 2 3 3 2 Output for the Sample Input 2 6 Sample Input 3 10 3 0 0 4 4 1 4 Output for the Sample Input 3 1127
[ { "submission_id": "aoj_2231_4303458", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MOD 10007\n\...
aoj_2227_cpp
Problem F: Bouldering Bouldering is a style of rock climbing. Boulderers are to climb up the rock with bare hands without supporting ropes. Your friend supposed that it should be interesting and exciting, so he decided to come to bouldering gymnasium to practice bouldering. Since your friend has not tried bouldering yet, he chose beginner’s course. However, in the beginner’s course, he found that some of two stones have too distant space between them, which might result his accidentally failure of grabbing certain stone and falling off to the ground and die! He gradually becomes anxious and wonders whether the course is actually for the beginners. So, he asked you to write the program which simulates the way he climbs up and checks whether he can climb up to the goal successfully. For the sake of convenience, we assume that a boulderer consists of 5 line segments, representing his body, his right arm, his left arm, his right leg, and his left leg. One of his end of the body is connected to his arms on their end points. And the other end of the body is connected to his legs on their end points, too. The maximum length of his body, his arms, and his legs are A , B and C respectively. He climbs up the wall by changing the length of his body parts from 0 to their maximum length, and by twisting them in any angle (in other word, 360 degrees). Refer the following figure representing the possible body arrangements. Figure 2: An example of possible body arrangements. 5 line segments representing his body, arms and legs. The length of his body, arms and legs are 8, 3 and 4 respectively. The picture describes his head as a filled circle for better understanding, which has no meaning in this problem. A boulderer climbs up the wall by grabbing at least three different rocks on the wall with his hands and feet. In the initial state, he holds 4 rocks with his hands and feet. Then he changes one of the holding rocks by moving his arms and/or legs. This is counted as one movement. His goal is to grab a rock named “destination rock” with one of his body parts. The rocks are considered as points with negligible size. You are to write a program which calculates the minimum number of movements required to grab the destination rock. Input The input data looks like the following lines: n A B C x 1 y 1 x 2 y 2 x 3 y 3 . . . x n y n The first line contains n (5 ≤ n ≤ 30), which represents the number of rocks. The second line contains three integers A , B , C (1 ≤ A , B , C ≤ 50), which represent the length of body, arms, and legs of the climber respectively. The last n lines describes the location of the rocks. The i -th contains two integers x i and y i (0 ≤ x i , y i ≤ 100), representing the x and y -coordinates of the i -th rock. In the initial state, the boulderer is grabbing the 1st rock with his right hand, 2nd with his left hand, 3rd with his right foot, and 4th with left foot. You may assume that the first 4 rocks are close to each other so that he can grab them ...(truncated)
[ { "submission_id": "aoj_2227_10852749", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst double eps = 1e-3;\n\nconst double pi =acos(-1.0);\n\nstruct Point\n{\n double x,y;\n Point() {...
aoj_2225_cpp
Problem D: Dungeon Wall In 2337, people are bored at daily life and have crazy desire of having extraordinary experience. These days,“Dungeon Adventure” is one of the hottest attractions, where brave adventurers risk their lives to kill the evil monsters and save the world. You are a manager of one of such dungeons. Recently you have been receiving a lot of complaints from soldiers who frequently enter your dungeon; they say your dungeon is too easy and they do not want to enter again. You are considering making your dungeon more difficult by increasing the distance between the entrance and the exit of your dungeon so that more monsters can approach the adventurers. The shape of your dungeon is a rectangle whose width is W and whose height is H . The dungeon is a grid which consists of W × H square rooms of identical size 1 × 1. The southwest corner of the dungeon has the coordinate (0; 0), and the northeast corner has ( W , H ). The outside of the dungeon is surrounded by walls, and there may be some more walls inside the dungeon in order to prevent adventurers from going directly to the exit. Each wall in the dungeon is parallel to either x -axis or y -axis, and both ends of each wall are at integer coordinates. An adventurer can move from one room to another room if they are adjacent vertically or horizontally and have no wall between. You would like to add some walls to make the shortest path between the entrance and the exit longer. However, severe financial situation only allows you to build at most only one wall of a unit length. Note that a new wall must also follow the constraints stated above. Furthermore, you need to guarantee that there is at least one path from the entrance to the exit. You are wondering where you should put a new wall to maximize the minimum number of steps between the entrance and the exit. Can you figure it out? Input W H N sx 0 sy 0 dx 0 dy 0 sx 1 sy 1 dx 1 dy 1 . . . ix iy ox oy The first line contains three integers: W , H and N (1 ≤ W , H ≤ 50, 0 ≤ N ≤ 1000). They are separated with a space. The next N lines contain the specifications of N walls inside the dungeon. Each wall specification is a line containing four integers. The i -th specification contains sx i , sy i , dx i and dy i , separated with a space. These integers give the position of the i -th wall: it spans from ( sx i , sy i ) to ( dx i , dy i ). The last two lines contain information about the locations of the entrance and the exit. The first line contains two integers ix and iy , and the second line contains two integers ox and oy . The entrance is located at the room whose south-west corner is ( ix , iy ), and the exit is located at the room whose southwest corner is ( ox , oy ). Output Print the maximum possible increase in the minimum number of required steps between the entrance and the exit just by adding one wall. If there is no good place to build a new wall, print zero. Sample Input 1 3 4 4 1 0 1 1 1 3 1 4 1 2 2 2 2 1 2 3 0 0 1 0 Output ...(truncated)
[ { "submission_id": "aoj_2225_10853847", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<math.h>\n#include<algorithm>\nusing namespace std;\n#define maxw 55\n#define maxn 1010\n\nint n;\nint w;\nint h;\nbool wall[maxw][maxw][4];\nint mp[maxw][maxw];\nint ix,iy;\nint ox,oy;\nint que[maxw*maxw...
aoj_2226_cpp
Problem E: Psychic Accelerator In the west of Tokyo, there is a city named “Academy City.” There are many schools and laboratories to develop psychics in Academy City. You are a psychic student of a school in Academy City. Your psychic ability is to give acceleration to a certain object. You can use your psychic ability anytime and anywhere, but there are constraints. If the object remains stationary, you can give acceleration to the object in any direction. If the object is moving, you can give acceleration to the object only in 1) the direction the object is moving to, 2) the direction opposite to it, or 3) the direction perpendicular to it. Today’s training menu is to move the object along a given course. For simplicity you can regard the course as consisting of line segments and circular arcs in a 2-dimensional space. The course has no branching. All segments and arcs are connected smoothly, i.e. there are no sharp corners. In the beginning, the object is placed at the starting point of the first line segment. You have to move the object to the ending point of the last line segment along the course and stop the object at that point by controlling its acceleration properly. Before the training, a coach ordered you to simulate the minimum time to move the object from the starting point to the ending point. Your task is to write a program which reads the shape of the course and the maximum acceleration a max you can give to the object and calculates the minimum time to move the object from the starting point to the ending point. The object follows basic physical laws. When the object is moving straight in some direction, with acceleration either forward or backward, the following equations hold: v = v 0 + at and s = v 0 t + (1/2) at 2 where v , s , v 0 , a , and t are the velocity, the distance from the starting point, the initial velocity (i.e. the velocity at the starting point), the acceleration, and the time the object has been moving in that direction, respectively. Note that they can be simplified as follows: v 2 − v 0 2 = 2 as When the object is moving along an arc, with acceleration to the centroid, the following equations hold: a = v 2 / r wher v , a , and r are the velocity, the acceleration, and the radius of the arc, respectively. Note that the object cannot change the velocity due to the criteria on your psychic ability. Input The input has the following format: N a max x a ,1 y a ,1 x b ,1 y b ,1 x a ,2 y a ,2 x b ,2 y b ,2 . . . N is the number of line segments; a max is the maximum acceleration you can give to the object; ( x a,i , y a,i ) and ( x b,i , y b,i ) are the starting point and the ending point of the i -th line segment, respectively. The given course may have crosses but you cannot change the direction there. The input meets the following constraints: 0 < N ≤ 40000, 1 ≤ a max ≤ 100, and -100 ≤ x a i , y a i , x b i , y b i ≤ 100. Output Print the minimum time to move the object from the starting point to the ending poi ...(truncated)
[ { "submission_id": "aoj_2226_10851598", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cctype>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <map>\n#include <algorithm>\n#include <iostream>\n#include <string>\n#include <set>\n#defin...
aoj_2230_cpp
Problem I: How to Create a Good Game A video game company called ICPC (International Company for Playing and Competing) is now developing a new arcade game. The new game features a lot of branches. This makes the game enjoyable for everyone, since players can choose their routes in the game depending on their skills. Rookie players can choose an easy route to enjoy the game, and talented players can choose their favorite routes to get high scores. In the game, there are many checkpoints connected by paths. Each path consists of several stages, and completing stages on a path leads players to the next checkpoint. The game ends when players reach a particular checkpoint. At some checkpoints, players can choose which way to go, so routes diverge at that time. Sometimes different routes join together at a checkpoint. The paths between checkpoints are directed, and there is no loop (otherwise, players can play the game forever). In other words, the structure of the game can be viewed as a DAG (directed acyclic graph), when considering paths between checkpoints as directed edges. Recently, the development team completed the beta version of the game and received feedbacks from other teams. They were quite positive overall, but there are some comments that should be taken into consideration. Some testers pointed out that some routes were very short compared to the longest ones. Indeed, in the beta version, the number of stages in one play can vary drastically depending on the routes. Game designers complained many brilliant ideas of theirs were unused in the beta version because of the tight development schedule. They wanted to have more stages included in the final product. However, it’s not easy to add more stages. this is an arcade game – if the playing time was too long, it would bring down the income of the game and owners of arcades would complain. So, the longest route of the final product can’t be longer than that of the beta version. Moreover, the producer of the game didn’t want to change the structure of paths (i.e., how the checkpoints connect to each other), since it would require rewriting the scenario, recording voices, creating new cutscenes, etc. Considering all together, the producer decided to add as many new stages as possible, while keeping the maximum possible number of stages in one play and the structure of paths unchanged. How many new stages can be added to the game? Input N M x 1 y 1 s 1 . . . x M y M s M The first line of the input contains two positive integers N and M (2 ≤ N ≤ 100, 1 ≤ M ≤ 1000). N indicates the number of checkpoints, including the opening and ending of the game. M indicates the number of paths between checkpoints. The following M lines describe the structure of paths in the beta version of the game. The i -th line contains three integers x i , y i and s i (0 ≤ x i < y i ≤ N - 1, 1 ≤ s i ≤ 1000), which describe that there is a path from checkpoint x i to y i consists of si stages. As for indices of the check ...(truncated)
[ { "submission_id": "aoj_2230_10850465", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cmath>\n#include<algorithm>\n#define add(a,b,c) (e[i]=b,pre[i]=last[a],last[a]=i,w[i]=c)\n#define prec 1e-6\n#define inf 1e9\nusing namespace std;\nstruct edge\n{ int u,v,w;\n edge(int u=0,int v=0,int...
aoj_2229_cpp
Problem H: Ropeway On a small island, there are two towns Darkside and Sunnyside, with steep mountains between them. There’s a company Intertown Company of Package Conveyance; they own a ropeway car between Darkside and Sunnyside and are running a transportation business. They want maximize the revenue by loading as much package as possible on the ropeway car. The ropeway car looks like the following. It’s L length long, and the center of it is hung from the rope. Let’s suppose the car to be a 1-dimensional line segment, and a package to be a point lying on the line. You can put packages at anywhere including the edge of the car, as long as the distance between the package and the center of the car is greater than or equal to R and the car doesn’t fall down. The car will fall down when the following condition holds: Here N is the number of packages; m i the weight of the i -th package; x i is the position of the i -th package relative to the center of the car. You, a Darkside programmer, are hired as an engineer of the company. Your task is to write a program which reads a list of package and checks if all the packages can be loaded in the listed order without the car falling down at any point. Input The input has two lines, describing one test case. The first line contains four integers N , L , M , R . The second line contains N integers m 0 , m 1 , ... m N -1 . The integers are separated by a space. Output If there is a way to load all the packages, output “Yes” in one line, without the quotes. Otherwise, output “No”. Sample Input 1 3 3 2 1 1 1 4 Output for the Sample Input 1 Yes Sample Input 2 3 3 2 1 1 4 1 Output for the Sample Input 2 No
[ { "submission_id": "aoj_2229_1574246", "code_snippet": "#include <cstdlib>\n#include <cctype>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#incl...
aoj_2232_cpp
Problem A: Ennichi 縁日に来たうさぎが, ある出店のゲームの景品がキャロットケーキであるのを見つけた. このゲームのルールは以下の通りである. 縦h マス× 横w マスの格子状のフィールドがあり, 各マスにつき高々1 個のブロックが置かれる. 各ブロックにはアルファベット大文字(‘A’ - ‘Z’) のいずれかで表される色がついている. 同色のブロックが縦または横に一直線上にn 個以上連続して並ぶと, それらのブロックは消滅する. 参加者は横に隣り合う2 つのマスを選んで, それらの状態を互いに入れ替える操作ができる. ブロックの入れ替え, 消滅, 落下によってブロックの有るマスの1 つ下のマスにブロックが無くなると, このブロックは落下する. このときふたたび同色のブロックがn 個以上並ぶと消滅する. ただし, ブロックの消滅は, 落下するブロックが存在する間は起こらず, すべてのブロックの落下が終了したタイミングで同時に起こる. 1 回の操作でフィールド上のすべてのブロックを消滅させると, このゲームは成功となり景品のケーキを得ることができる. うさぎは1 回分の参加費で確実にケーキを手に入れたく, それができない場合は参加したくない. ゲーム開始時のフィールドの状態から, うさぎがこのゲームに参加すべきであるか答えよ. Input 入力の一行目にはh, w, n がスペースで区切られて与えられる. 2 ≤ h , w , n ≤ 30 続く h 行にはフィールドの状態が上から順に与えられる. アルファベット大文字はブロックを, ‘.’ は空きマスを表す. 与えられるフィールドの状態には, 縦あるいは横に n 個以上連続する同色のブロックはなく, 落下する状態にあるブロックもない. 1 個以上のブロックが存在する. Output うさぎがこのゲームに参加すべきであるなら”YES”を, そうでないなら”NO”を一行に出力せよ. Sample Input 1 4 6 3 ...... ...Y.. ...Y.. RRYRYY Sample Output 2 YES Sample Input 1 4 6 3 ...... ...Y.. ...Y.. RRYRY. Sample Output 2 NO
[ { "submission_id": "aoj_2232_11059717", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b...
aoj_2234_cpp
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
[ { "submission_id": "aoj_2234_7242501", "code_snippet": "#include <iostream>\n#include <map>\n#include <vector>\nusing namespace std;\n\nlong long GCD(long long a, long long b) {\n\tif (b == 0) return a;\n\treturn GCD(b, a % b);\n}\n\nlong long GetAnswer(long long H, long long W, long long A, long long B, lo...
aoj_2233_cpp
Problem B: Carrot Tour うさぎがある国を旅行している. この国には1 から n の番号がついた n 個の都市があり, うさぎは今都市1にいる. 都市 i は座標平面上の1 点( x i , y i ) とみなす. うさぎは以下の条件をみたすように旅をする. 移動経路は折れ線であり, その各部分は異なる2 都市を結ぶ線分でなければならない. 移動経路の全長は r 以下でなければならない. 経路のうち重なった部分も, 通った回数分数える. 移動する方向が変わるとき, 曲がる角度は θ 以下でなければならない. 最初の移動方向に制限はない. うさぎがある都市から別の都市へ移動をすると, 移動先の都市でニンジンを1 本もらえる. 同じ都市を複数回訪れることは可能であり, 訪れるたびにニンジンをもらえる. うさぎがこの旅で手に入れることのできるニンジンの本数の最大値を求めよ. Input 入力の一行目には一つの整数 n が, 二行目には二つの実数 r , θ がスペースで区切られて与えられる. 1 ≤ n ≤ 20 0 < r < 10 4 0° < θ < 180° 続く n 行には, 整数 x i , y i がスペースで区切られて与えられる -10 000 ≤ x i , y i ≤ 10 000 r , θ を±10 −3 以内で変化させても答えは変わらない. どの2 つの都市の位置も異なる. Output うさぎがこの旅で手に入れることのできるニンジンの本数の最大値を一行に出力せよ. Sample Input 1 5 100.1 90.1 0 0 0 10 5 5 10 0 10 10 Sample Output 1 10
[ { "submission_id": "aoj_2233_10880835", "code_snippet": "#define _USE_MATH_DEFINES\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint x[22], y[22];\ndouble r, theta;\n\nbool check_angle(int p, int i, int n) {\n\tdouble a1 = x[i] - x[p], a2 = y[i] - y[p];\n\tdouble b1 = x[n] - x[i],...
aoj_2238_cpp
Problem G: Nurie 紙の上に円が n 個書かれている. うさぎは k 色の絵の具を持っていて, 次のルールに従って紙に色を塗っていく. 各領域をある1 色の絵の具で塗るか, 何も塗らない. ここで「領域」とは, 円弧の集合で囲まれた面積有限の部分を指す. 隣り合っている2 つの領域を同じ色の絵の具で塗ることはできない. ここで「隣り合っている」とは,境界の一部を共有する円弧があることを指す. 共有点が有限個である2 つの領域は, 隣り合っているとはみなされない. 隣り合っている2 つの領域の両方を塗らないままにすることは許される. うさぎはできるだけ多くの領域を絵の具で塗りたい. 塗れる領域の個数の最大値を求めよ. Input 1 行目: n k (1 ≤ n ≤ 20, 1 ≤ k ≤ 1 000 000 000) 2-( n + 1) 行目: x i y i r i , (−1 000 ≤ x i , y i ≤ 1 000, 1 ≤ r i ≤ 1 000) (整数) どの2 つの円も一致しない. いずれか2 円の交点として得られる点集合は, 距離が10 −3 以下の異なる2 点を含まない. Output 絵の具で塗れる領域の個数の最大値を一行に出力せよ. Sample Input 1 2 1 10 0 10 20 0 10 Sample Output 1 2
[ { "submission_id": "aoj_2238_2266327", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-8)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp ma...
aoj_2239_cpp
Problem H: Nearest Station うさぎがある電車のチケットを n 枚持っている. チケットにはそれぞれ0 から n − 1 までの番号がついていて, k 番のチケットを使うと, p ⋅ a k + q ⋅ b k 駅進むことができる. うさぎは今いる駅から m 駅進んだ駅にあるニンジン食べ放題の店に行きたいが, なるべく歩く距離を短くしたい. 駅は等間隔に並んでいる. チケットを電車の上り線で進むことのみに用いるとき, うさぎは最小何駅分の徒歩で店に着けるか. Input 1 ≤ n , m , a , b , p , q ≤ 1 000 000 000 000 (整数) Output うさぎは最小何駅分の徒歩で店に着けるか, その数を一行に出力せよ. Sample Input 1 6 200 2 3 4 5 Sample Output 1 1 Sample Input 2 6 1 2 3 4 5 Sample Output 2 1
[ { "submission_id": "aoj_2239_10866027", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VS = vector<string>; using LL = long long;\nusing VI = vector<int>; using VVI = vector<VI>;\nusing PII = pair<int, int>; using PLL = pair<LL, LL>;\nusing VL = vector<LL>; using ...
aoj_2236_cpp
Problem E: Rabbit Plays Games! うさぎがとあるロールプレイングゲームで遊んでいる. 城に入る直前で, 敵に待ち伏せされていた! うさぎが操作する主人公1 人と, n 体の敵との戦闘となった. 各キャラクターには4 つの能力値, 体力 h i , 攻撃力 a i , 防御力 d i , 敏捷 s i が定められている. i = 0 は主人公の情報, 1 ≤ i ≤ n は各敵の情報を表す. 戦闘はターン制である. 各ターン, 生き残っているキャラクターが, 敏捷の値が高い順に攻撃を行う. 敵は必ず主人公を攻撃する. 主人公は敵1 体を攻撃するが, どの敵を攻撃するかは毎ターンごとに主人公が選べる.攻撃力 a のキャラクターが防御力 d のキャラクターに攻撃するとき, max{ a − d , 0} のダメージが与えられる. 受けたダメージの合計が体力の値以上になったキャラクターは直ちに戦闘不能になってしまう. 主人公が戦闘不能になったとき, あるいは敵がすべて戦闘不能になったとき, 戦闘は終了する. Input 1 ≤ n ≤ 40 000 1 ≤ h i , a i , d i , s i ≤ 1 000 000 000 (整数) s i はすべて異なる. Output 主人公が必ず戦闘不能になってしまうとき, −1 を出力せよ. そうでないとき, 主人公が受けるダメージの合計の最小値を一行に出力せよ. Sample Input 1 2 10 3 1 2 2 4 1 3 2 2 1 1 Sample Output 1 4 Sample Input 2 1 1 1 1 1 10000 10000 10000 10000 Sample Output 2 -1
[ { "submission_id": "aoj_2236_10511586", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = ...
aoj_2237_cpp
Problem F: The Castle 待ち伏せていた敵を見事撃破したうさぎは, 主人公を敵の城内に進めることに成功した. 主人公が城の地下牢に捕らわれていたねこたちを解放したところ, 彼らのうちの何匹かが主人公の力になってくれることになった. ねこたちの話では, 城の奥にいる魔王に辿りつくには1 番から n 番の n 個の部屋をこの順に通り抜けることになるが, 各部屋には1 体ずつ敵が待ち受けていて逐一倒していかなければならないという. 仲間になった m 匹のねこそれぞれについて, 各部屋の敵それぞれに対する勝率が分かっており, 主人公はこのねこたちを1 匹ずつ城の奥へ向けて派遣する. 各部屋はそこにいる敵を倒してからでなければ通過できないので, あるねこがある部屋の敵にやられたら, 次のねこはその部屋の敵から戦っていくことになる. 派遣されたねこは敵にやられるまで進むが, 派遣したねこがどの部屋の敵にやられたかは毎回知ることができ, それによって次にどのねこを派遣するかを決めることができる. どのようにすれば, ねこたちが待ち受けるすべての敵を倒せる確率が最大になるだろうか. Input 入力の一行目には m と n がスペースで区切られて与えられる. 1 ≤ m , n ≤ 16 つづく m 行には, 猫が敵に勝つ確率を表す n 個の実数が与えられる. i + 1 行目の j 個目の実数は, j 番の部屋の敵に勝つ確率を表している. 確率は小数点以下3 桁まで. Output ねこたちが待ち受けるすべての敵を倒せる確率が最大になるようにうまく順番を決めていくとき, その確率を答えよ. 出力は誤差を含んでいてもよいが, 真の値との誤差は10 −9 以内にせよ. Sample Input 1 2 3 0.900 0.500 0.100 0.500 0.500 0.500 Sample Output 1 0.372500000000
[ { "submission_id": "aoj_2237_10373053", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #includ...
aoj_2235_cpp
Problem D: Graph Construction n 匹のうさぎがいて, 0 番から n − 1 番の番号がついた小屋に1 匹ずつ住んでいる. あるとき, 秘密の組織によって地下通路の建設工事が行われる, という情報がうさぎたちのもとへ入った. 地下通路を使えば, うさぎたちは他のうさぎたちの小屋へ遊びに行けるようになって嬉しい. 通路は両方向に進むことができ, また通路同士は交わらない. 諸事情により, 一度建設された通路が破壊されてしまうこともある. 通路の建設や破壊の工事は1 本ずつ行われ, 各工事中はうさぎは自分の小屋に留まっているものとする. うさぎたちは, 工事のいくつかの段階において, あるうさぎとあるうさぎが遊べるかどうかを事前に知りたい. うさぎたちは仲良しなので, 遊びに行くときに他のうさぎの小屋を自由に通ることができる. プログラミングが好きなうさぎたちは, 昔似たような問題を解いたので簡単だろうと思って挑戦し出したが, なかなか効率の良いプログラムが書けない. うさぎの代わりにこの問題を解け. Input 入力の一行目には n と k がスペース区切りで与えられる。2 ≤ n ≤ 40 000, 1 ≤ k ≤ 40 000 つづく k 行には工事情報と質問が合わせて, 時間順に与えられる. “1 u v ” — 小屋 u と v を結ぶ通路が建設される. 小屋 u と v を結ぶ通路がないときにのみ現れる. “2 u v ” — 小屋 u と v を結ぶ通路が破壊される. 小屋 u と v を結ぶ通路があるときにのみ現れる. “3 u v ” — 小屋 u と v のうさぎが遊べるかどうか判定せよ. 0 ≤ u < v < n Output 入力に現れる各質問について, 遊べるなら”YES”を, そうでないなら”NO”を1 行ずつ出力せよ. Sample Input 1 4 10 1 0 1 1 0 2 3 1 2 2 0 1 1 2 3 3 0 1 1 0 1 2 0 2 1 1 3 3 0 2 Sample Output 1 YES NO YES
[ { "submission_id": "aoj_2235_10456769", "code_snippet": "// competitive-verifier: PROBLEM\n#include <iostream>\n#include <utility>\n#include <vector>\n#include <algorithm>\n#include <bit>\n#include <cassert>\n#include <map>\n#include <stack>\n/**\n * @brief Undo可能Union-Find\n * @details Implement (union by ...
aoj_2240_cpp
Problem I: Lapin Noir ここは, 地面が正六角形を敷き詰めたマス目になっている街である. 各マスは, 下の図のように2 つの整数で表される. ねこは(0, 0) のマスへ行こうとしている. いたずら好きの黒うさぎはこのことを知って, ねこの邪魔をしようと考えた. 黒うさぎはねこがいるマスへ跳び, その周りの6 マスのうち1 つまたは隣り合う2 つをブロックすることができる. ねこは周りの6 マスのうち, ブロックされていないマスを1 つ選んで進む. ねこが動くたび黒うさぎはねこを追いかけ, またブロックを行うことができる. 黒うさぎが動くと元のブロックは解除される. 下の図はブロックの様子を示している. この街にはねこの縄張りとなっているマス目がいくつかある. 縄張りは正六角形の周をなすマス目の集合 n 個の合併として表される. 黒うさぎはねこの縄張りに立ち入ることはできるが, ブロックすることはできない.つまり, ねこは縄張り内であれば自由に動ける. ねこの出発点の候補が k 個あり, マス( x i , y i ) (1 ≤ i ≤ k ) である. それぞれについて, ねこが必ず目的地(0, 0) に辿り着けるか, あるいは黒うさぎがうまく妨害することによってねこが目的地に辿り着けないかを判定せよ. Input 1 行目:1 ≤ n ≤ 40 000 2 ∼ ( n + 1) 行目: ねこの縄張りを表す正六角形の頂点 ( n + 2) 行目: 1 ≤ k ≤ 40 000 ( n + 3) ∼ ( n + k + 2) 行目: ねこの出発点 x i , y i −1 000 000 000 ≤ (各座標) ≤ 1 000 000 000 ねこの縄張りを表す正六角形は, 6 頂点の位置が反時計回りに与えられる. Output 出発地の候補それぞれについて, ねこが必ず目的地(0,0) に辿り着けるなら”YES”を, あるいは黒うさぎがうまく妨害した場合ねこが目的地に辿り着けないなら”NO”を, 一行ずつ出力せよ. Sample Input 1 2 1 -1 3 -1 3 1 1 3 -1 3 -1 1 3 0 4 0 4 1 3 2 2 2 2 1 3 1 1 -1 -1 2 4 Sample Output 1 YES NO YES
[ { "submission_id": "aoj_2240_4872355", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\n...
aoj_2241_cpp
Problem J: Usaneko Matrix うさぎとねこが勝負をしている. ルールは以下の通りである. まず2 匹はそれぞれ n 行 n 列の正方形状に n 2 個の整数を紙に書き, トランプを1 枚ずつ引く. 次に, 1 から 1 000 000 までの数が1 つずつ書かれた1 000 000 枚のカードを2 匹でシャッフルし, これを1 枚ずつ交互に引いていく. 2 匹はカードが引かれるたび, カードと同じ数が自分の紙に書かれていたらそれに印をつける. 「印がついた n 個の数の組であって, 一直線上に並んでいるもの」の個数が, はじめに引いたトランプの数以上になることを勝利条件とする. 与えられた m 枚目のカードまでで, うさぎとねこのどちらが勝つか答えよ. ただし勝敗は, あるカードが引かれて印をつけ終わった段階で2 匹のうち片方のみが勝利条件をみたしたときに決まるものとし, それ以外の場合は引き分けとする. いずれかが勝利条件をみたした後でもカードが引かれることはあるが, これは勝敗に影響しない. Input 1 行目:“ n u v m ” (正方形のサイズ, うさぎのトランプの数, ねこのトランプの数, 引かれるカードの枚数) 2-( N + 1) 行目:うさぎが紙に書く n 2 個の数( N + 2)-(2 N + 1) 行目:ねこが紙に書く n 2 個の数(2 N + 2)-(2 N + M + 1) 行目:引かれるm 枚のカード 1 ≤ n ≤ 500 1 ≤ u , v ≤ 13 1 ≤ m ≤ 100 000 1 ≤ (書かれる数) ≤ 1 000 000 うさぎが紙に書く n 2 個の数, ねこが紙に書く n 2 個の数, 引かれる m 枚のカードに書かれた数はそれぞれの中で異なる. Output うさぎが勝つ場合には”USAGI”を, ねこが勝つ場合には”NEKO”を, 引き分けならば”DRAW”, それぞれ 一行に出力せよ. Sample Input 1 3 2 2 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Sample Output 1 USAGI Sample Input 2 3 2 1 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Sample Output 2 DRAW
[ { "submission_id": "aoj_2241_10850418", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < (n); i++)\n#define int long long\n#define FOR(i, m, n) for (int i = (m); i < (n); i++)\n#define all(x) x.begin(), x.end()\nusing pii = pair<int, int>;\n\nint A[5...
aoj_2242_cpp
Problem A: Era Name As many of you know, we have two major calendar systems used in Japan today. One of them is Gregorian calendar which is widely used across the world. It is also known as “Western calendar” in Japan. The other calendar system is era-based calendar, or so-called “Japanese calendar.” This system comes from ancient Chinese systems. Recently in Japan it has been a common way to associate dates with the Emperors. In the era-based system, we represent a year with an era name given at the time a new Emperor assumes the throne. If the era name is “A”, the first regnal year will be “A 1”, the second year will be “A 2”, and so forth. Since we have two different calendar systems, it is often needed to convert the date in one calendar system to the other. In this problem, you are asked to write a program that converts western year to era-based year, given a database that contains association between several western years and era-based years. For the simplicity, you can assume the following: A new era always begins on January 1st of the corresponding Gregorian year. The first year of an era is described as 1. There is no year in which more than one era switch takes place. Please note that, however, the database you will see may be incomplete. In other words, some era that existed in the history may be missing from your data. So you will also have to detect the cases where you cannot determine exactly which era the given year belongs to. Input The input contains multiple test cases. Each test case has the following format: N Q EraName 1 EraBasedYear 1 WesternYear 1 . . . EraName N EraBasedYear N WesternYear N Query 1 . . . Query Q The first line of the input contains two positive integers N and Q (1 ≤ N ≤ 1000, 1 ≤ Q ≤ 1000). N is the number of database entries, and Q is the number of queries. Each of the following N lines has three components: era name, era-based year number and the corresponding western year (1 ≤ EraBasedYear i ≤ WesternYear i ≤ 10 9 ). Each of era names consist of at most 16 Roman alphabet characters. Then the last Q lines of the input specifies queries (1 ≤ Query i ≤ 10 9 ), each of which is a western year to compute era-based representation. The end of input is indicated by a line containing two zeros. This line is not part of any dataset and hence should not be processed. You can assume that all the western year in the input is positive integers, and that there is no two entries that share the same era name. Output For each query, output in a line the era name and the era-based year number corresponding to the western year given, separated with a single whitespace. In case you cannot determine the era, output “ Unknown ” without quotes. Sample Input 4 3 meiji 10 1877 taisho 6 1917 showa 62 1987 heisei 22 2010 1868 1917 1988 1 1 universalcentury 123 2168 2010 0 0 Output for the Sample Input meiji 1 taisho 6 Unknown Unknown
[ { "submission_id": "aoj_2242_11059750", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b...
aoj_2245_cpp
Problem D: Dice Room You are playing a popular video game which is famous for its depthful story and interesting puzzles. In the game you were locked in a mysterious house alone and there is no way to call for help, so you have to escape on yours own. However, almost every room in the house has some kind of puzzles and you cannot move to neighboring room without solving them. One of the puzzles you encountered in the house is following. In a room, there was a device which looked just like a dice and laid on a table in the center of the room. Direction was written on the wall. It read: "This cube is a remote controller and you can manipulate a remote room, Dice Room, by it. The room has also a cubic shape whose surfaces are made up of 3x3 unit squares and some squares have a hole on them large enough for you to go though it. You can rotate this cube so that in the middle of rotation at least one edge always touch the table, that is, to 4 directions. Rotating this cube affects the remote room in the same way and positions of holes on the room change. To get through the room, you should have holes on at least one of lower three squares on the front and back side of the room." You can see current positions of holes by a monitor. Before going to Dice Room, you should rotate the cube so that you can go though the room. But you know rotating a room takes some time and you don’t have much time, so you should minimize the number of rotation. How many rotations do you need to make it possible to get though Dice Room? Input The input consists of several datasets. Each dataset contains 6 tables of 3x3 characters which describe the initial position of holes on each side. Each character is either ' * ' or ' . '. A hole is indicated by ' * '. The order which six sides appears in is: front, right, back, left, top, bottom. The order and orientation of them are described by this development view: Figure 4: Dice Room Figure 5: available dice rotations There is a blank line after each dataset. The end of the input is indicated by a single ' # '. Output Print the minimum number of rotations needed in a line for each dataset. You may assume all datasets have a solution. Sample Input ... ... .*. ... ... .*. ... ... ... ... ... .*. ... ... .*. ... ... ... ... .*. ... *.. ... ..* *.* *.* *.* *.* .*. *.* *.. .*. ..* *.* ... *.* ... .*. .*. ... .** *.. ... ... .*. .*. ... *.. ..* ... .** ... *.. ... # Output for the Sample Input 3 1 0
[ { "submission_id": "aoj_2245_4879027", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"uno...
aoj_2243_cpp
Problem B: Step Step Evolution Japanese video game company has developed the music video game called Step Step Evolution. The gameplay of Step Step Evolution is very simple. Players stand on the dance platform, and step on panels on it according to a sequence of arrows shown in the front screen. There are eight types of direction arrows in the Step Step Evolution: UP, UPPER RIGHT, RIGHT, LOWER RIGHT, DOWN, LOWER LEFT, LEFT, and UPPER LEFT. These direction arrows will scroll upward from the bottom of the screen. When the direction arrow overlaps the stationary arrow nearby the top, then the player must step on the corresponding arrow panel on the dance platform. The figure below shows how the dance platform looks like. Figure 1: the dance platform for Step Step Evolution In the game, you have to obey the following rule: Except for the beginning of the play, you must not press the arrow panel which is not correspond to the edit data. The foot must stay upon the panel where it presses last time, and will not be moved until it’s going to press the next arrow panel. The left foot must not step on the panel which locates at right to the panel on which the right foot rests. Conversely, the right foot must not step on the panel which locates at left to the panel on which the left foot rests. As for the third condition, the following figure shows the examples of the valid and invalid footsteps. Figure 2: the examples of valid and invalid footsteps The first one (left foot for LEFT, right foot for DOWN) and the second one (left foot for LOWER LEFT, right foot for UPPER LEFT) are valid footstep. The last one (left foot for RIGHT, right foot for DOWN) is invalid footstep. Note that, at the beginning of the play, the left foot and right foot can be placed anywhere at the arrow panel. Also, you can start first step with left foot or right foot, whichever you want. To play this game in a beautiful way, the play style called “ Natural footstep style” is commonly known among talented players. “Natural footstep style” is the style in which players make steps by the left foot and the right foot in turn. However, when a sequence of arrows is difficult, players are sometimes forced to violate this style. Now, your friend has created an edit data (the original sequence of direction arrows to be pushed) for you. You are interested in how many times you have to violate “Natural footstep style” when you optimally played with this edit data. In other words, what is the minimum number of times you have to step on two consecutive arrows using the same foot? Input The input consists of several detasets. Each dataset is specified by one line containing a sequence of direction arrows. Directions are described by numbers from 1 to 9, except for 5. The figure below shows the correspondence between numbers and directions. You may assume that the length of the sequence is between 1 and 100000, inclusive. Also, arrows of the same direction won’t appear consecutively in the line ...(truncated)
[ { "submission_id": "aoj_2243_11059884", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b...
aoj_2244_cpp
Problem C: Dungeon Quest II The cave, called "Mass of Darkness", had been a agitating point of the evil, but the devil king and all of his soldiers were destroyed by the hero and the peace is there now. One day, however, the hero was worrying about the rebirth of the devil king, so he decided to ask security agency to patrol inside the cave. The information of the cave is as follows: The cave is represented as a two-dimensional field which consists of rectangular grid of cells. The cave has R × C cells where R is the number of rows and C is the number of columns. Some of the cells in the cave contains a trap, and those who enter the trapping cell will lose his hit points. The type of traps varies widely: some of them reduce hit points seriously, and others give less damage. The following is how the security agent patrols: The agent will start his patrol from upper left corner of the cave. - There are no traps at the upper left corner of the cave. The agent will patrol by tracing the steps which are specified by the hero. - The steps will be provided such that the agent never go outside of the cave during his patrol. The agent will bring potions to regain his hit point during his patrol. The agent can use potions just before entering the cell where he is going to step in. The type of potions also varies widely: some of them recover hit points so much, and others are less effective. - Note that agent’s hit point can be recovered up to HP max which means his maximum hit point and is specified by the input data. The agent can use more than one type of potion at once. If the agent's hit point becomes less than or equal to 0, he will die. Your task is to write a program to check whether the agent can finish his patrol without dying. Input The input is a sequence of datasets. Each dataset is given in the following format: HP init HP max R C a 1,1 a 1,2 ... a 1, C a 2,1 a 2,2 ... a 2, C . . . a R ,1 a R ,2 ... a R , C T [ A-Z ] d 1 [ A-Z ] d 2 . . . [ A-Z ] d T S [ UDLR ] n 1 [ UDLR ] n 2 . . . [ UDLR ] n S P p 1 p 2 . . . p P The first line of a dataset contains two integers HP init and HP max (0 < HP init ≤ HP max ≤ 1000), meaning the agent's initial hit point and the agent’s maximum hit point respectively. The next line consists of R and C (1 ≤ R , C ≤ 100). Then, R lines which made of C characters representing the information of the cave follow. The character a i,j means there are the trap of type a i,j in i -th row and j -th column, and the type of trap is denoted as an uppercase alphabetic character [ A-Z ]. The next line contains an integer T , which means how many type of traps to be described. The following T lines contains a uppercase character [ A-Z ] and an integer d i (0 ≤ d i ≤ 1000), representing the type of trap and the amount of damage it gives. The next line contains an integer S (0 ≤ S ≤ 1000) representing the number of sequences which the hero specified as the agent's patrol route. The ...(truncated)
[ { "submission_id": "aoj_2244_10849113", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <algorithm>\n#include <queue>\nusing namespace std;\n#define MAXN 310\n#define MAXM 5010\n#define FI first\n#define SE second\n#define MP(x,y) make_pair(x,y)\nconst int INF =100000000...
aoj_2250_cpp
Problem I: Operator The customer telephone support center of the computer sales company called JAG is now in- credibly confused. There are too many customers who request the support, and they call the support center all the time. So, the company wants to figure out how many operators needed to handle this situation. For simplicity, let us focus on the following simple simulation. Let N be a number of customers. The i -th customer has id i , and is described by three numbers, M i , L i and K i . M i is the time required for phone support, L i is the maximum stand by time until an operator answers the call, and K i is the interval time from hanging up to calling back. Let us put these in other words: It takes M i unit times for an operator to support i -th customer. If the i -th customer is not answered by operators for L i unit times, he hangs up the call. K i unit times after hanging up, he calls back. One operator can support only one customer simultaneously. When an operator finish a call, he can immediately answer another call. If there are more than one customer waiting, an operator will choose the customer with the smallest id. At the beginning of the simulation, all customers call the support center at the same time. The simulation succeeds if operators can finish answering all customers within T unit times. Your mission is to calculate the minimum number of operators needed to end this simulation successfully. Input The input contains multiple datasets. Each dataset has the following format: N T M 1 L 1 K 1 . . . M N L N K N The first line of a dataset contains two positive integers, N and T (1 ≤ N ≤ 1000, 1 ≤ T ≤ 1000). N indicates the number of customers in the dataset, and T indicates the time limit of the simulation. The following N lines describe the information of customers. The i -th line contains three integers, M i , L i and K i (1 ≤ M i ≤ T , 1 ≤ L i ≤ 1000, 1 ≤ K i ≤ 1000), describing i -th customer's information. M i indicates the time required for phone support, Li indicates the maximum stand by time until an operator answers the call, and Ki indicates the is the interval time from hanging up to calling back. The end of input is indicated by a line containing two zeros. This line is not part of any dataset and hence should not be processed. Output For each dataset, print the minimum number of operators needed to end the simulation successfully in a line. Sample Input 3 300 100 50 150 100 50 150 100 50 150 3 300 100 50 150 100 50 150 200 50 150 9 18 3 1 1 3 1 1 3 1 1 4 100 1 5 100 1 5 100 1 10 5 3 10 5 3 1 7 1000 10 18 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10 9 10 11 10 11 12 0 0 Output for the Sample Input 2 3 3 4
[ { "submission_id": "aoj_2250_10867556", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <iostream>\n#include <queue>\n#define FI first\n#define SE second\nusing namespace std;\nconst double EPS = 1e-8;\nconst int MAXN = 1005;\nconst int INF = 11111111...
aoj_2251_cpp
Problem J: Merry Christmas International Christmas Present Company (ICPC) is a company to employ Santa and deliver presents on Christmas. Many parents request ICPC to deliver presents to their children at specified time of December 24. Although same Santa can deliver two or more presents, because it takes time to move between houses, two or more Santa might be needed to finish all the requests on time. Employing Santa needs much money, so the president of ICPC employed you, a great program- mer, to optimize delivery schedule. Your task is to write a program to calculate the minimum number of Santa necessary to finish the given requests on time. Because each Santa has been well trained and can conceal himself in the town, you can put the initial position of each Santa anywhere. Input The input consists of several datasets. Each dataset is formatted as follows. N M L u 1 v 1 d 1 u 2 v 2 d 2 . . . u M v M d M p 1 t 1 p 2 t 2 . . . p L t L The first line of a dataset contains three integer, N , M and L (1 ≤ N ≤ 100, 0 ≤ M ≤ 1000, 1 ≤ L ≤ 1000) each indicates the number of houses, roads and requests respectively. The following M lines describe the road network. The i -th line contains three integers, u i , v i , and d i (0 ≤ u i < v i ≤ N - 1, 1 ≤ d i ≤ 100) which means that there is a road connecting houses u i and v i with d i length. Each road is bidirectional. There is at most one road between same pair of houses. Whole network might be disconnected. The next L lines describe the requests. The i -th line contains two integers, p i and t i (0 ≤ p i ≤ N - 1, 0 ≤ t i ≤ 10 8 ) which means that there is a delivery request to house p i on time t i . There is at most one request for same place and time. You can assume that time taken other than movement can be neglectable, and every Santa has the same speed, one unit distance per unit time. The end of the input is indicated by a line containing three zeros separated by a space, and you should not process this as a test case. Output Print the minimum number of Santa necessary to finish all the requests on time. Sample Input 3 2 3 0 1 10 1 2 10 0 0 1 10 2 0 3 2 4 0 1 10 1 2 10 0 0 1 10 2 20 0 40 10 10 10 0 1 39 2 3 48 3 5 20 4 8 43 3 9 10 8 9 40 3 4 5 5 7 20 1 7 93 1 3 20 0 0 1 100000000 2 100 3 543 4 500 5 400 6 300 7 200 8 100 9 100 0 0 0 Output for the Sample Input 2 1 4
[ { "submission_id": "aoj_2251_10848566", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 200000000;\n#define N 2100\nint lef[1005], pn;\nint tim, T[1005];\nvector<int> G[1005];\nbool mat...
aoj_2253_cpp
ブレイブ・フォース・ストーリー English text is not available in this practice contest. 「最果ての地,ナイン・アイランドには特別な力を持つものが生きていた. ある者は火,ある者は氷,ある者は風,ある者は土を意のままに操ることができた. 人はこれらの術者をこう呼んだ.『Brave Force』と・・・.時は戦国時代.権力者たちはBrave Forceを私欲のために使わんとBrave Force狩りを始めた. Brave Forceを巡る戦いが今始まろうとしている.」 以上は,あなたが今つくろうとしている戦略シミュレーションゲームのプロローグである.このプロローグは本問題には全く関係ない. 問題の説明に戻ろう.戦略シミュレーションの世界においては, 正6角形のマスがよく使われる. これは, 正方形のマスに比べて方向による距離の違いが小さく, 敷き詰めも可能であるからである. 今回はこのようなマップ上でコマを動かすことを考える.コマは各ターンごとに隣接するマスに移動させることができる. もちろんマップ上には多くの障害物があってそこには移動することができない.さて一定のターン数が経過するまでにたどり着くことが可能なマスの数はいくつあるだろうか. Input 入力はそれぞれがマップの情報を表す1つ以上のデータセットからなる. データセットの最初の行には, 2つの整数が含まれていて, 1番目がターン数 t で, 2番目が障害物の数 n である. それに続く n 行にはそれぞれ障害物のマスの座標を表す2つの整数が含まれていて, 1番目が x 座標で, 2番目が y 座標である. 障害物の座標は互いに異なっている. そして最後の行にはスタート位置のマスの座標を表す2つの整数が含まれていて, 1番目が x 座標で, 2番目が y 座標である. このマスには障害物はない. またこのマスは到達できるマスに含められる. マスに割り当てられた座標は下の図のようになっている. 図B-1 マスに割り当てられた座標 入力の終わりは"0 0"を含む行で表される. いずれの座標も絶対値が30以下である. ターン数は1以上30以下である. 障害物の数は0以上300以下である. 図B-2 Sample Input の1つ目のデータセット Output 各マップについて到達できるマスの数を表す整数を各行に出力せよ. それ以外の余計な文字を出力に含めてはいけない. Sample Input 1 1 1 0 0 0 2 2 -2 1 2 0 2 2 2 0 -1 1 4 4 -2 1 1 -2 1 2 3 -3 -2 0 4 6 0 1 1 1 1 0 -1 0 -1 -1 0 -1 0 0 0 0 Output for the Sample Input 6 18 19 58 1
[ { "submission_id": "aoj_2253_10681617", "code_snippet": "#include <bits/stdc++.h>\n#include <cstdint>\n#include <regex>\nusing namespace std;\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vc = vector<char>;\nusing vb = vector<bool>;\nusin...
aoj_2246_cpp
Problem E: Alice and Bomb Alice and Bob were in love with each other, but they hate each other now. One day, Alice found a bag. It looks like a bag that Bob had used when they go on a date. Suddenly Alice heard tick-tack sound. Alice intuitively thought that Bob is to kill her with a bomb. Fortunately, the bomb has not exploded yet, and there may be a little time remained to hide behind the buildings. The appearance of the ACM city can be viewed as an infinite plane and each building forms a polygon. Alice is considered as a point and the bomb blast may reach Alice if the line segment which connects Alice and the bomb does not intersect an interior position of any building. Assume that the speed of bomb blast is infinite; when the bomb explodes, its blast wave will reach anywhere immediately, unless the bomb blast is interrupted by some buildings. The figure below shows the example of the bomb explosion. Left figure shows the bomb and the buildings(the polygons filled with black). Right figure shows the area(colored with gray) which is under the effect of blast wave after the bomb explosion. Figure 6: The example of the bomb explosion Note that even if the line segment connecting Alice and the bomb touches a border of a building, bomb blast still can blow off Alice. Since Alice wants to escape early, she wants to minimize the length she runs in order to make herself hidden by the building. Your task is to write a program which reads the positions of Alice, the bomb and the buildings and calculate the minimum distance required for Alice to run and hide behind the building. Input The input contains multiple test cases. Each test case has the following format: N bx by m 1 x 1,1 y 1,1 ... x 1, m 1 y 1, m 1 . . . m N x N ,1 y N ,1 ... x N , m N y N , m N The first line of each test case contains an integer N (1 ≤ N ≤ 100), which denotes the number of buildings. In the second line, there are two integers bx and by (-10000 ≤ bx , by ≤ 10000), which means the location of the bomb. Then, N lines follows, indicating the information of the buildings. The information of building is given as a polygon which consists of points. For each line, it has an integer m i (3 ≤ m i ≤ 100, ∑ N i =1 m i ≤ 500) meaning the number of points the polygon has, and then m i pairs of integers x i,j , y i,j follow providing the x and y coordinate of the point. You can assume that the polygon does not have self-intersections. any two polygons do not have a common point. the set of points is given in the counter-clockwise order. the initial positions of Alice and bomb will not by located inside the polygon. there are no bomb on the any extended lines of the given polygon’s segment. Alice is initially located at (0, 0). It is possible that Alice is located at the boundary of a polygon. N = 0 denotes the end of the input. You may not process this as a test case. Output For each test case, output one line which consists of the minimum distance required for Alice to run and hide behin ...(truncated)
[ { "submission_id": "aoj_2246_10829154", "code_snippet": "#include<bits/stdc++.h>\n//#define Alex std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);\n//#define int long long\n//#define double long double\n//#define L(i,a,b) for(int i = a;i <= b;i++)\n//#define R(i,a,b) for(int i = a;i >= b;i-...
aoj_2254_cpp
最短ルート English text is not available in this practice contest. あなたの所属する知的プログラミングサークル (通称 Intelligent Clever Programming Circle, ICPC)では,今大掃除の最中である. 部室には,歴代の先輩たちの残していったさまざまなアイテムが所狭しと放置されている. あなたが棚を整理していると,奥に大量のレトロゲームが封印されているのを発見した. その中のいくつかに見覚えがあったあなたは,久々にそのゲームを遊んでみることにした. 見つけたゲームの詳細は次の通りである. このゲームには1番から N 番までの N 個のステージがあり,任意の順に攻略することができる. また,1から N まで番号付けられた装備があり,それらの装備を使用することでステージの攻略時間を短縮することができる. ゲームを始めた段階では装備は一つも持っていないが, i 番のステージを攻略すると i 番の装備を入手することができ,一度入手した後は何度でも使用できる. 装備は一ステージで一種類だけしか使用することができないが,異なるステージで同じ装備を使用することはできる. あなたは昔このゲームをクリアしたことがあるので,各装備に対して,その装備を使用したときに各ステージを攻略するのにかかる時間をすべて把握している. ただ普通にクリアするだけではつまらないので,あなたは全てのステージを攻略し終わるまでにかかる時間を最小にしようと考えた. そのため,あなたはICPCでの経験を生かして,各情報を与えたときに全てのステージを攻略し終わるまでにかかる時間の最小値を計算するプログラムを書くことにした. Input 入力は複数のデータセットからなる. 入力の終わりは1つのゼロからなる行によって与えられる. 各データセットは一つのゲームに関する情報を表し,その形式は以下の通りである. N t 10 t 11 ... t 1N t 20 t 21 ... t 2N ... t N0 t N1 ... t NN データセットの最初の行は1つの整数 N からなり,ステージの数を表す. 続く N 行は N+1 個の整数からなり,ステージの攻略時間を表す. t i0 は i 番のステージを装備なしで攻略するのにかかる時間である. t i j ( j > 0)は i 番のステージを j 番の装備で攻略するのにかかる時間である. それぞれの値は次の制約を満たしている. 1 ≤ N ≤ 16 1 ≤ t i j ≤ 100,000 Output それぞれのデータセットに対して,全てのステージを攻略し終わるまでにかかる時間の最小値を表す整数を1行に出力せよ.出力行にはこの数値以外の文字を含んではならない. Sample Input 3 100 100 100 100 100 1 100 100 100 1 1 100 3 100 100 1 100 200 102 100 102 100 100 1 100 7 100 100 60 60 70 70 70 90 100 50 100 55 45 45 55 44 100 50 60 100 51 50 55 30 100 70 10 20 1 10 10 40 200 90 10 30 10 10 10 30 150 200 12 1 11 11 1 30 10000 1200 1100 1100 1200 1200 1090 1 0 Output for the Sample Input 102 202 1301
[ { "submission_id": "aoj_2254_10853366", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N;\n while(cin >> N, N) {\n vector<vector<int>> t(N, vector<int>(N + 1));\n for(int i = 0; i < N; ++i) {\n for(int j = 0; j < N + 1; ++j) {\n ...
aoj_2255_cpp
6÷2(1+2) English text is not available in this practice contest. 数学科の学生であるきたまさ君は,数学はとても得意なのだが算数はあまり得意ではない. 特に演算子の優先順位が苦手で, カッコの中を先に計算するというのはわかるのだが, "+" と "*" のどちらを優先したら良いかとか複数の演算子が並んでいるときに左から計算したら良いのか右から計算したら良いのか覚えておらず, その時の気分で好きな順番で計算をしている.例えば,"1*1-1+1"という数式を前から"((1*1)-1)+1"という順番で計算する場合もあれば,真ん中から"(1*(1-1))+1"という順番で計算する場合さえある. また,分数や小数も苦手で,割り算では常に絶対値の小さい方に丸め,小数部を切り捨てて整数にしてしまう. 割る数がゼロであった場合には,きたまさ君はどこかで間違えたと思ってもう一度最初から計算しなおすことにしている. きたまさ君はどんな順番で計算しても最終的な計算結果は同じになると主張しているので,あなたにはそれが間違いであると示すために,与えられた数式に対して きたまさ君の計算結果が何通りあり得るかを求めるプログラムを書いて欲しい. ただし,演算の順番が異なっていても,最終的な答えが同じならば一つと数えるものとし, また,与えられる数式は,少なくとも一つはゼロ割が発生しない演算順序が存在し,いかなる演算順序で計算した場合でも,計算結果と途中で現れる数の絶対値は常に 10 9 以下であることが保証されている. Input 入力はひとつ以上の行からなり,入力の各行は数式ひとつを含む.数式の文法は以下の BNF で与えられる. <expr> ::= <num> | "(" <expr> ")" | <expr> "+" <expr> | <expr> "-" <expr> | <expr> "*" <expr> | <expr> "/" <expr> <num> ::= <digit> | <num> <digit> <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" すべての数式はこの構文規則に従う.また,入力行の長さは 200 文字を超えない.ひとつの数式が含む演算子 ( "+" , "-" , "*" , "/" ) の数は 10 個を超えない. 入力の終わりは "#" ひとつだけからなる行によって示される. Output それぞれの数式について,きたまさ君の計算結果が何通りあり得るかを出力せよ.出力は数式ひとつごとに 1 行とする. Sample Input 6/2*(1+2) 1-1-1 (1-1-1)/2 # Output for the Sample Input 2 2 1
[ { "submission_id": "aoj_2255_10685115", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<i...
aoj_2249_cpp
Problem H: Road Construction King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected. In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions: For every pair of cities, there is a route (a set of roads) connecting them. The minimum distance between the capital and each city does not change from his original plan. Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost. Input The input consists of several datasets. Each dataset is formatted as follows. N M u 1 v 1 d 1 c 1 . . . u M v M d M c M The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and M indicate the number of cities and the number of roads in the original plan, respectively. The following M lines describe the road information in the original plan. The i -th line contains four integers, u i , v i , d i and c i (1 ≤ u i , v i ≤ N , u i ≠ v i , 1 ≤ d i ≤ 1000, 1 ≤ c i ≤ 1000). u i , v i , d i and c i indicate that there is a road which connects u i -th city and v i -th city, whose length is d i and whose cost needed for construction is c i . Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom. The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset. Output For each dataset, print the minimum cost of a plan which satisfies the conditions in a line. Sample Input 3 3 1 2 1 2 2 3 2 1 3 1 3 2 5 5 1 2 2 2 2 3 1 1 1 4 1 1 4 5 1 1 5 3 1 1 5 10 1 2 32 10 1 3 43 43 1 4 12 52 1 5 84 23 2 3 58 42 2 4 86 99 2 5 57 83 3 4 11 32 3 5 75 21 4 5 23 43 5 10 1 2 1 53 1 3 1 65 1 4 1 24 1 5 1 76 2 3 1 19 2 4 1 46 2 5 1 25 3 4 1 13 3 5 1 65 4 5 1 34 0 0 Output for the Sample Input 3 5 137 218
[ { "submission_id": "aoj_2249_11056492", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n#include <queue>\nusing namespace std;\nconst int INF=0x3f3f3f3f;\nconst int maxv=10002;\nstruct edge{int en,dis,cost;};\ntypedef pair<int,int> P;\nint main(){\n int sc...
aoj_2256_cpp
ケーキ分割問題 English text is not available in this practice contest. イコとピコはACM博士によって開発された人工知能を搭載した双子のロボットである.今日は二人の誕生日なので,博士は二人のためにたくさんのイチゴがのったケーキを用意した. ケーキは横幅 W ×奥行き H の長方形の形をしている. 便宜上,ケーキは上から見たとき(0,0),( W ,0),( W , H ),(0, H )を頂点とする長方形になるように置かれているとする. また,二人に均等になるようにちょうど2 N 個のイチゴが乗っている. せっかくなので,博士は二人にケーキを切らせることにした. イコとピコは協力してケーキを切るため,イコが包丁の一端を辺(0,0)-(0, H )の間におき,ピコはもう一端を辺( W ,0)-( W , H )の間において同時に下に下ろすという方法をとることにした. もちろん包丁は直線状なので,選んだ2点を通る直線でケーキは2つに分かれる.(図E-1) 図E-1 博士は人工知能を完成させたことに満足し他の部分を適当に作ったために,二人の腕のパーツは不安定になってしまった. そのため,辺(0,0)-(0, H )(または辺( W ,0)-( W , H ))上の狙った場所に置こうとしてもどうしてもずれてしまうのである. 二人はイチゴが好きなので,できれば N 個ずつに分けたいと考えている. そこで, N 個ずつになるように切れない場合は2点を選びなおすことにした. 2 N 個のイチゴの配置によっては半分ずつに分けられなかったり,分けられたとしても,とても確率が低い場合も考えられる. このような場合に何度も2点を選びなおすのは不毛なので,まず初めに N 個ずつに分かれるように切れる確率を計算することにした. 二人の腕のパーツは不安定なので,動きを予想するのは難しい. とりあえず,二人は「包丁を置く点は辺(0,0)-(0, H )(または辺( W ,0)-( W , H ))上に必ず乗り,置かれる確率はどの二点も等しい」という仮定を置くことにした. また,イチゴの大きさを考慮するのも面倒なので,点とみなすことにした. これは人工知能の性能を試すいい機会である. 博士と同じ研究所に勤めているあなたに,2 N 個のイチゴの位置を与えられたときに, 2人と同じ仮定を用いてイチゴが N 個ずつに分かれる確率を計算するプログラムを作成する仕事が割り当てられた. Input 入力は複数のデータセットからなる.入力の終わりは空白で区切られた3つのゼロからなる行によって与えられる.各データセットは1つのケーキに関する情報を表し,その形式は以下の通りである. W H N x 1 y 1 ... x 2N y 2N データセットの最初の行は3つの整数 W , H , N からなり,それぞれケーキの横幅,奥行き,イチゴの個数/2を表す. 続く2 N 行は2個の整数からなり,それぞれイチゴのx座標とy座標を表す. それぞれの値は次の制約を満たしている. 1 ≤ W , H ≤ 1,000 1 ≤ N ≤ 100 0 ≤ x i ≤ W 0 ≤ y i ≤ H また,2つの異なるイチゴが同じ座標にあることはない. Output それぞれのデータセットに対して, 2人の仮定を用いた場合にイチゴが2等分される確率を表す実数を1行に出力せよ. 答えには 10 -8 を越える誤差があってはいけない.それ以外の余計な文字を出力してはならない. Sample Input 2 2 1 0 1 2 1 3 3 1 1 1 2 1 0 0 0 Output for the Sample Input 0.5000000000 0.1666666667
[ { "submission_id": "aoj_2256_10848200", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define endl '\\n'\n#define ls rt << 1\n#define rs rt << 1 | 1\n#define maxn 200010\nusing namespace std;\n\nconst double eps = 1e-12;\nconst double pi = acos(-1.0);\nint sgn(double x)\n{\n if (fabs(x...
aoj_2260_cpp
問題 B : (iwi) 20XX 年の今,G○○gle 社の世界的な支配により,人々のコミュニケーションは限られており, 例えば,自由に人と情報をやりとりすることは基本的に許されていない. 特に,過去にプログラミングコンテストで名を馳せた強豪たちは,危険と判断され, お互いバラバラにされ,密かに注意深く監視されているという. 自分も,そんな一人であった,ような気がする. しかし実は,自分は記憶を失ってしまっているのだ. 自分が何と名乗っていたか,それすらも思い出せない. 'i' とか 'w' とか括弧とかを使っていた気がするが, それっぽいものを書きだしてみても,しっくりこない. そういえば,今思い出したことには,左右に線対称だった気がするのだ. 問題 'i', 'w', '(', ')' からなる文字列が与えられた時, 一部の文字を他の文字に置き換えて,'i', 'w', '(', ')' からなる左右に線対称な文字列にしたい. 文字の追加や削除は許さず,1 文字を別の 1 文字に変える操作のみを行うことにする. 少なくとも何文字置き換えなければならないかを出力するプログラムを作成せよ. ここで用いる左右に線対称の定義は,以下とする. 以下の文字列は左右に線対称. 空文字列 "i" "w" 文字列 x が左右に線対称のとき,以下の文字列も左右に線対称. "i" x "i" "w" x "w" "(" x ")" ")" x "(" 以上のもののみが左右に線対称. ここで,"i" x "i" とは "i" と x と "i" を連結したものを表す. 他のものも同様である. 入力 入力は 'i', 'w', '(', ')' からなる 1 つの文字列である. 出力 置き換えなければならない文字数を表す整数を出力せよ. 制約 入力の文字列の長さは 10 以下である. 入出力例 入出力例 1 入力例 1: (iwi) 入力例 1 に対する出力例: 0 入力例 1 では文字列がはじめから左右に線対称である. 入出力例 2 入力例 2: ii(((((ww 入力例 2 に対する出力例: 5 入力例 2 では例えば ww((i))ww 等に変更すればよい.
[ { "submission_id": "aoj_2260_9264521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define reps(i, l, r) for(ll i = (l); i < (r); i++)\n#define endl \"\\n\";\n#define all(a) (a).begin(), (a).e...
aoj_2258_cpp
全自動円形掃除機 English text is not available in this practice contest. Automatic Cleaning Machine (ACM) 社は画期的な全自動円形掃除機 Intelligent Circular Perfect Cleaner(ICPC) を開発した. なんとこの ICPC は人がいない日中に自動で動き出し,自分が通り過ぎた場所のゴミを掃除する機能を備えている. 全自動という特性を活かすためにも ICPC の集塵機の容積を大きくし,人手でゴミを捨てる回数を減らしたい. しかしながら ICPC が大型化すればするほど ICPC が円形故に部屋の隅の掃除できない部分の面積も大きくなってしまう.(図G-1) 図G-1 また廊下の幅よりも大きくしてしまうと廊下を通ることができなくなってしまう.(図G-2) 図G-2 ACM 社は凄腕プログラマーのあなたに,部屋の見取り図とICPCの中心座標と半径からその部屋の中でICPCが掃除可能な面積を出力するプログラムの作成を依頼した. Input 入力は複数のデータセットからなり,各データセットは以下の形式をしている. n x y r x 1 y 1 x 2 y 2 ... x n y n データセットの1行目には4つの整数 n , x , y , r が記されており,それぞれ部屋を構成する多角形の頂点数(3 ≤ n ≤ 20), ICPCの初期位置の中心座標 (-100 ≤ x , y ≤ 100), ICPCの半径 (1 ≤ r ≤ 100)を表している. 続く n 行には多角形の頂点の座標が反時計回りに記されており,以下の条件を満たす. -100 ≤ x i , y i ≤ 100 全ての壁は,x軸かy軸に並行 ICPCの初期位置は必ず部屋の内側にあり,どの部屋の壁とも10 -3 以上離れている 任意の部屋の頂点同士の距離,任意の部屋の頂点と部屋の辺の距離,任意の部屋の辺同士の距離は 2 r +10 -3 より大きいかもしくは 2 r -10 -3 より小さい 四つのゼロのみからなる行が入力の終わりを表す. Output 各データセットに対し,ICPCが掃除可能な領域の面積を1行に出力せよ.答えには10 -6 を超える誤差があってはならない. Sample Input 4 5 5 1 0 0 10 0 10 10 0 10 8 5 5 1 0 0 9 0 9 1 10 1 10 9 9 9 9 10 0 10 12 25 5 1 0 0 10 0 10 4 20 4 20 0 40 0 40 10 20 10 20 5 10 5 10 10 0 10 0 0 0 0 Output for the Sample Input 99.1415926535897 96.7123889804 199.2321787
[ { "submission_id": "aoj_2258_2589625", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst int nrep = 50;\n\n/********************* Geometry Library *****************/\nconst double EPS = 1e-9;...
aoj_2257_cpp
桜詩 願はくは花の下にて春死なむ English text is not available in this practice contest. Nathan O. Davis は集積回路コースの学生である. Nathan は足りない単位を補充するために,日本文化に関する授業を履修している.今日の課題は詩の作成である. Nathan は勉強不足で日本語が不得手なため,プログラムに詩を自動生成させようと考えた.手始めに彼は,日本語の単語の接続辞書を入手した. あとは単語の接続にしたがってランダムな文章を生成するだけである. しかしながら,ランダムに生成された全ての文字列が詩として認められるわけではない. 詩には,いくつかの季語のうち1つが,一度だけ現れていなければならない.ある季語が2回以上出現することや,2種類以上の季語が1回ずつ出現することは許されない. また,季語は単語の接続の境界をまたいで出現してもよい. あなたの仕事は,入力で与えられた単語の接続辞書と季語のリストから,指定された長さの詩が何通りに作られるかを求めるプログラムを作成することである. 違う単語を繋げて詩となる同じ文字列が得られた場合,それらは重複して数え上げるものとする.答えは非常に大きくなりうるので, 1,000,000,007 で割った余りを出力せよ. Input 入力は複数のテストケースを含んでいる.1つのテストケースは,以下の形式で与えられる. N M K from 1 to 1 from 2 to 2 : from N to N seasonword 1 seasonword 2 : seasonword K 入力の最初の行には3つの整数 N (1 ≤ N ≤ 250), M (1 ≤ M ≤ 500), K (1 ≤ K ≤ 30) が含まれ,それぞれ単語の接続辞書の大きさ,作成すべき詩の長さ,季語の数を表す. 続く N 行は単語の接続辞書の情報を表す. 各行は2つの文字列 from i , to i を含み,単語 from i の後に続けて単語 to i が出現してもよいということを表す. to i の後に続けて from i が出現してもよいということを表すものではないことに注意せよ.また, from i で終わるような他の文字列の後に続けて to i が出現してもよいということを表すものでもない.詩は,接続辞書に含まれるどの単語から始めてもよい. 続く K 行は1つの文字列 seasonword i からなり,それぞれ季語を表す. 入力中に現れる文字列は全て小文字のアルファベットからなり,その長さは 1 以上 20 以下である. 接続辞書の各項目,および季語は互いに異なる. すなわち, i ≠ j に対して from i ≠ from j または to i ≠ to j が成り立つ. 同様に, i ≠ j に対して seasonword i ≠ seasonword j が成り立つ. 入力の末尾には,入力の終了を表す 3 つの 0 がある. Output 生成される異なる詩の数を 1,000,000,007 で割った余りを1行に出力せよ. 先に言及したように,違う単語を繋げて詩となる同じ文字列が得られた場合,それらは重複して数え上げるものとする. 厳密に言えば,2つの詩 s , t があり,それぞれ単語の列 [ a 1 , a 2 , ..., a n ], [ b 1 , b 2 , ..., b m ] を順に連結して得られたものであるとき, n = m かつすべての 1 ≤ i ≤ n に対して a i = b i であるとき,またそのときに限って,2つの詩 s , t は同一の詩とみなされる. Sample Input 4 64 2 negawakuha hananoshitanite hananoshitanite harushinan harushinan sonokisaragino sonokisaragino mochizukinokoro sakura hana 2 15 2 naha naha naha gachoon sakura hana 3 7 2 asakur a a sakura asa kura sakura hana 9 100 2 a a a h a n h a h h h n n a n h n n sakura hana 4 2 2 a a a b b a b b ab b 4 7 4 i cpc mi cp ac mi cp c ac wa tle re 0 0 0 Output for the Sample Input 1 1 3 715991824 1 1
[ { "submission_id": "aoj_2257_10867672", "code_snippet": "#include <bits/stdc++.h>\n\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\ntemplate <typename T> T &chmin(T &a, const T &b) { return a = m...
aoj_2267_cpp
問題 I : ビット演算 いよいよ G○○gle Code Jam の世界大会が始まろうとしている. 前では P○tr, t○mek, SnapDrag○n をはじめとする, G○○gle の誇る最強のコーダー達が睨みを利かせている. 妙な動きを見せれば,命はないだろう. 目をつむり,コンテスト開始をじっと待つ. …妙だ,コンテストが始まらない. 目を開けてみると…なんということだ.部屋の全ての人間が,倒れている. いや違う,全てではない.一人の男,wata を除いてだ. wata 「やっと話せるぞ. 久しぶりだな,(iwi)」 (iwi) 「…お前は何者だ? 俺の知る wata は幼なじみ系美少女のはずが…」 wata 「まだそんな妄想に取り憑かれているのか!! 思い出せ!! 受け止めろ!! 彼はもうこの世界には居ないんだ!!」 はっ…!! 僕は全てを思い出していた. 僕は世界で最初にプログラミングコンテストで人を殺めたのだ. そして,僕が殺したのは他でもなく,最も仲の良かった友人の一人,kita_masa であった. kita_masa は自らをビット演算の達人と言い, あらゆる関数はビット演算で記述できると言う,少し変わった奴だった. そういえば今,ビット演算を用いて作成したい関数があるが, kita_masa にそれをお願いすることはできない. そこで,kita_masa の代わりになるプログラムを作成することにした. 問題 N 個の整数の組 ( x i , y i ) が与えられる. 全ての組に対して y i = f ( x i ) なる関数 f を制約の下で作ることを考える. 関数 f は,C 言語のソースコードとして以下のように表現できるものとする: uint32_t f(uint32_t x) { return 式; } ここで,uint32_t は 32 ビット符号無し整数を表す. また,式は以下の BNF で表現できるものとする: <expr> ::= "x" | <num> | "(~" <expr> ")" | "(" <expr> <op2> <expr> ")" <op2> ::= "&" | "|" | "^" | "+" | "-" | "*" <num> は 32 ビット符号無し整数で表現できる非負の整数を 10 進数で自然に (leading-zero 等を持たず) 表現したものとする. このような関数の式を出力するプログラムを作成せよ. 入力 入力の最初の行は 1 つの整数 N を含む. 続く N 行の i 行目は 2 つの整数 x i , y i を含む. 出力 条件を満たす式を出力せよ. 制約 1 ≤ N ≤ 8 0 ≤ x i , y i < 256 式は必ず,上記の BNF に従う書式で出力せよ.例え C 言語の表現として妥当であったとしても,空白や改行の挿入や括弧の削除などはしてはならない. 出力は 100000 文字を越えてはならない. 条件を満たす関数 f が常に存在するような入力が与えられる. 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. '+', '-', '*' を用いないような正しい出力が存在する 入出力例 入力例1: 8 1 1 2 2 3 3 4 0 5 1 6 2 7 3 8 0 入力例 1 に対する出力の例: (x&3) 入力例2: 8 1 0 2 0 3 2 4 0 5 4 6 4 7 6 8 0 入力例 2 に対する出力の例: (x&(x-1)) 良く知られる最右の 1 ビットをオフにする操作である.
[ { "submission_id": "aoj_2267_8975919", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\ntypedef unsigned uint;\nstring num(uint n){\n stringstream ss;\n ss << n;\n return ss.str();\n}\nstring negative(string expr){\n return \"(~...
aoj_2264_cpp
問題 F : 全域木 今,G○○gle Code Jam の地区大会が始まろうとしている. 左の席に座っている男の ID は wata と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがある. しかし,僕の仲間は一人残さず美少女だったはずだ. 僕の記憶の中の wata は,マトロイドが好きだった. 特に,マトロイド交差が大好きで, 様々なマトロイド達を交差させることに一種の興奮すら覚えると言う少し変わった奴だった. マトロイドの理論の力を使えば, 与えられたグラフ上で辺を共有しない複数の全域木を求めることはとても簡単な問題だと言っていた気がする. しかし,特別なグラフに関しては, マトロイドのアルゴリズムを直接に適用するよりも高速なアルゴリズムがあるのではないか? 問題 N 個の頂点からなる完全グラフにおいて, 辺を共有しない全域木を K 個作成せよ. 完全グラフとは,全ての相異なる 2 頂点間に 1 本の辺を持つグラフである. 下図は,4 頂点の完全グラフの例である. 4 頂点の完全グラフ. 全域木とは,元のグラフの全ての頂点と一部の辺からなる木のことである. 木とは,連結かつ閉路を持たないグラフのことである. 下図は,4 頂点の完全グラフにおける,辺を共有しない 2 つの全域木である. 4 頂点の完全グラフの全域木の例. 4 頂点の完全グラフの全域木であり,前の例と辺を共有しないもの. 入力 入力は 1 行からなり, 2 つの整数 N , K が書かれている. 出力 条件を満たす K 個の全域木を作ることができない時,-1 とだけ出力せよ. 条件を満たす K 個の全域木を作ることができる時, K 個の全域木を改行で区切り出力せよ. 1 つの全域木は N - 1 行で表される. その i 行目には,その全域木の辺 i が結ぶ 2 つの頂点を表す 2 つの整数をスペースで区切り出力する. ここで,頂点は 1 から N までの整数で表すものとする. 制約 2 ≤ N ≤ 10000 1 ≤ K ≤ 100 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 8 入出力例 入出力例 1 入力例 1: 4 2 入力例 1 に対する出力の例: 1 2 1 4 2 3 1 3 2 4 3 4 この入出力例は問題文中の図と対応している. 入出力例 2 入力例 2: 4 3 入力例 2 に対する出力の例: -1
[ { "submission_id": "aoj_2264_8962765", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n,k;\n cin >> n >> k;\n if(k>n/2){\n cout << -1 << endl;\n return 0;\n }\n vector<vector<string>> ans(k);\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n int i...
aoj_2261_cpp
問題 C : [[iwi]] そうだ,僕のハンドルネームは (iwi) だ. 僕は確かに昔にプログラミングコンテストに参加していた. そして,多くの仲間と楽しい時間を過ごした. 確か,仲間たちは全員,美少女だったような気がする. プログラミングコンテストの世界は,僕のハーレムだったような気がする. G○○gle は,僕のハーレムを奪ったのだ,そうに違いない. 昔の仲間の手がかりをつかむためにも,やはりプログラミングコンテストに出なければならない. G○○gle Code Jam に参加登録することにしよう. 今度の ID には,丸括弧以外の括弧も検討に入れてみよう. 問題 'i', 'w', '(', ')', '{', '}', '[', ']' からなる文字列が与えられた時, その部分列をとって,線対称な文字列を作りたい. 最大で何文字の文字列を作ることができるかを計算するプログラムを作成せよ. 与えられる文字列は, "iwi" という文字列を一度含み,それ以外の部分には 'i' と 'w' を含まない. より形式的には,与えられる文字列は s "iwi" t ( s と "iwi" と t を連結したもの)という形で表すことができ, s と t は '(', ')', '{', '}', '[', ']' からなる文字列である. s や t が 0 文字である可能性もある. 作る文字列は,与えられる文字列の部分列をとって作る. 部分列とは,元の文字列からいくつかの文字を取り出し,それらを, 元の文字列に含まれる順番で繋げたものである. 取り出す文字たちは必ずしも元の文字列で連続していなくても良い. また,作る文字列も,与えられる文字列と同様に,"iwi" という文字列を一度含み, それ以外の部分には 'i' と 'w' は含まないようにしたい. ここで用いる左右に線対称の定義は,以下とする. 以下の文字列は左右に線対称. 空文字列 "i" "w" 文字列 x が左右に線対称のとき,以下の文字列も左右に線対称. "i" x "i" "w" x "w" "(" x ")" ")" x "(" "{" x "}" "}" x "{" "[" x "]" "]" x "[" 以上のもののみが左右に線対称. 入力 入力は 'i', 'w', '(', ')', '{', '}', '[', ']' からなり上記の条件を満たす文字列である. 出力 上記の条件を満たし作ることのできる文字列の長さの最大値を出力せよ. 制約 入力の文字列の長さは 15 以下である. 入出力例 入出力例 1 入力例 1: [[[iwi[[[ 入力例 1 に対する出力例: 3 "iwi" という文字列しか作ることができない. 入出力例 2 入力例 2: [{)iwi(]} 入力例 2 に対する出力例: 7 "[)iwi(]" や "{)iwi(}" など 7 文字の文字列を作ることができる.
[ { "submission_id": "aoj_2261_4088642", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nbool check(string s){\n int n = s.size();\n bool ok = true;\n for(int i=0;i<n/2;i++){\n if(s[i] == '('){\n ok &= s[n-1-i] == ')';\n }\n else if(s[i] == ')'){\n ...
aoj_2263_cpp
問題 E : ファーストアクセプタンス プログラミングコンテストでは,各問題に関して, その問題を最初に正解した人の名前や正解時間が, ファーストアクセプタンス(最初の正解)として解説等でしばしば言及される. 久しぶりにプログラミングコンテストに参加するとはいえ,予選で落ちるとは到底思えない. ならば,最初のうちは,多少高いスコアを取ることを目指すよりも, ファーストアクセプタンスを多く獲得し,存在をアピールしたほうが良いのではないか. 自分の実力を持ってすれば,各問題を見た瞬間に, その問題を自分が何分で解くことができるかと, その問題が開始後何分で自分以外の参加者によって最初に解かれるかがわかる. これらの情報を用いて, どの程度ファーストアクセプタンスを獲得できるかを計算するプログラムを作っておこう. 問題 N 問の問題から成るプログラミングコンテストを考える. 問題 i を自分が解くには A i 秒の時間がかかり, また,問題 i は開始後 B i 秒で自分以外の参加者によって最初に解かれるとする. 自分が問題を解く順番は自由に決められるが, 1 つの問題を解き始めたら,解き終えるまでその問題をやるものとする. 問題を解き終え次の問題に取りかかる際などの, 問題を解いている時間以外の時間は十分小さいと考え,無視して考えることにする. また,全ての問題は終了までに 1 人以上の自分以外の参加者によって解かれるものと考える. 自分以外の参加者によって最初に解かれるよりも早く, あるいは同時に問題 i を自分が解きおえた時, 問題 i のファーストアクセプタンスを獲得できる. すなわち,問題 i を自分が開始後 t i 秒に解き終えたとしたとき, t i ≤ B i であれば,問題 i のファーストアクセプタンスを獲得できる. 最大で何個の問題に関してファーストアクセプタンスが獲得できるかを計算するプログラムを作成せよ. 入力 入力の最初の行は 1 つの整数 N を含む. 続く N 行には,各問題に関する情報が与えられる. これらの行のうちの i 行目には 2 個の数字 A i , B i が書かれている. 出力 最大で獲得することのできるファーストアクセプタンスの数を出力せよ. 制約 1 ≤ N ≤ 1000 1 ≤ A i ≤ 10 6 ( 1 ≤ i ≤ N ) 1 ≤ B i ≤ 10 6 ( 1 ≤ i ≤ N ) 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 16 入出力例 入出力例 1 入力例 1: 3 3 5 5 9 10 20 入力例 1 に対する出力例: 3 入出力例 2 入力例 2: 3 3 2 5 15 10 12 入力例 2 に対する出力例: 2
[ { "submission_id": "aoj_2263_10222670", "code_snippet": "#include <bits/stdc++.h> \nusing namespace std;\nusing ll=long long;\nusing dll=long double;\nusing pq=priority_queue<int,vector<int>,greater<int>>;\nusing graph=vector<vector<int>>;\n\n#define int ll\n#define db dll\n#define sbt(x) (x).begin(),(x)...
aoj_2262_cpp
問題 D : 停止問題 G○○gle Code Jam は G○○gle 社が年に 1 度開催するコンテストである. 優勝者は G○○gle への入社を許される,世界最高峰のコンテストだ. しかし勿論,それ以外の参加者は帰らぬ者となる. G○○gle Code Jam では自分の好きなプログラミング言語や処理系を使うことができる. 僕は Defunge という自らが開発したプログラミング言語で参加することにした. この言語を使えば,計算困難な問題はおろか,判定不能な問題ですら解決できる気がしている. 問題 与えられるプログラムが停止するかを判定するプログラムを作成せよ. 与えられるプログラムは,以下で説明するプログラミング言語 Defunge で記述されている. Defunge のプログラムの命令は 1 文字であり,1 次元の列ではなく 2 次元の格子状に並んでいる. 下図は,Defunge のプログラムの例である: 6>--v. .^--_@ Defunge の言語仕様は以下のようになっている. Defunge のプログラムは、左上のマスから右向きで開始する. (左上のマスの命令が最初に実行される.) 命令によって進む向きが上下左右に変更されることがある. 端に達したら反対側の端へジャンプする. メモリは 0 から 15 までの 1 つの整数を記憶することができる. メモリにははじめ 0 が記憶されている. Defunge の命令は以下の通りである. '<' … 実行の向きを左にする. '>' … 実行の向きを右にする. '^' … 実行の向きを上にする. 'v' … 実行の向きを下にする. '_' … メモリの値が 0 ならば実行の向きを右に,そうでなければ左にする. '|' … メモリの値が 0 ならば実行の向きを下に,そうでなければ上にする. '?' … 実行の向きが上下左右のいずれかにランダムに等確率で変更される. '.' … 何もしない. '@' … プログラムの実行を停止する. '0' - '9' … メモリの値を指定の数値にする. '+' … メモリの値に 1 を加える,ただし値が 15 だった場合 0 にする. '-' … メモリの値から 1 を引く,ただし値が 0 だった場合 15 にする. 入力 入力の最初の行は 2 つの整数 R, C を含む. 続く R 行はプログラムを表す。それぞれ C 文字の文字列を含む。 出力 プログラムが停止する可能性がある場合は YES と出力せよ. そうでないとき,NO と出力せよ. 制約 1 ≤ R ≤ 20 1 ≤ C ≤ 20 入出力例 入出力例 1 入力例 1: 2 6 6>--v. .^--_@ 入力例 1 に対する出力: YES 入出力例 2 入力例 2: 2 6 5>--v. .^--_@ 入力例 2 に対する出力: NO 入出力例 3 入力例 3: 2 6 .>--v. .^--?@ 入力例 3 に対する出力: YES 補足 Defunge は Befunge と類似している. Befunge の理解は Defunge の理解を助けるかもしれないが, 異なる点も多いため,注意せよ. http://ja.wikipedia.org/wiki/Befunge
[ { "submission_id": "aoj_2262_9264727", "code_snippet": "/* _ ____ */\n/* AC U /\"\\ u U /\"___| AC */\n/* \\/ _ \\/ \\| | u */\n/* AC / ___ \\ | |/__ AC */\n/* /_/ \\_\\ \\____| ...
aoj_2266_cpp
問題 H : キャッシュ戦略 今,G○○gle Code Jam の地区大会が始まろうとしている. 斜め右前の席に座っている男の ID は lyrically と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 僕の記憶の中の lyrically は,アルゴリズムの力や実装の力もさながら, 気合で問題に正解することにも定評があった. 例えば,計算量が多少悪いプログラムでも,上手な実装をすることで高速にし, 正解とするようなことも得意としていた. プログラムを高速にする上で,非常に大切になってくるのが, キャッシュメモリとの親和性である. 問題 ブロック毎に読み込みのコストが異なるメモリを考える. 起こる全てのメモリアクセスがあらかじめ分かった状態での, フルアソシアティブのキャッシュメモリでの最善なキャッシュ戦略を求めたい. 以下,平易な言葉で説明する. M 個の箱と, N 個のボールがある. ボールには 1 から N までの番号が付いており, 各ボール i には重さ w i が決まっている. また,長さ K の数列 a 1 , a 2 , …, a K が与えられる. 各 a j は 1 ≤ a j ≤ N を満たす整数である. はじめは全ての箱は空である. j = 1, 2, …, K の順に,以下を行いたい. ボール a j が入っている箱があれば,何もしない. この操作にかかるコストは 0 である. ボール a j が入っている箱がなければ,いずれかの箱にボール a j を入れる. ボールを箱に入れる際,もし既にその箱に入っているボールがあれば,そのボールは外に出す. この操作にかかるコストは w a j である.(コストは,入れる箱や取り出すボール等にはよらない.) コストの和の最小値を計算するプログラムを作成せよ. 入力 入力の最初の行は 3 つの整数 M , N , K を含む. 続く N 行の i 行目には整数 w i が書かれている. 続く K 行の j 行目には整数 a j が書かれている. 出力 コストの和の最小値を出力せよ. 制約 1 ≤ M ≤ 10 1 ≤ N ≤ 10 4 1 ≤ K ≤ 10 4 1 ≤ w i ≤ 10 4 1 ≤ a j ≤ N 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ M ≤ 3 1 ≤ N ≤ 10 1 ≤ K ≤ 10 3 入出力例 入力例 1 入力例 1: 3 3 6 10 20 30 1 2 3 1 2 3 入力例 1 に対する出力例: 60 入力例 2 入力例 2: 2 3 6 10 20 30 1 2 3 1 2 3 入力例 2 に対する出力例: 80
[ { "submission_id": "aoj_2266_10849099", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\nconst int N = 10005;\nconst int M = N * 4;\n\nint n, m, k;\nint w[N];\nint a[N];\nint lst[N];\n\ntypedef int ftype;\nconst ftype INF = 0x3f3f3f3f;\nint head[N], to[M], nxt[M], to...
aoj_2265_cpp
問題 G : プログラミングコンテストチャレンジブック 今,G○○gle Code Jam の地区大会が始まろうとしている. 前の席に座っている男の ID は omeometo と言うらしい. 後ろの席に座っている男の ID は jellies と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 彼らは,机の上に蟻のイラストが掛かれた本を持っている. あれはもしや…プログラミングコンテストのアルゴリズムを最初に網羅し,今では発売禁止となった伝説の本, 「プログラミングコンテストチャレンジブック」ではないか? 僕は,omeometo がトイレに行ったのを見計らい,少し拝借して,読んでみることにした. プログラミングコンテストチャレンジブック …しかし,僕は一瞬で本を読み終えてしまった. 拍子抜けだ.簡単すぎる. 少し古い本だからといって,こんな内容でよく売る気になったものだ. 例えば,最初の三角形の問題.これは,簡単すぎてお話にならない.自分だったら,こうする. 問題 N 本の直線状の棒がある.棒 i の長さは a i である. あなたは,それらの棒から 6 本を選び, それらの 3 本ずつで,2 個の三角形を作ろうと考えている. 3 本の棒はそれぞれ三角形の辺として用い,2 つの棒が触れる位置は棒の端点のみとする. つまり,棒の一部を三角形の辺として使うことは許されず,必ず棒全体を辺としなければならない. また,棒の太さは考えず,三角形は正の面積を持たなければならないものとする. 2 個の三角形の周長の和の最大値を求めよ. ただし,2 個の三角形を作ることができない際には 0 を答えとせよ. 入力 入力の最初の行には整数 N が書かれている. 続く N 行の i 行目には 1 つの整数 a i が書かれている. 出力 答えの整数を出力せよ. 制約 1 ≤ N ≤ 10 5 1 ≤ a i ≤ 10 15 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 100 入出力例 入力例: 6 1 1 1 1 1 1 入力例に対する出力: 6
[ { "submission_id": "aoj_2265_10042900", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nbool check3(vector<int> a) {\n //for (int i: a) cout << i << ' '; cout << endl;\n\tsort(a.begin(), a.end());\n\treturn a[2] < a[1] + a[0];\n}\n\nbool check6(vector<int> a) {...
aoj_2269_cpp
問題 K : 巡回セールスマン問題 2011 年のあの頃,まだプログラミングコンテストはとても平和であり, 我々はルールを精読せずに気軽にコンテストに参加していた. しかし,そこに G○○gle の陰謀は迫っていた. ある日,G○○gle によって秘密裏に買収されていた T○pC○der のコンテストは, 人知れずうちにルールが改変されており,命がけのプログラミングコンテストとなっていた. そして僕は,そこで,何も知らず, まっ先に kita_masa のプログラムのオーバーフローを突いたのだ…. (iwi) 「うう…ああ…」 wata 「今やるべきことは自分を攻めることではない.チャンスを無駄にする気か?」 …そういえば,今こいつは,部屋に居る全ての人間を一瞬にして倒していた. どうなっているんだ? wata 「まず,知っているかもしれないが, G○○gle は NP 完全問題を含む多くの計算困難と言われてきた問題に対する多項式時間の効率的なアルゴリズムを持っている.P vs NP 問題を解決したが,敢えてそれを秘匿することによってここまで成長したのだ.」 (iwi) 「もしやとは思っていたが,やはりか…. しかし,お前は今,G○○gle の人間を一瞬にして倒した.もしやお前も…?」 wata 「残念ながら違う.しかし…お前なら,分かるはずだろう?」 …なるほど!! そういうことか. 彼は大学時代,指数時間のアルゴリズムを高速にすることに興味を持っていた. 例え指数時間であっても,指数の底が極めて小さければ, 現実的なインスタンスに対して十分に高速な可能性がある. wata 「そういうことだ.例えば,今僕は,巡回セールスマン問題の O * (1.0001 n ) 時間のアルゴリズムを完成させようとしている.」 凄まじい,想像以上だ…. wata 「完成させるにあたって,グラフアルゴリズムに詳しいお前の力が必要だ. 手を貸してくれるな?」 (iwi) 「ああ,勿論だ.まかせろ!!」 問題 グラフが与えられる. グラフは N 個の頂点を持ち,頂点には 1 から N の番号が付いている. また,グラフは M 個の辺を持ち,辺は有向 (一方通行) で, それぞれ距離が決まっている. 頂点 1 から出発し,頂点 2, 3, …, N をこの順番に訪れ,頂点 1 へ戻る経路を考える. ここで,「この順番に訪れる」というのは,経路に含まれる頂点の部分列に 2, 3, …, N が含まれるということである. つまり,スタート地点の頂点 1 から頂点 2 への移動は, 直接の移動であっても,他の頂点を経由した移動であってもよく, 次の頂点 2 から頂点 3 への移動, さらにその次の頂点 3 から頂点 4 への移動等に関しても同様である. 同じ頂点や同じ辺を 2 度通っても構わない. グラフが与えられた時, 頂点 1 から出発し頂点 2, 3, …, N をこの順番に訪れ頂点 1 へ戻る経路のうち, 最短のものの長さを出力するプログラムを作成せよ. 経路の長さとは,経路に含まれる辺の距離の和である. 複数回含まれる辺に関しては,含まれる回数の分だけ距離を加えよ. 入力 入力の最初の行は 2 つの整数 N , M を含む. これは,グラフの頂点数と辺数を表す. 続く M 行は,辺の情報を表す. これらの行のうちの i 行目は 3 つの整数 a i , b i , c i が書かれており, 辺 i は頂点 a i から頂点 b i を結び,その距離は c i であることを表す. 出力 条件を満たす経路のうち最短のものの長さを出力せよ. ただし,条件を満たす経路が存在しない場合は,-1 を出力せよ. 制約 2 ≤ N ≤ 10 5 0 ≤ M ≤ N + 500 1 ≤ a i , b i ≤ N , a i ≠ b i 1 ≤ c i ≤ 10 3 入出力例 入出力例 1 入力例 1: 5 5 1 2 10 2 3 20 3 4 30 4 5 40 5 1 50 入力例 1 に対する出力の例: 150 入出力例 2 入力例 2: 5 5 1 5 10 5 4 20 4 3 30 3 2 40 2 1 50 入力例 2 に対する出力の例: 600
[ { "submission_id": "aoj_2269_10342595", "code_snippet": "// AOJ #2269 Traveling Salesman Problem\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1e9;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int ...
aoj_2270_cpp
問題 L : L 番目の数字 反乱を嗅ぎつけた多くの G○○gle のコーダーが我々を取り押さえにやってきた. かなりの数だ. その中には,東京大学時代に僕らを優しく指導してくれた先輩たちも多く見受けられる. 残念ながら,今では敵同士だ. wata 「予想以上の数だ…」 (iwi) 「ああ,我々が超高速指数時間アルゴリズムを持っているからと言って,それは彼らにようやく並んだに過ぎない.この数では…」 ?? 「心配要らない!君たち,アルゴリズムをこっちに渡すんだ!」 声がした窓の外を見ると,G○○gle の建物に向きあう集団がいた. 黒の生地に赤で I,緑で ○,黄色で M と書かれた T シャツ … 彼らは I○M か!? そして奇妙なことに,彼らは,向かってくる G○○gle のコーダー達を, キーボードに触れることなく倒していた. どうなっている? (iwi) 「…! ついに Wats○n 2 は完成したのか!」 wata 「どういうことだ?」 2011 年,I○M は彼らの高い技術力を活用し, 自然言語で出題されたクイズに高速かつ正確に応答するシステムである Wats○n を完成させ, クイズ番組にてクイズ王と呼ばれてきた人間たちに勝利した. そして 20XX 年の今,Wats○n は, プログラミングコンテストの問題に対し解答のプログラムを作成するシステムとして生まれ変わったのだ. I○M の登場により,我々を取り囲む人数が減ってゆく. wata 「チャンスだ!半分は任せられるか?」 (iwi) 「もちろんだ!」 僕は,妄想と決別したことにより,全てを思い出していた. 当然,当時の知識や経験は,今から見ればレベルが低すぎて役に立たない. しかし,一番大切なものを思い出すことができた. それは,プログラミングコンテストを愛する気持ちである. 問題を開く瞬間のワクワク感,サンプルが通った瞬間の盛り上がり, ステータスが更新される瞬間の緊張,勝利の瞬間の喜び. (iwi) 「必ず kita_masa の復讐を果たし,この狂った世界を終わらせる!」 そして,僕は自信の 1 題を繰り出す. これは,東京大学時代の自分が愛した問題「 K 番目の数字」を独自のテクニックにより一般化かつ大規模化した問題である. 問題 グラフが与えられる. N 個の頂点があり,頂点に 1 から N の番号が付いている. これらの頂点は, N - 1 本の辺によりツリー状に接続されている. 各頂点 v には数字 a v が定まっている. 下図は,与えられるグラフの例である. 各頂点には上部に頂点の番号が,下部にその頂点に定められた数字 ( a v ) が書かれている. 与えられるグラフの例 次に, Q 個の以下のようなクエリが与えられる. 各クエリ q には,頂点 v q , w q と整数 l q が指定される. 頂点 v q から頂点 w q への経路に含まれる頂点の数字のうち l q 番目に小さいものを求めよ. 経路は単純なもののみを考える.単純な経路とは,同じ頂点を二度通らない経路のことである.グラフがツリー状であることから,単純な経路は一意である. 経路には両端点(すなわち頂点 v q , w q )を含むものとする. 例えば,上図のグラフが与えられている時, ( v q , w q , l q ) = (1, 6, 3) なるクエリ q に対する答えは 7 である. これは,頂点 1 から頂点 6 への経路には頂点 1, 3, 4, 6 が含まれており, それらに定まった数字 2, 5, 8, 7 の 3 番目めに小さな数字は 7 であることによる. グラフとクエリを受け取り,結果を出力するプログラムを作成せよ. 入力 入力の最初の行は 2 つの整数 N , Q を含む. これは,頂点数とクエリの数を表す. 続く N 行は,各頂点の持つ数字を表す. これらの行のうちの v 行目は 1 つの整数 x v が書かれており, これは頂点 v の持つ数字を表す. 続く N - 1 行は,辺の情報を表す. これらの行のうちの e 行目は 2 つの整数 a e , b e が書かれており, これらは辺 e が結ぶ 2 つの頂点を表す. 続く Q 行は,クエリの情報を表す. これらの行のうちの q 行目は 3 つの整数 v q , w q , l q が書かれており, これらはクエリ q の情報を表す. 出力 出力は Q 行からなる. q 行目にクエリ q の答えを出力せよ. 入力に関する制約 1 ≤ N , Q ≤ 10 5 1 ≤ x v ≤ 10 9 1 ≤ a e , b e ≤ N 1 ≤ v q , w q ≤ N 頂点 v q から頂点 w q への経路には l q 個以上の頂点が存在する. 入出力例 入力例: 6 11 2 4 5 8 9 7 1 3 2 3 3 4 4 5 4 6 1 6 1 1 6 2 1 6 3 1 6 4 1 2 1 1 2 2 1 2 3 2 5 1 2 5 2 2 5 3 2 5 4 入力例に対する出力: 2 5 7 8 2 4 5 4 5 8 9
[ { "submission_id": "aoj_2270_10978199", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n// #include <atcoder/all>\n// using namespace atcoder;\n\nusing ll = long long;\n#define r...
aoj_2274_cpp
問題 D 列の構成 問題文 1 から N までの相異なる整数が N / 2 個書かれたカードがいくつか与えられるので,次の条件を満たすような長さ N の数列 seq を 1 つ作って出力して欲しい. seq の各要素は 0 か 1 である. すべてのカードについて以下が成り立つ: カードに書かれた数字を card[1], ..., card[N/2] とする.このときカードに書かれた部分の和: seq[card[1]] + ... + seq[card[N/2]] が N / 8 以上かつ 3N / 8 以下になっている. 例えば, N = 8 でカードが 2 枚渡され,カードに書かれている数字がそれぞれ [1, 2, 7, 8] , [4, 5, 7, 8] であったとする.このとき seq=[0, 1, 0, 1, 0, 1, 0, 1] や seq=[0, 0, 0, 0, 1, 1, 1, 1] とおくと条件を満たすようにできている. 入力形式 入力は次の形式で与えられる. N K card 1 [1] card 1 [2] ... card 1 [N/2] card 2 [1] card 2 [2] ... card 2 [N/2] ... card K [1] card K [2] ... card K [N/2] 1 行目において N は構成するべき数列の長さ, K はカードの枚数である. 続く K 行には各カードの情報が与えられる. card i [1], ..., card i [N/2] が i 番目のカードに書かれている数字である. 出力形式 seq の i 番目の要素が i 文字目に対応するように数列 seq を 1 行に出力せよ. なお,どの入力に対しても解は必ず少なくとも 1 つは存在する. 制約 8 ≤ N ≤ 1,000 , 1≤ K ≤ N / 2 N は 8 の倍数 1 ≤ card i [1] < card i [2] < ... < card i [N/2] ≤ N 入出力例 入力例 1 8 2 1 2 7 8 4 5 7 8 出力例 1 01010101 入力例 2 8 3 2 3 4 6 3 4 5 8 3 4 6 8 出力例 2 01110011
[ { "submission_id": "aoj_2274_10809100", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#i...
aoj_2276_cpp
問題 F ボ〜ル 問題文 2 次元平面上の原点にボールがあり, x 軸の正方向との角度が反時計回りに見て 0 度 180 度までの間の方向に一様な確率で発射されようとしている (発射される方向は整数角であるとは限らない).ボールの大きさは十分小さく,平面上では点であると見なすことにする.この問題における目的は,このボールをできるだけ高い確率で捕獲することである. 平面上に N 個の場所 (x i , y i ) が与えられる.ボールを捕獲するために,あなたは N 個の場所から K 個の場所を選んで,それぞれの場所に人を配置することができる.人は i 番目の与えられた場所に対して半径 r i 以内の距離まで動いてボールを取ることが出来る. 人の配置をうまく選んでボールを捕獲できる確率を最大にするとき,その確率を出力せよ. 入力形式 入力は以下の形式で与えられる. N K x 1 y 1 r 1 ... x N y N r N N はボールを捕獲するために人を置くための場所の数であり, K はその中から使うことの出来る場所の数である. (x i , y i ) は i 番目の場所の座標であり, r i はそこから動くことの出来る距離である. 出力形式 確率を小数表記で 1 行に出力せよ.小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 1≤ N≤ 1,500, 1≤ K≤ N |x i | ≤ 1,000, |y i | ≤ 1,000, 1 ≤ r i < (x i 2 + y i 2 ) 1/2 入力値はすべて整数である. 入出力例 入力例 1 2 1 10 10 10 -10 10 10 出力例 1 0.50 2 つ場所があり,そのうちのどちらかに人を配置できる.この場合,どちらに配置しても確率は 1/2 になる. 入力例 2 2 2 10 10 10 -10 10 10 出力例 2 1.0 入力例 3 5 3 -10 -10 5 10 10 2 -10 10 3 -10 0 4 10 0 2 出力例 3 0.3574057314330 入力例 4 4 2 1 1 1 2 2 2 3 3 3 4 4 4 出力例 4 0.50
[ { "submission_id": "aoj_2276_4339412", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,m,n) for(int i=(m);i<(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\nusing ll = long long;\nconst int INF = 0x3f3f3f3f;\nconst ll LINF...
aoj_2268_cpp
問題 J : 乱択平衡分二分探索木 ところで,G○○gle Code Jam の地区大会で右の席に座っていた男の ID は rng_58 と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 彼女はいわゆる無表情不思議系キャラだったような気がする. ということは,彼は知らない男だ.rng とは何の略だろう. おそらく Random Number Generator に違いない. 乱択アルゴリズムを得意とするのだろう. ところで,乱数を用いた平衡二分探索木である Treap は, 2011 年頃のプログラミングコンテストではしばしば用いられていたようだが, 20XX 年の今では,あまり使用されていない. 当時 Treap は実装が平易であるという理由により Splay 木や Scapegoat 木と並んでよく使われていた. 20XX 年の今,命をかけたプログラミングコンテストが日常的に行われるこの世界では, 実装が多少平易になるというような甘い根拠での選択は到底考えられない. 皆,少しでもプログラムが高速になるよう考えているし, 世界大会出場者ともなれば,Left-leaning 赤黒木程度なら十数秒で記述できるのが当然だ. 例えば,Treap が赤黒木よりも遅くなってしまうのは, 高さの定数倍が効いてくるからであると言われている. ここは,落ち着いてそれを数値的に考察することにより,精神の安定を取り戻そう. 問題 キーを実数とする Treap に N 個のランダムな要素を挿入した際に, 高さが h ( h = 0, 1, …, N - 1 ) となる確率を求めよ. 以下,より詳しく説明する. Treap とは乱数を用いた平衡二分探索木である. 各ノードは,キーの他に,優先度という値を持つ. ここでは,キーと優先度は 0 から 1 までの実数値とする. Treap では,以下の 2 つの条件が常に保たれる. キーに関する条件 各ノードに関して,左の子以下のノードは自分より小さなキーを持つ 各ノードに関して,右の子以下のノードは自分より大きなキーを持つ 優先度に関する条件 各ノードに関して,子のノードは自分より小さな優先度を持つ 下図は,Treap の例である. 各ノードの,上部にキーが,下部に優先度が書かれている. 7 つのノードからなる Treap の例. 挿入の操作は,以下のように行われる. 挿入するキー を x とおく. 挿入する新しいノードの優先度 p を 0 から 1 までの一様分布よりランダムに決定する. 通常の二分探索木と同様に,まずは優先度を無視し新しいノードを挿入する. 優先度の条件を満たすように,新しいノードを上に持ち上げるような回転操作を必要なだけ繰り返す. 木の回転操作に関しては,補足の節に詳しく記述したので,参考にせよ. 下図は,先ほどの例にキーとして 0.5,優先度として 0.7 を持つ頂点を挿入した過程の例である. まずは優先度が無視され,ノードが挿入される. 回転を行い新しいノードが上へ行く. さらに回転を行うと,優先度の条件が満たされるので,挿入は終了する. 空の Treap に N 個のキーを, やはり 0 から 1 までの一様分布よりランダムに選び挿入してゆくとする. 最終的に高さが h となる確率を, 各 h = 0, 1, …, N - 1 について求めよ. なお,実数は十分な精度を持って扱われるとし, 例えば 2 つのノードの優先度が等しくなる確率は 0 としてよい. また,高さとは,根のノードから各ノードに至るために通らなければならない辺の本数の最大値である. 入力 1 つの整数 N が書かれている. 出力 出力は N 行から成る. i 行目に高さが i - 1 となる確率を出力せよ. 値は小数点以下何桁表示しても構わない. 制約 1 ≤ N ≤ 3 × 10 4 出力する値は 10 - 5 以下の誤差を含んでいても構わない. 入出力例 入力例 1: 1 入力例 1 に対する出力例: 1.0 入力例 2: 2 入力例 2 に対する出力例: 0.0000000000 1.0000000000 補足 木の回転操作には,右回転と左回転がある. 右回転とは,以下の 2 つの図の 1 つめの状態を 2 つめの状態にする操作であり, 左回転とは,以下の 2 つの図の 2 つめの状態を 1 つめの状態にする操作である. 右回転をする前,または左回転をした後 右回転をした後,または左回転をする前 Treap においては,持ち上げたいノードがその親ノードの左の子であった場合, 持ち上げたいノードを上図の P とするような右回転を適用し, 持ち上げたいノードがその親ノードの右の子であった場合, 持ち上げたいノードを上図の Q とするような左回転を適用する. 木の回転操作について,より詳しくは,例えば以下を参照せよ. http://ja.wikipedia.org/wiki/%E6%9C%A8%E3%81%AE%E5%9B%9E%E8%BB%A2 Treap について,より詳しくは,例えば以下を参照せよ. http://en.wikipedia.org/wiki/Treap http://www.prefield.com/algorithm/container/treap.html
[ { "submission_id": "aoj_2268_10307688", "code_snippet": "// AOJ #2268 Randomized Self-Balancing Binary Search Tree\n// 2025.3.17\n\n#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <typename R>\nvoid fft(vector<complex<R>> &f, int dir) {\n int n = f.size();\n assert(n == (1 << static_cast<in...
aoj_2278_cpp
問題 H あばれうなぎ 問題文 きつねのしえるはうなぎを食べるのが好きである. 2T+1 枚の鉄板が連続して並んでおり,順番に -T, -T+1, ..., T の番号が付いている.しえるはこれらの鉄板に熱を加え,生きたうなぎを焼こうとしている.うなぎを焼く手順は以下のようなものである. まず,時刻 t=-1 のときに各鉄板にどれだけのエネルギーを加えるかを決め,実際にエネルギーを加える.各鉄板に加えるエネルギーの総和は E 以下でなければならない.すなわち, i 番の鉄板に加えるエネルギーを E(i) として, E(i) ≥ 0, E(-T)+...+E(T) ≤ E でなければならない.なお,エネルギーは整数でなくてもよい. エネルギーを加えたあと,鉄板の熱さは E(i) / C(i) になる.ここで, C(i) は i 番の鉄板の比熱である. t = -0.5 のときにうなぎを 0 番の鉄板に載せる. t = 0, 1, 2,..., T のときに,うなぎは今自分がいる鉄板の熱さだけ加熱される. t = 0.5, 1.5, 2.5,...,T-0.5 のとき,うなぎは自分の今いる鉄板にとどまるか,両隣の鉄板に移動することができる. t = T+0.5 のときにうなぎを鉄板の上から回収する. ところで生きたうなぎというのはとても活きがよくさらに頭も良いので,もしかすると自分にかかる熱さの総和が最小になるように動いたりするんではないかとしえるは不安になった.そうなると鉄板に適当に熱を加えただけではうなぎを十分に加熱できない恐れさえある. そこであなたには,うなぎが常に最適に動くとして,うなぎに与えることのできる熱さの和の最大値を求めて欲しい. 入力形式 入力は以下の形式で与えられる. T E C(-T) C(-T+1) ... C(T) T はうなぎを熱する時間, E は鉄板に与えることの出来るエネルギーの総和, C(i) は i 番の鉄板の比熱である. 出力形式 1 行目にうなぎに与えられる熱さの和の最大値を出力せよ.小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 1 ≤ T ≤ 10 5 1 ≤ E ≤ 10 5 1 ≤ C(i) ≤ 10 5 入力に含まれる値はすべて整数である. 入出力例 入力例 1 1 100 1 1 1 出力例 1 100.0 この場合は 0 番の鉄板にエネルギーを全て加えるのが最適である. 入力例 2 2 100 1 2 100 2 1 出力例 2 2.8301886792453 入力例 3 5 100000 99999 99999 99999 1 1000 1000 1000 1 99999 99999 99999 出力例 3 199.4680851063830 謝辞 この問題は Writer の二人がインド料理 RAJU 百万遍店とマクドナルド百万遍店において夏の暑さに辟易する中で作られた.
[ { "submission_id": "aoj_2278_4376010", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100005\n\nin...
aoj_2282_cpp
Problem B: B問題 R大学の2D好きの人たち (2DRespecters)は、A津大学で開催されるプログラミングの練習合宿に参加する。 この練習合宿では、参加者たちが自作のプログラミング問題を持ち寄り、練習に用いる。 2DRespectersも、いくつかの問題を作成することになった。 しかし、合宿の3日前になっても、まだ、B問題が完成していなかった。 最初のB問題の担当者が作成した問題は、担当者が問題の制約を難しくし過ぎてしまったため、C問題以降にまわされてしまったのだ。 そして、簡単すぎても、難しすぎてもいけない、微妙な難易度の調整が必要とされるB問題は、誰もが手を付けたがらなかった。 それ以来、B問題担当者の椅子は空席のままである。 合宿3日前になって、遂に、この問題を解決するために、2DRespecters期待のホープが立ち上がった。 だが、彼は、B問題を作成する気がない。 彼は、自分でB問題を作成したくないので、B問題の作成担当を他者に押し付ける手法を考え出したのだ。 彼の手法は、表面上、B問題の作成担当者を平等に決定する。 彼の手法は、以下の通りである。 問題作成のために要した作業時間が最も少ない人がB問題担当者になる。 作業時間が最も少ない人が複数人居た場合は、より問題難易度の小さい問題の担当者がB問題作成担当者になる。 作業時間が最も少ない人が複数人居て、その中で、問題難易度の最も小さい人が複数人いた場合は、ホープの彼がB問題作成担当者になる。 彼の手法は、一見、最も仕事をしていない人がB問題の作成を担当するため、2DRespectersのメンバー全員に快く受け入れられた。 しかし、彼の手法には、以下のような裏がある。 各個人の作業時間は、各個人が口頭で申請するため、嘘の申請をすることが可能である。 問題の作業可能時間を超える作業時間や負の作業時間を申請すると、嘘だとばれる。 ある問題の作成のための作業時間は、必ず問題難易度の整数倍になることが知られているため、これに反する作業時間を申請した場合も、嘘だとばれる。 嘘の申請をした人の嘘がばれた場合、その人が必ずB問題作成担当者になる。 申請は一人ずつ順番に行い、嘘がばれる場合は、申請の時点でばれる。 一人でも嘘の申請がばれた時点で、B問題作成担当者が決まり、それ以降の申請は行わない。 申請者は、自分より前の申請のみを考慮し、申請の時点で自分がB問題作成担当者にならないような最小の作業時間を申請するものとする。 申請の時点で、申請者がB問題作成担当者にならないような報告ができない場合は、嘘だとばれないような最大の作業時間を申請する。 各個人の担当する問題難易度のリストが申請順に与えられたとき、誰がB問題作成担当者になるかを求めるプログラムを作成せよ。 ホープの彼の申請順番は、一番最後である。 なお、本問題はフィクションであり、本問題の作成の経緯とは一切の関係がない。 Input 入力は、複数のデータセットからなり、データセットの終わりは、半角スペースで区切られた0が二つだけを含む行で表される。 データセットの総数は40以下である。 データセットの1行目では、整数 n ( 2 ≤ n ≤ 100 1,000 )と整数 m ( 1 ≤ m ≤ 1,000 )が半角スペース区切りで与えられる。 データセットの2行目では、整数 a_1 , a_2 , ..., a_n ( 1 ≤ a_i ≤ 1,000 , 1 ≤ i ≤ n )が半角スペース区切りで与えられる。 1行目の n と m は、それぞれ、申請者の数と問題の作業可能時間を表す。 2行目では、申請順に並べられた問題難易度のリストが与えられており、 a_i は、問題の難易度を表す。 Output それぞれのデータセットごとに、B問題作成担当者の申請順番を1行で出力せよ。 Sample Input 3 100 3 7 4 5 24 4 6 12 3 8 5 1 1 1 2 2 3 0 0 Output for Sample Input 2 4 5
[ { "submission_id": "aoj_2282_1960392", "code_snippet": "#include <bits/stdc++.h>\n#define f first\n#define s second\nusing namespace std;\nint n,m,a[1001];\ntypedef pair <int,int> P;\ntypedef pair<P,int> PP;\n\nint solve(){\n PP t[1001];\n t[0]=PP(P((m/a[0])*a[0],a[0]),0);\n for(int i=1;i<n;i++){\n so...
aoj_2280_cpp
問題 J Mod 3 Knights Out 問題文 ある日の夕方,コンテストの問題を解き終えた二人は今日の問題について話し合っていた. A「くそー,あのライツアウトの問題,mod 2 じゃなくて mod 3 とか mod 7 とかだったら解法分かったのにー!」 B「じゃあ,mod 3 で別の方法で解く問題を作ればいいんですね.分かりました!」 こうして次のような問題が誕生した. H × W のチェス盤がある.チェス盤の各マスには 0 から 2 の整数が書かれている.このチェス盤にナイトを置いていく.ただし,各マスには多くても 1 体のナイトしか置けない.各マスについて, (マスの数値+そこを攻撃するマスにいるナイトの数)=0 mod 3 が成り立つようなナイトの配置を 良い 配置と呼ぶ.攻撃するマスとはそのマスから縦方向に ±2 マスかつ横方向に ±1 マス,もしくは縦方向に ±1 マスかつ横方向に ±2 マスずれたマスのことである. 良いナイトの配置の数を求めよ.答えは大きくなる可能性があるので 1,000,000,007 で割った余りを答えよ. 入力形式 最初の行に H と W がスペース区切りで与えられる. 次の H 行には、チェス盤のマス目に書かれている数値として W 個の 0 , 1 , 2 のいずれかの整数がスペース区切りで与えられる. 出力形式 良いナイトの配置の数を 1,000,000,007 で割った余りを出力せよ. 制約 1 ≤ H ≤ 50 1 ≤ W ≤ 16 入出力例 入力例 1 5 5 0 2 0 2 0 2 0 0 0 2 0 0 0 0 0 2 0 0 0 2 0 2 0 2 0 出力例 1 5 入力例 2 3 3 2 2 2 2 0 2 2 2 2 出力例 2 8 入力例 3 7 7 2 2 2 2 2 2 2 2 1 1 2 1 1 2 2 0 1 0 1 0 2 2 2 0 2 0 2 2 2 0 1 0 1 0 2 2 1 1 2 1 1 2 2 2 2 2 2 2 2 出力例 3 96 入力例 4 7 3 2 2 2 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 2 2 2 出力例 4 8 入力例 5 6 6 0 2 0 1 0 2 2 0 1 2 2 2 0 2 2 0 0 0 2 0 2 0 2 0 0 2 2 1 0 2 0 0 0 2 0 2 出力例 5 1 入力例 6 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 出力例 6 1 謝辞 この問題は Tester と Writer が アジア地区予選の問題 に関して話し合ったのをきっかけとして作られた。
[ { "submission_id": "aoj_2280_10433362", "code_snippet": "// AOJ #2280 Mod 3 Knights Out\n// 2025.4.30\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf)...
aoj_2275_cpp
問題 E Fox Number 問題文 きつねのしえるは数字について考えるのが好きである.ある日,しえるはこの世にねこの数字というものがあることを知り,自分もそのような数字が欲しくなってしまった.そこで,次の性質を満たす数字をとりあえず Fox Number と呼ぶことにした. [性質]: 整数 N は k (≥ 1) 個の素数 p 1 , ..., p k と正整数 e 1 , ..., e k で, p 1 < ... < p k , e 1 ≥ ... ≥ e k を満たすものによって N = p 1 e 1 × ... × p k e k と書けるとき,Fox Number であると呼ぶことにする. ところでしえるはこの性質をその場の思いつきで決めてしまったのでこれがどういう性質を持っているのか全くわかっていない.そこで,この数字の性質を調べるために区間 [A-B, A+B] に含まれる Fox Number の個数を出力して欲しい. 入力形式 1 行目に以下の 2 つの整数が与えられる. A B 出力形式 1 行目に, A-B 以上 A+B 以下の Fox Number の個数を出力せよ. 制約 1 ≤ A ≤ 10 12 0 ≤ B ≤ 5 × 10 5 入出力例 入力例 1 18 2 出力例 1 4 16 = 2 4 , 17 = 17 1 , 19 = 19 1 , 20 = 2 2 × 5 1 は Fox Number である.一方で 18 = 2 × 3 2 は Fox Number ではない.合計して 4 つの Fox Number がある. 入力例 2 100 10 出力例 2 18 入力例 3 123456 789 出力例 3 1464
[ { "submission_id": "aoj_2275_4301948", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <numeric>\n#include <algorithm>\n#include <deque>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <iomanip>\n#include <map>\n#include <stack>\n#include <queue>\n#include <...
aoj_2277_cpp
問題 G XOR 回路 問題文 計算機科学実験及演習 3 は CAD を用いて CPU を設計する授業である.CPU は多くの回路を組み合わせなければ動かず,その中の 1 つに n ビット入力の XOR 回路がある.ここで XOR 回路とは,入力ビット列 x 1 x 2 ...x n に対して, x 1 + x 2 + ... + x n \ (mod 2) を出力する回路のことを言う.しかし完璧に動作する XOR 回路を設計するのは時間がかかるので,とりあえず n ビットのうち k ビットだけ使う XOR 回路 A を作ることにした.つまり,ある i 1 , ... , i k が存在し,回路 A は x i 1 + x i 2 + ... + x i k \ (mod 2) を出力する. 暫く後に今度は k ビット入力の XOR 回路が欲しくなった.何だ簡単ではないか.先ほどの回路 A を使えばよい.ただ残念なことに,回路 A がどの k ビットを使っていたのか忘れてしまった上に,回路 A の設計図も間違って削除してしまった.しかしコンパイル済みの回路 A は残っている.なので,入力 x 1 x 2 ...x n を入れて回路 A を実行することで,その出力 x i 1 + x i 2 + ... + x i k \ (mod 2) を見ることは出来る. 出来るだけ作業の時間を短くしたいので,回路 A の実行回数には上限を設定することにしよう.どうすれば回路Aが依存しているビット i 1 , ... , i k を見つけられるだろうか? 入出力 最初に n と k がスペース区切りで与えられる.以降,プログラムは回路 A に入力を与え,その出力を読むことが出来る.例えば C/C++ で回路 A にビット列 x 1 x 2 ...x n を与えるには printf("? x 1 x 2 ...x n \n"); fflush(stdout); とする.ここで各 x i の間にスペースをいれてはならない. 次に, scanf("%d", &v); とすると v に対応する出力 x i 1 + x i 2 + ... + x i k \ (mod 2) が入る. 最終的に回路 A が依存するビット i 1 ,...,i k を出力するには printf("! i 1 i 2 ...i k \n"); fflush(stdout); とする.ここで各 i j の間はスペースを丁度 1 つずついれる. 制約 1 ≤ n ≤ 10,000 1 ≤ k ≤ min(10, n) 各データセットごとに,回路 A の実行回数の上限は 200 回であり,それを超えると誤答 ( Query Limit Exceeded ) と判定される. 入出力例 入力例 1 以下の例はプログラムの入出力の例である.左の列はプログラムの出力,右の列はプログラムへの入力を時系列順に示している.最初に n k が入力として与えられる.ここでは n = 2, k = 1 である.次に回路 A に 00 という入力をいれると,回路 A は 0 を返した.次に回路 A に 01 という入力をいれると,回路 A は再び 0 を返した.このことから回路 A は 1 ビット目のみ利用していることが分かり,プログラムは 1 を解答として出力した. プログラムの出力 プログラムへの入力 2 1 ?00 0 ?01 0 !1 入力例 2 以下の例では, n = 2, k = 2 であり,直ちに回路 A が 1 ビット目と 2 ビット目の両方を利用していることが分かる.よってプログラムは 1 と 2 を解答として出力した. プログラムの出力 プログラムへの入力 2 2 !1 2
[ { "submission_id": "aoj_2277_10851253", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, k, qq[10005], ans[10005];\n\nint ask(int lf = 0, int rg = n - 1){\n cout << \"?\";\n for(int i = 0; i < n; i ++)\n cout << ((lf <= i && i <= rg) ? qq[i] : 0);\n cout << endl;\n\n...
aoj_2283_cpp
Problem C: Seishun 18 Kippu R大学のとある学生sirokurostoneはA津大学で行われる合宿に参加しようとしていた。 他のメンバーは新幹線を使用する予定なのだが、sirokurostoneは青春18切符を使って向かおうとしていた。 同じく青春18切符使いのとある2D好きな人物も合宿に参加しようとしていた。 どうせならと一緒に行こうと考えたsirokurostoneは途中の駅で2D好きな彼を拾って向かおうと考えた。 sirokurostoneはR大学を出る前に遅延状況を確認してすこしでも時間のかからないルートを通ってA津大学のある駅に着きたいと考えている。 sirokurostoneはルートが確定した後に2D好きな彼にもその情報を伝えるつもりである。 しかし、sirokurostoneは遅延状況を見てもどのルートを通れば早くA津大学のある駅に到着できるか分からず困っている。 そこで、あなたの仕事はsirokurostoneに代わって遅延状況からどれくらいでA津大学のある駅に着くのかを求めることである。 各駅間には、駅間の距離と遅延予想時間がある。 電車は動き始めてから40km/hを維持するものとする。 地点aと地点bの間の移動時間は、 距離/40+遅延予想時間 で求めることができる。 各駅での停車時間は無視してよいものとする。 Input 複数のデータセットの並びが入力として与えられる。 データセット数は50以下であることが保障されている。 各データセットは、次の形式をしている。 n m s p g a 1 b 1 d 1 t 1 ... a i b i d i t i ... a m b m d m t m 整数 n ( 3 ≤ n ≤ 500 )と m ( 2 ≤ m ≤ 5000 )は、それぞれ駅の数、各駅間の線路の数の合計を表す。 s 、 p 、 g は、それぞれsirokurostoneが乗る駅、2D好きな人物が乗る駅、A津大学がある駅の場所を示す文字列である。 s 、 p 、 g はそれぞれ違う文字列である。 続く m 行は、駅間情報を表す。 a i 、 b i は、線路がつなぐ駅を示す文字列である。 a i と b i は、双方向に行き来することができる。 a i と b i が一致することはない。 整数 d i ( 40 ≤ d i ≤ 10000 )は、 a i と b i の距離を表す。 d i は40の倍数であることが保証されている。 整数 t i ( 0 ≤ t i ≤ 20 )は、 a i と b i の間の遅延予想時間を示す。 駅名を表す文字列は、全てアルファベットの小文字および大文字からなる20文字以下の文字列であらわされる。 駅と駅の間の線路は高々1本である。 入力の終わりは2つのゼロを含む行で表される。 Output 入力データセットごとに、A津大学がある駅への到達時間(単位:時間)を出力せよ。 sirokurostoneが乗る駅から2D好きな人が乗る駅までの経路と、2D好きな人物が乗る駅からA津大学がある駅の場所までの経路があることが保証されている。 Sample Input 4 4 A B G A B 40 3 B C 80 0 A G 40 0 B G 80 0 5 6 Kusatsu Tokyo Aizu Tokyo Nagoya 120 3 Kusatsu Nagoya 40 0 Kusatsu Kanazawa 40 0 Kanazawa Aizu 40 0 Tokyo Kanazawa 40 0 Tokyo Aizu 80 4 0 0 Output for Sample Input 5 4
[ { "submission_id": "aoj_2283_9847642", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <set>\n\nusing namespace std;\nusing ll = long long;\n\nconst ll INF = static_cast<ll>(1) << 62;\n\nvoid solve() {\n ll n, m; cin >> m;\n string s, t, f; cin >> s >> t >> f;\n ma...
aoj_2284_cpp
Problem D: 伝説の剣 ※ この問題には厨二成分が多く含まれます。胸焼けにご注意ください。 ついに復活を遂げた魔王は、再び世界を闇に包むために人間界に攻め入ろうとしていた。 魔王は、本格的な侵攻を始める前に、まず伝説の剣を破壊することにした。 前回の戦争において、魔王は伝説の剣を持つ勇者によって倒された。 魔王の体は闇の衣によって守られているため、生半可な攻撃では魔王を傷つけることはできない。 しかし、伝説の剣は、神々の加護により、魔王の闇の衣すら容易に貫いてしまう。 そのため、今回の戦争に勝利するためには、なんとしてもその伝説の剣を手に入れて破壊する必要がある。 調査の末、伝説の剣はとある遺跡の最奥に安置され、次代の勇者が現われるのを待っていることがわかった。 伝説の剣は、邪悪な者や盗賊によって奪われるのを防ぐために、周囲に点在する無数の宝珠によって封印されている。 魔王は、強力な魔力を持っているため、触れるだけで宝珠を破壊することができる。 ただし、宝珠による封印は多重構造になっており、表層から順に破壊していく必要がある。 例えば、第1の封印の宝珠を破壊する前に第2以降の封印の宝珠に触れたとしても、破壊することはできない。 また、複数の宝珠が同じ封印を構成していることもあるが、魔王はそのうちの一つに触れるだけで、同時にその封印を破壊することができる。 遺跡には神聖な力が満ちており、並の魔物では入ることすらままならない。 そこで、魔王自ら赴き伝説の剣を回収してくることになった。 いかに魔王といえど、長時間その力を受け続ければただではすまない。 魔王の右腕であるあなたの仕事は、念のため魔王が全ての封印を破壊し、伝説の剣の下に辿りつくまでの時間を求めることである。 Input 入力は、複数のデータセットからなり、入力の終わりはスペースで区切られたゼロ二つからなる行である。 データセットの総数は50以下である。 各データセットは、次の形式をしている。 w h s(1,1) ... s(1,w) s(2,1) ... s(2,w) ... s(h,1) ... s(h,w) w と h は、それぞれ遺跡のフロアを表現する行列データの幅と高さを示す整数であり、それぞれ 1 ≤ w, h ≤ 100 であり、 2 ≤ w + h ≤ 100 と仮定して良い。 続く h 行はそれぞれ、スペースで区切られた w 個の文字から構成されており、文字 s(y,x) は、座標 (y,x) の地点の状態を示す。 その意味は、以下の通りである。 S:魔王が最初にいる地点。フロアに必ず1つだけ存在する。 G:伝説の剣の地点。必ず1つだけ存在する。 .:なにもない。 数字:宝珠がある地点。数字は構成する封印の番号を示す。数字は1以上の整数であり、間の数字に抜けはないとしてよい。 また、魔王は、遺跡のフロア内の上下左右の隣接する座標に移動することができ、その移動にかかる時間を1とする。 破壊できる宝珠は触れるだけで破壊できるため、宝珠の破壊には時間はかからない。 Output 各データセットに対して、魔王が全ての封印を破壊し、伝説に剣に辿りつくために必要な最短時間を出力せよ。 Sample Input 10 10 S . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . . . . . . 2 . . . . . . . . G 10 10 S . . . . . 3 . . . . . 3 . . . . . . . . . . . 1 . . . . . . . . . . . 4 . . . . . 3 . . . 1 . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . 5 . . . . 2 . . . . . . . . G 10 10 S . . . . . . . . 1 . . . . . 5 . . . . . 4 . . . . . . . . . . . . 8 . 9 . . . . . . . 10 . . . . . . . 7 . G . . . . . . . . 11 . . . . . . 3 . . . . . . . . 6 . . . . . . . 2 . . . . . . . . . . . . 0 0 Output for Sample Input 38 36 71
[ { "submission_id": "aoj_2284_7936801", "code_snippet": "#include <algorithm>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <vector>\nusing namespace std;\n#define fr first\n#define sc second\ntypedef pair<int, int> P;\nP s, g;\ntypedef pair<P, P> Q;...
aoj_2279_cpp
問題 I 山 問題文 L 氏はある都市の市長である.L 氏の市長としての手腕はすさまじく時を止め一瞬にして都市中に鉄道網を引いたり,財政の赤字を一夜にして莫大な黒字にしたり,はては巨大な怪獣をどこからともなく呼んでパフォーマンスに使うこともした. そんな L 氏は土地の隣り合った行または列同士を何度も入れ替えることででこぼこな土地を山形にするという遊びを最近発明した.土地は H×W 個のブロックに区切られており,ブロック (y,x) では海面からの高さが h y,x であると測定されている.入れ替え後のブロック (y,x) の高さを h' y,x と書くことにする.この土地が山形であるとは,以下の図のように,あるブロック (y^*,x^*) があって,そこを山頂として山頂から離れるに従って高さが単調減少しているものをいう. h' y^*-1, x^*-2 < h' y^*-1, x^*-1 < h' y^*-1, x^* > h' y^*-1, x^*+1 > h' y^*-1, x^*+2 ∧ ∧ ∧ ∧ ∧ h' y^*, x^*-2 < h' y^*, x^*-1 < h' y^*, x^* > h' y^*, x^*+1 > h' y^*, x^*+2 ∨ ∨ ∨ ∨ ∨ h' y^*+1, x^*-2 < h' y^*+1, x^*-1 < h' y^*+1, x^* > h' y^*+1, x^*+1 > h' y^*+1, x^*+2 ところで,L 氏はいくらがんばっても山形にできないような土地が存在する気がした.そこで,前もってコンピュータにその土地が山形にできるかどうかをチェックさせることにした. 入力形式 最初の行に H と W がスペース区切りで与えられる. 次の H 行に土地の高さが与えられる. 各行は W 個の数値からなり,それぞれ対応する場所の高さを表す. 出力形式 土地を山形にできるなら "YES" ,できないなら "NO" を出力せよ. 制約 1 ≤ H,W ≤ 1,000 0 ≤ h y,x ≤ 10 8 全ての h y,x は相異なる. 入出力例 入力例 1 3 3 1 3 2 7 9 8 4 6 5 出力例 1 YES 入力例 2 以下の例では 2 番目の列と 3 番目の列を入れ替えることにより土地を山形にすることができる. 3 3 9 3 6 8 2 5 7 1 4 出力例 2 YES 入力例 3 1 1 100 出力例 3 YES 入力例 4 3 3 1 3 2 4 6 5 7 8 9 出力例 4 NO 謝辞 この問題は京都市の周りに山が多いことから作られた.よって京都市の周りの山に感謝の意を表する.
[ { "submission_id": "aoj_2279_10215449", "code_snippet": "// AOJ #2279\n// Mountain 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n...
aoj_2285_cpp
Problem E: アニペロ 猛暑が続いた、長かったようで短かったような夏がもうすぐ終わろうとしていた。 そんな8月下旬のある日、とある2D好きな人物とその先輩のslipは、アニペロサマーライブ、通称アニペロと呼ばれるイベントに参加していた。 アニペロとは、さまざまなアニメソングアーティストたちが集結する、日本国内最大のアニメソングライブイベントである。 今年のアニペロは、公式で公表されていたアーティストの他に、シークレットで超豪華歌手も出演し、大盛況の内に幕を閉じた。 アニペロには初参戦だった2D好きな彼は、ライブ後の余韻にひたりつつも、ひとつの疑問を抱えていた。 「アニペロに出演するアーティストはどのように決めているのだろうか?」 彼は、数あるアーティストの中から、出演アーティストの選出をするのに、次のような方法があるのではないかと、選出法を考えてみた。 まず、アニペロのアーティストには、シークレットアーティストとスタンダードアーティストの2種類の分類があるものとする。 シークレットアーティストとは、ライブに出演することを事前公表せず、ライブ本番になって突然現れるアーティストのことを指す。 スタンダードアーティストとは、ライブに出演することを事前に公表してよいアーティストのことを指す。 全てのアーティストは、次のステータスを持つ。 アーティスト名:文字列 アーティストを雇うための金額(以下、雇用金と呼ぶ):自然数 このアーティストが出演することで、お客をどれほど満足させられるか(以下、満足度と呼ぶ):自然数 今回、ライブに出演するアーティストを選ぶために、シークレットアーティスト候補 N 人、スタンダードアーティスト候補 M 人が、すでに用意されているものとする。さらに、ライブの主催者は、アーティストを雇用するために使用できる資金 LIMIT を持っているものとする。 主催者は、次の条件を満たすように、アーティストを選出しなければならない。 N 人のシークレットアーティスト枠から、1人以上、2人以下を選出する(1人or2人なのは、シークレットがいなかったり多かったりすることを避けるためである) M 人のスタンダードアーティスト枠から、 X 人以上のアーティストを選出する アーティストを全て選出し終えたとき、雇用金の合計を、 LIMIT 以下にする 選出したアーティスト全員で得られる満足度の合計を最大化する さて、ここまで選出法を考えた2D好きな彼は、この方法でプログラムを書いてみようと思った。しかし、彼は選出法を考えるのに気力を使ってしまい、プログラムを書く気力がなくなってしまったようなので、彼の代わりにプログラムを書いてあげてほしい。 あなたの仕事は、上記の選出法に従い、アーティストを選出したときのお客の最大満足度を出力するプログラムを作成することである。 Input 入力は、複数のデータセットからなる。データセットの総数は20以下である。 各データセットは、次の形をしている。 LIMIT N M X SEC_NAME 1 SEC_E 1 SEC_S 1 ... SEC_NAME i SEC_E i SEC_S i ... SEC_NAME N SEC_E N SEC_S N NAME 1 E 1 S 1 ... NAME i E i S i ... NAME M E M S M 整数 LIMIT ( 4 ≤ LIMIT ≤ 1000 )、 N ( 2 ≤ N ≤ 100 )、 M ( 2 ≤ M ≤ 100 )、 X ( 2 ≤ X ≤ M )は、 それぞれアーティストを雇用するのに使用できる資金、シークレットアーティスト候補の数、スタンダードアーティスト候補の数、スタンダードアーティストから選出しなければならない最低人数を表す。 SEC_NAME i 、 NAME i 、はそれぞれシークレットアーティスト候補、スタンダードアーティスト候補の名前を指す30文字以下の文字列である。 文字列には、アルファベット('A'-'Z', 'a'-'z')のみが使用される。 同じアーティスト名が2回以上出現することはない。 整数 SEC_E i 、 E i ( 1 ≤ SEC_E i , E i ≤ 10 )、 SEC_S i 、 S i ( 1 ≤ SEC_S i , S i ≤ 10 ) は、それぞれシークレットアーティスト候補の雇用金、スタンダードアーティスト候補の雇用金、シークレットアーティスト候補が出演したときに得られる満足度、スタンダードアーティスト候補が出演したときに得られる満足度を表す。 入力の終了は、0が4つの行で示される。このデータについては処理する必要はない。 Output 選出法を適用してアーティストを選出したとき、お客の満足度の最大値を出力せよ。 アーティストの選出の仕方は、必ずあると仮定してよい。 Sample Input 100 2 2 2 A 5 6 B 7 8 C 1 2 D 3 4 27 2 3 3 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 27 2 3 2 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 44 3 6 5 YamatoNoHito 9 10 ZettoNoHito 9 10 TMR 10 10 SkillNoGroup 8 10 NanaSama 6 9 FRPSD 6 8 Magi3rdDeshi 5 7 Magi13thDeshi 2 2 MagicalItoh 4 6 0 0 0 0 Output for Sample Input 20 30 31 55
[ { "submission_id": "aoj_2285_9658651", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define N 110\n\nint limit, n, m, x;\nint e1[N], s1[N];\nint e2[N], s2[N];\nint dp[1010][N][N];\nint ep[1010];\n\nint main(){\n\twhile (1){\n\t\tcin >> limit >> n >> m >> x;\n\t\tif (limit == 0 && n == 0 ...
aoj_2288_cpp
Problem H: Oh, My Goat! 太郎君は、次郎君に古文のノートを借りた。 明日の古文の宿題を写させてもらうためだ。 太郎君は、用心深い性格をしているため、万が一のことを考えて、近所のコンビニで次郎君のノートのコピーを取って自宅へ帰った。 太郎君は、自宅で宿題を始めようとしたときに、慌てた。 次郎君から借りたノートが鞄に入っていなかったからだ。 どうやら、コンビニに忘れてきてしまったようだ。 太郎君は、急いでコンビニに引き返した。 コピー機とその周辺を懸命に捜索したが、ノートを見つけることはできなかった。 困り果てた太郎君は、コンビニの店長に助けを求めた。 余談だが、店長は地球環境に優しい人で、白ヤギさんを飼っている。 店長は、地球環境に気を配る心遣いを持っているため、常日頃から、ペットの白ヤギさんに、餌としてコピー機周辺の客の忘れものを与えている。 店長によると、ノートは、今晩の白ヤギさんの晩御飯であり、白ヤギさんは現在食事中だそうである。 太郎君は白ヤギさんに頼みこみ、食べかけのノートを取り返すことはできたが、ノートはビリビリに破れぼろぼろである。 太郎君は、急いで自宅に引き返した。 自室の机とその周辺を懸命に捜索したが、ノートのコピーを見つけることはできなかった。 困り果てた太郎君は、お母さんに助けを求めた。 余談だが、太郎君のお母さんは地球環境に優しい人で、黒ヤギさんを飼っている。 太郎君のお母さんは、地球環境に気を配る心遣いを持っているため、常日頃から、ペットの黒ヤギさんに、餌として太郎君の宿題を与えている。 お母さんによると、ノートのコピーは、今晩の黒ヤギさんの晩御飯であり、黒ヤギさんは現在食事中だそうである。 太郎君は黒ヤギさんに頼みこみ、食べかけのノートを取り返すことはできたが、ノートのコピーはビリビリに破れぼろぼろである。 次郎君のノートとそのコピーをビリビリのぼろぼろにされてしまった太郎君は、ノートを可能な限り復元しようと考えた。 そこで、繋がりそうなノートの切れ端同士を繋ぎ合わせて、「繋ぎ合せたノート」を作成した。 同様に、繋がりそうなノートのコピーの切れ端同士を繋き合わせて、「繋ぎ合せたノートのコピー」を作成した。 太郎君は、あらゆる可能性を考えるために、繋がりそうな切れ端同士をつなぎ合わせた。 このため、1つの切れ目に2つ以上の切れ端が繋がれてしまった箇所もあった。 繋ぎ合わせ方の例を図1に示す。 図1は、7つの切れ端と6つの切れ目からなる。 図1 切れ端"haru"と"natsu"に続く切れ目には、切れ端"wa"が繋がり、文章が合流している。 切れ端"wa"に続く切れ目には、切れ端"ake"と"saron"が繋がり、文章が分岐している。 このような合流や切れ端があるため、図1は次のような4つの文章を表す。 haruwaakebono haruwasaronpusu natsuwaakebono natsuwasaronpusu ノートやノートのコピーは、白ヤギさんや黒ヤギさんが、食べてしまったため、復元できない部分があったり、彼らのご飯に混ざっていた関係のない書類の切れ端が混在していることもある。 太郎君の代わりに、「繋ぎ合わせたノート」と「繋ぎ合わせたノートのコピー」の2つを入力として受け取って、両方に共通して現れる文章の数を求めるプログラムを作成せよ。 Input 入力の1行目では、データセットの数 T ( 1 ≤ T ≤ 100 )が与えられる。 データセットは、「繋ぎ合せたノート」の情報と、「繋ぎ合せたノートのコピー」の情報から成る。 「繋ぎ合せたノート」の情報として、 1行目に n ( 2 ≤ n ≤ 500 )と m ( 1 ≤ m ≤ 600 )が与えられる。 n は、切れ端と切れ端を繋ぎ合せる切れ目の数である。 続く m 行で繋ぎ合せ方が与えられる。 各行は、切れ端の上端に繋がる切れ目の番号 a 、切れ端の下端につながる切れ目の番号 b 、切れ端を表す文字列 s からなる。 ただし、 0 ≤ a < b < n が満たされる。 s は長さが1以上5以下の文字列で、アルファベットの小文字のみから成る。 「繋ぎ合せたノートのコピー」も「繋ぎ合せたノート」と同じ形式で与えられる。 なお、0番目の切れ目から繋ぎ合せ情報を辿り、 n-1 番目の切れ目に到達する過程で得られる文字列を連結したもののみを文章と呼ぶものとする。 また、それぞれの繋ぎ合せの中で同じ文章が2つ以上出現することはない。 Output それぞれのデータセット毎に、答えの整数を1,000,000,007で割った余りを1行で出力せよ。 Sample Input 4 2 1 0 1 a 2 2 0 1 a 0 1 b 5 5 0 1 a 0 2 b 1 3 a 2 3 c 3 4 e 3 3 0 1 aa 0 2 bce 1 2 a 2 4 0 1 abc 0 1 def 0 1 ghi 0 1 jkl 2 4 0 1 ghi 0 1 jkl 0 1 mno 0 1 pqr 6 7 0 1 haru 0 1 natsu 1 2 wa 2 3 ake 2 4 saron 3 5 bono 4 5 pusu 5 4 0 1 haruw 1 2 aakeb 2 3 o 3 4 no Output for Sample Input 1 1 2 1
[ { "submission_id": "aoj_2288_10339676", "code_snippet": "// AOJ #2288 Oh, My Goat!\n// 2025.3.31\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\ntypedef vector<vector<pair<char,int>>> Automaton;\n\npair<Automaton, int> buildAutomaton(int n, int m, vec...
aoj_2286_cpp
Problem F: Farey Sequence slipは数字を眺めるのが好きである。 ファイルをダウンロードしているときの、残り時間を眺めているだけで、時間を潰せるほどである。 そんなslipが友人から面白い数列を教えられた。 その数列の定義は以下のとおりである。 一般項を F n と表わす。 F n とは、 n 以下の分母を持つ0以上1以下のすべての約分された分数(既約分数)を小さな順から並べたものである。 ただし、整数0、1はそれぞれ分数0/1、1/1として扱われる。 つまり F 3 は、 F 3 = (0/1, 1/3, 1/2, 2/3, 1/1) のように表される。 F 5 であれば F 5 = (0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1) と表される。 slipはこの数列にすぐにのめり込んだ。 しかしこの数列を眺めていても、なかなか特徴がつかめなかった。 特徴をつかむためにまずは各項の項数がいくつあるのか調べようと思ったが、 増え方の規則がわからず、自力で理解することを断念した。 そこで、 勝手に友人と思っているあなたに、指定した項にどれだけの項数があるのかを聞くことにした。 あなたの仕事は、与えられた項における項数を求めることである。 Input 入力は以下の形式で与えられる。 t n 1 ... n i ... n t t ( 1 ≤ t ≤ 10000 )はテストケースの数である。 n i ( 1 ≤ n i ≤ 1000000 )は与えられた項の番号である。 Output 各テストケースごとに、項数を1行に表示せよ。 Sample Input 2 3 5 Output for Sample Input 5 11
[ { "submission_id": "aoj_2286_11042230", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vll = vector<ll>;\nusing vvll = vector<vector...
aoj_2290_cpp
A: Attack the Moles ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,もぐらたたきを何回も行って,反射神経と記憶力を高めようというものである.出てくるもぐらを次々に叩き,出来るだけ多くのポイントを獲得したい. もぐらが出てくる可能性のある場所は直線状に並んでいて,基準点からの距離によって座標が定まっている.うさぎはしばらく修行を続けるうちに,もぐらの出現する場所と時間が常に一緒であることに気が付いた.うさぎは,その情報をすべて記録し,コンピュータで解析を行うことにした. もぐらを叩くには,もぐらの出現位置に手を動かした後,もぐらの出てくるタイミングにぴったり合わせてもぐらを叩かなければならない.もぐらをうまく叩けると,そのもぐらに応じてポイントを得ることが出来る.もぐらを叩く動作は一瞬で行うことが出来るが,手を移動させる速さには限界がある.うさぎはもぐらを叩くにあたって左右両方の手を用いることができる.左手と右手は独立に動かすことが可能であるが,左手は常に右手より座標が小さい位置に存在しなければならない.このような条件下で,最大でどれだけのポイントが得られるかを調べたい. Input N V X Left X Right X 1 T 1 P 1 ... X N T N P N N は出てくるもぐらの数, V は手を移動させられる最大の速さ, X Left , X Right はそれぞれ,左手,右手の初期位置の座標である. X i , T i , P i はそれぞれ, i 番目のもぐらの,出現する位置の座標,ゲーム開始から出現までの時間,叩けた際に得られるポイントである. 1 ≤ N ≤ 3,000,1 ≤ V ≤ 10,000,1 ≤ X Left < X Right ≤ 100,000,1 ≤ X 1 ≤ X 2 ≤ ... ≤ X N ≤ 100,000,1 ≤ T i ≤ 100,000,1 ≤ P i ≤ 100,000 を満たす.( X i , T i ) として同一の組は複数回現れない. Output うさぎが得られる最大のポイントを 1 行に出力せよ. Sample Input 1 3 10 150 250 100 20 123 201 10 67 202 10 45 Sample Output 1 190 Sample Input 2 1 7 20 90 55 5 73 Sample Output 2 73 Sample Input 3 10 2 1000 2000 400 300 1 600 200 1 700 800 1 700 500 1 900 600 1 1000 700 1 1300 900 1 1400 400 1 1500 1000 1 2000 100 1 Sample Output 3 10
[ { "submission_id": "aoj_2290_9372895", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\nt...
aoj_2295_cpp
F: Power of Power ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,非常に大きな数の計算を行って,計算力をつけて意識を高めようというものである.大きい数が簡単に出てくる演算として,累乗が挙げられる. N 個の非負整数 A 1 , A 2 , ..., A N がある.これらの並べ替え B 1 , B 2 , ..., B N であって, B 1 B 2 ... B N - 1 B N が最大になるものを求めたい.うさぎたちにとってはもちろん常識であるが,この計算は右上から順に行う.また,0 0 = 1 であると約束することにする. 最大条件を満たす B 1 , B 2 , ..., B N は複数通りあるかもしれない.そのような場合は,そのうちで辞書順最小の列を選ぶことにしよう. Input N A 1 ... A N 1 ≤ N ≤ 100,0 ≤ A i ≤ 1,000,000,000 を満たす. Output 出力は N 行からなる. i 行目には B i を出力せよ. Sample Input 1 4 7 5 10 6 Sample Output 1 5 6 7 10 Sample Input 2 3 0 0 1000000000 Sample Output 2 1000000000 0 0
[ { "submission_id": "aoj_2295_2389243", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(x) begin(x),end(x)\n\nusing namespace std;\nusing ld = long double;\n\nvector<int> solve2(const vector<int>& v) {\n int n = v.size();\...
aoj_2292_cpp
C: Common Palindromes ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,文字列中にある回文を探すことによって,文章から隠れたメッセージを読解する能力を高めようというものである.回文はたくさんあるかもしれないので,探すついでに個数も数えてしまいたい. 2 つの文字列 S , T が与えられるので,以下を満たす整数の組 ( i , j , k , l ) の個数を求めたい. 1 ≤ i ≤ j ≤ ( S の長さ). 1 ≤ k ≤ l ≤ ( T の長さ). S の i 文字目から j 文字目までを取り出した部分文字列は, T の k 文字目から l 文字目までを取り出した部分文字列と同一であり,さらにこれらは回文 (左から読んでも右から読んでも同じになる文字列) となっている. Input S T 文字列 S , T はともに長さが 1 以上 50,000 以下であり,アルファベット大文字からなる. Output 条件を満たす整数の組 ( i , j , k , l ) の個数を 1 行に出力せよ. Sample Input 1 ICPC CPCPC Sample Output 1 10 Sample Input 2 BABBAB ABBA Sample Output 2 14 Sample Input 3 MYON USAGI Sample Output 3 0
[ { "submission_id": "aoj_2292_9777583", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD = 10000000000000099;\nconst long long BASE = 1333;\nconst int MAXN = 5 * 100000;\n\nlong long POWER[MAXN + 1];\n\nlong long mul(long long a, long long b) {\n return ((__int128_t)a...
aoj_2294_cpp
E: Entangled with Lottery ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,あみだくじを利用して,未来を読む力をつけて運気を高めようというものである.もちろん直感に頼るだけでなく,綿密な確率計算も欠かせない. 今回考えるあみだくじは,長さ H + 1 センチメートルの縦棒 N 本からなる.うさぎは棒の上部を見て, N 本のうち 1 本を選ぶことになる.棒の下部には,左から P 本目の棒の場所にのみ「当たり」と書かれている.あみだくじにはいくつかの横棒が含まれる.横棒の配置に関して,以下の条件を考えよう. 各横棒は, a を 1 以上 H 以下の整数として,縦棒の上端から a センチメートルの高さにある. 各横棒は,隣り合った 2 本の縦棒のみを結ぶ. 同じ高さには複数の横棒は存在しない. うさぎはこれらの条件を満たすように M 本の横棒を引いた.あいにくうさぎは記憶力が良いので,当たりの位置や横棒の位置をすべて覚えてしまっており,あみだくじを楽しめない.そこで友達のねこにさらに横棒を追加してもらうことにした. まず,うさぎは当たりを狙って N 本の棒のうち 1 本を選ぶ.その後,ねこは以下の操作をちょうど K 回行う. 横棒を追加しても上で指定された条件を満たすような場所のうち 1 箇所を無作為に選ぶ.ここで,どの場所も等確率で選ばれるものとする.選んだ場所に横棒を追加する. そして,うさぎが選んだ棒が当たりであったかを判定する.棒の辿り方は通常のあみだくじと同様である (横棒に出会うたびに隣の縦棒に移る).うさぎは,可能な限り当たりとなる確率を高くしたい. Input H N P M K A 1 B 1 ... A M B M A i , B i (1 ≤ i ≤ M ) はうさぎが引いた横棒のうち i 番目のものが,縦棒の上端から A i センチメートルの高さにあり,左から B i 本目の縦棒と左から B i + 1 本目の縦棒を結ぶことを表す整数である. 2 ≤ H ≤ 500,2 ≤ N ≤ 100,1 ≤ P ≤ N ,1 ≤ M ≤ 100,1 ≤ K ≤ 100, M + K ≤ H ,1 ≤ A 1 < A 2 < ... < A M ≤ H ,1 ≤ B i ≤ N - 1 を満たす. Output 当たりとなる確率が最大となるようにうさぎが棒を選んだときの,当たりとなる確率を 1 行に出力せよ.10 -6 以下の絶対誤差が許容される. Sample Input 1 9 4 3 2 1 2 1 7 3 Sample Output 1 0.571428571 Sample Input 2 9 4 3 2 3 2 1 7 3 Sample Output 2 0.375661376
[ { "submission_id": "aoj_2294_10189840", "code_snippet": "// AOJ #2294\n// Entangled with Lottery 2025.2.4\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 1e-14;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int H, N, P, M, K;\n cin >> H >> N >> P >> M >> K;\n\...
aoj_2298_cpp
I: Starting Line ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,一直線上の道を走って,体力と判断力を養おうというものである.うさぎは今,スタートラインに立って長い長い道を見渡している. 道の途中にはいくつかニンジンが置かれており,うさぎはニンジンを食べると加速することができる.加速していないときのうさぎの走る速さは毎秒 U メートルであるが,ニンジンを食べることで,最後のニンジンを食べてから T 秒後までは速さが毎秒 V メートルとなる.また,うさぎはニンジンを K 個まで食べずに持っておくことができる.ニンジンを持っていても走る速さは変わらない. ニンジンを持ったり食べたりするのに時間はかからないとして,ゴールまでの最短所要時間を求めたい. Input N K T U V L D 1 ... D N N はニンジンの個数, L はスタートからゴールまでの距離 (メートル), D i (1 ≤ i ≤ N ) は i 番目のニンジンが置かれている場所のスタートからの距離 (メートル) である. 1 ≤ N ≤ 200,1 ≤ K ≤ N ,1 ≤ T ≤ 10,000,1 ≤ U < V ≤ 10,000,2 ≤ L ≤ 10,000,0 < D 1 < D 2 < ... < D N < L を満たす.入力の値はすべて整数である. Output 最短所要時間 (秒) を 1 行に出力せよ.10 -6 以下の絶対誤差が許容される. Sample Input 1 1 1 1 2 3 100 50 Sample Output 1 49.500000000 Sample Input 2 3 1 1 2 3 100 49 50 51 Sample Output 2 48.666666667
[ { "submission_id": "aoj_2298_7149956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int N, K, T, U, V, L;\n cin >> N >> K >> T >> U >> V >> L;\n vector<int> D(N), G(L + 1, -1), S(L + 1);\n for(int i = 0; i < N; i++){\n cin >> D[i];\n G[D[i]] = i;\n S[D[i]]++;...
aoj_2293_cpp
D: Dangerous Tower ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,積み木を丁寧に積み上げることによって,決してミスタイピングをしないほどの手先の器用さを手に入れようというものである.せっかく積み木がたくさんあるので,高い塔を作ろう. 積み木は N 個あり, i 番目 (1 ≤ i ≤ N ) の積み木は 1 × A i × B i の直方体の形をしている.長さ 1 の辺は奥行き方向に用いて,長さ A i , B i の辺を横方向と高さ方向に 1 つずつ割り当てることにした.積み木を積み上げるとき,上の段の積み木は下の段の積み木より横の長さが厳密に短くなければならない.積み木は好きな順番に使うことができ,また,使わない積み木があってもよい.このような制約下で,作ることが可能な最も高い塔を作りたい. Input N A 1 B 1 ... A N B N 1 ≤ N ≤ 1,000,1 ≤ A i , B i ≤ 1,000,000 を満たす.入力の値はすべて整数である. Output 塔の高さの最大値を 1 行に出力せよ. Sample Input 1 3 10 40 10 40 20 30 Sample Output 1 80 Sample Input 2 4 1 2 2 3 3 4 4 1 Sample Output 2 11
[ { "submission_id": "aoj_2293_10946082", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int INF = 1e9;\n\nusing pii = pair<int, int>;\n\nstruct edge {\n int to, cap, cost, rev;\n};\n\nusing edges = vector<edge>;\nusing graph = vector<edges>;\n\nvoid add_edge(graph& g, int fro...
aoj_2291_cpp
B: Brilliant Stars ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,夜空に輝くたくさんの星たちを観測して,視力と集中力を鍛えようというものである.うさぎは独自のセンスで星の特徴を記録していくことができる. うさぎは「星の観測日記」として記録した星の特徴をノートにつけていくことにした.ここで,「似ている」 2 つの星をともに記録することは避けたい.2 つの星が「似ている」かどうかは,すべてうさぎの判断で決まっている.「似ている」かどうかの関係はあまり複雑ではなく,以下の条件を満たす. 条件: 異なる 4 つの星 A, B, C, D があり,A と B,B と C,C と D がそれぞれ「似ている」とする.このとき,A と C が「似ている」か,B と D が「似ている」か,あるいはその両方が成り立つ. うさぎは「似ている」 2 つの星を記録しないようになるべくたくさんの星を記録したい.また,そのようにする方法が何通りあるかも知りたい. Input N M X 1 Y 1 ... X M Y M N は星の個数, M は「似ている」 2 つの星の組の個数である.星は 1 以上 N 以下の整数の番号により表され, X i , Y i (1 ≤ i ≤ M ) は星 X i と星 Y i が「似ている」ことを表す. 1 ≤ N ≤ 100,000,0 ≤ M ≤ 200,000,1 ≤ X i < Y i ≤ N を満たす.( X i , Y i ) として同一の組は複数回現れない.「似ている」かどうかの関係は,問題文で指定された条件を満たす. Output 1 行目に,「似ている」 2 つの星を記録しないように記録できる星の個数の最大値を,2 行目に,その最大値を実現できる星の組み合わせの個数 (記録する順番のみを変えたものは同じと考える) を 1,000,000,009 で割った余りを出力せよ. Sample Input 1 4 3 1 2 1 3 1 4 Sample Output 1 3 1 Sample Input 2 11 5 1 2 3 4 5 6 7 8 9 10 Sample Output 2 6 32 Sample Input 3 9 14 1 2 1 3 2 3 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 5 6 7 8 8 9 Sample Output 3 4 6
[ { "submission_id": "aoj_2291_10866253", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1ll << 60;\n#define REP(i, n) for(ll i=0; i <ll(n); i++)\ntemplate <class T> using V = vector<T>;\ntemplate <class A, class B>\nbool chmax(A &a, B b) { return a < b ...
aoj_2296_cpp
G: Quest of Merchant ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,街を駆け回り交易を行うことで,商売の力を手にしようというものである. 今後の修行のためにも,できるだけ多くの資金を稼ぎたい. この世界は,東西南北方向の道路が等間隔に並んでいて,碁盤状になっている.唯一存在する市場の位置を (0, 0),街には x 座標, y 座標が定まっている (座標が整数値の点が交差点に対応する).うさぎは道路に沿ってのみ移動することができ,隣接する交差点間を移動するのに 1 分を要する.いくつかの交差点には街がある.交易では,街で商品を買い,市場にて販売することによって価格の差の分の利益を得る. うさぎの初期資金は十分にあり,お金が足りないから商品が購入できない,といったことはない.だが,それぞれの商品には重さがあり,うさぎは重さの合計が W までの商品しか同時に持ち運ぶことができない.よって,街に商品を仕入れに行ったり市場に戻ったりを繰り返すことになる.場合によっては,複数の街で商品を購入してから市場に戻ることになるかもしれない. 街での商品の購入や,市場での商品の販売は一瞬で行えるものとする.また,街にある商品が品切れになってしまうことは有り得ず,無限に購入することが可能である. うさぎは,今回売買を行おうとしている各商品の名前,1 個あたりの重さおよび販売価格と,各街の x 座標, y 座標および販売商品の名前と価格を,データにまとめた.うさぎは今,市場がある (0, 0) の位置にいる.プログラムを使って,制限時間 T 分の間にどれだけ稼ぐことが可能か調べたい. Input N M W T S 1 V 1 P 1 ... S M V M P M L 1 X 1 Y 1 R 1,1 Q 1,1 ... R 1, L 1 Q 1, L 1 ... L N X N Y N R N ,1 Q N ,1 ... R N , L N Q N , L N N は街の数, M は商品の種類数である. S i , V i , P i (1 ≤ i ≤ M ) はそれぞれ, i 番目の商品の名前,1 個あたりの重さ,1 個あたりの販売価格である. 街は 1 以上 N 以下の整数で表される. L j , X j , Y j (1 ≤ j ≤ N ) はそれぞれ,街 j で売られている商品の種類数,街 j の x 座標, y 座標である. R j , k , Q j , k (1 ≤ j ≤ N ,1 ≤ k ≤ L j ) はそれぞれ,街 j で売られている k 番目の商品の名前,価格である. 1 ≤ N ≤ 7,1 ≤ M ≤ 7,1 ≤ W ≤ 10,000,1 ≤ T ≤ 10,000,1 ≤ V i ≤ W ,1 ≤ P i ≤ 10,000,1 ≤ L j ≤ M ,-10,000 ≤ X j ≤ 10,000,-10,000 ≤ Y j ≤ 10,000,1 ≤ Q j , k ≤ 10,000 を満たす.商品の名前はアルファベット小文字からなる長さが 1 以上 7 以下の文字列である.その他の値はすべて整数である. S i はすべて異なる.( X j , Y j ) として同一の組は複数回現れず,( X j , Y j ) = (0, 0) となることはない. 各 j に対して R j , k はすべて異なり,各 R j , k はいずれかの S i に一致する. Output うさぎが得られる最大の利益を 1 行に出力せよ. Sample Input 1 2 2 100 20 alfalfa 10 10 carrot 5 10 1 1 6 carrot 4 1 -3 0 alfalfa 5 Sample Output 1 170 Sample Input 2 2 3 100 20 vim 10 10 emacs 5 10 vstudio 65 100 2 1 6 emacs 4 vstudio 13 3 -3 0 vim 5 emacs 9 vstudio 62 Sample Output 2 183
[ { "submission_id": "aoj_2296_10366916", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #includ...
aoj_2297_cpp
H: Rectangular Stamps ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,絵を描くことによって,創造力を高めようというものである.四角いスタンプを用いて,上手く模様を描こう. 大小さまざまなスタンプを使い,4 × 4 のマス目の紙に指定された赤・緑・青の通りの絵を完成させたい.スタンプは長方形であり,マス目にぴったり合わせて使う.スタンプの縦と横を入れ替えることはできない. 紙は最初色が塗られていない状態にある.紙にスタンプを押すと,押された部分がスタンプの色に変わり,下に隠れた色は全く見えなくなる.スタンプの色は付けるインクにより決定されるので,どのスタンプでも,好きな色を選ぶことが可能である.スタンプは紙から一部がはみ出た状態で押すことも可能であり,はみ出た部分は無視される. 1 つのスタンプを複数回使うことは可能である.同じスタンプを別の色に対して使ってもよい.スタンプを押すのはやや神経を使う作業なので,出来るだけスタンプを押す回数を少なくしたい. Input N H 1 W 1 ... H N W N C 1,1 C 1,2 C 1,3 C 1,4 C 2,1 C 2,2 C 2,3 C 2,4 C 3,1 C 3,2 C 3,3 C 3,4 C 4,1 C 4,2 C 4,3 C 4,4 N はスタンプの個数, H i , W i (1 ≤ i ≤ N ) はそれぞれ, i 番目のスタンプの縦の長さ,横の長さを表す整数である. C i , j (1 ≤ i ≤ 4,1 ≤ j ≤ 4) は,上から i 行目,左から j 列目のマスについて指定された絵の色を表す文字である.赤は R ,緑は G ,青は B で表される. 1 ≤ N ≤ 16,1 ≤ H i ≤ 4,1 ≤ W i ≤ 4 を満たす.( H i , W i ) として同一の組は複数回現れない. Output 絵を完成させるためにスタンプを押さなければならない最小の回数を 1 行に出力せよ. Sample Input 1 2 4 4 1 1 RRRR RRGR RBRR RRRR Sample Output 1 3 Sample Input 2 1 2 3 RRGG BRGG BRRR BRRR Sample Output 2 5
[ { "submission_id": "aoj_2297_9995285", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 9...
aoj_2299_cpp
J: Tiles are Colorful ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,流行りのパズルをすばやく解いて,瞬発力を鍛えようというものである.今日挑戦するのは,色とりどりのタイルが並んでいてそれらを上手く消していくパズルだ 初期状態では,グリッド上のいくつかのマスにタイルが置かれている.各タイルには色がついている.プレイヤーはゲーム開始後,以下の手順で示される操作を何回も行うことができる. タイルが置かれていないマスを 1 つ選択し,そのマスを叩く. 叩いたマスから上に順に辿っていき,タイルが置かれているマスに至ったところでそのタイルに着目する.タイルが置かれているマスがないまま盤面の端に辿り着いたら何にも着目しない. 同様の操作を叩いたマスから下・左・右方向に対して行う.最大 4 枚のタイルが着目されることになる. 着目したタイルの中で同じ色のものがあれば,それらのタイルを盤面から取り除く.同じ色のタイルの組が 2 組あれば,それら両方を取り除く. タイルを取り除いた枚数と同じ値の得点が入る. 着目をやめる. たとえば,以下のような状況を考えよう.タイルが置かれていないマスはピリオドで,タイルの色はアルファベット大文字で表されている. ..A....... .......B.. .......... ..B....... ..A.CC.... ここで上から 2 行目,左から 3 列目のマスを叩く操作を考える.着目することになるタイルは A , B , B の 3 枚であるから, B の 2 枚が消えて盤面は以下のようになり,2 点を得る. ..A....... .......... .......... .......... ..A.CC.... このパズルはゆっくりしていると時間切れになってしまい,盤面の一部が見えなくなりどのくらい修行が足りなかったのかがわからなくなってしまう. 各色のタイルは 2 枚ずつ置かれているが,それらをすべて消せるとは限らないので,予めプログラムに得点の最大値を計算させておきたい. Input M N C 1,1 C 1,2 ... C 1, N C 2,1 C 2,2 ... C 2, N ... C M ,1 C M ,2 ... C M , N 整数 M , N は盤が 縦 M × 横 N のマス目であることを表す. C i , j はアルファベット大文字またはピリオド ( . ) であり,上から i 行目,左から j 列目のマスについて,アルファベット大文字の場合は置かれているタイルの色を表し,ピリオドの場合はこのマスにタイルが置かれていないことを表す. 1 ≤ M ≤ 500,1 ≤ N ≤ 500 を満たす.各アルファベット大文字は入力中に 0 個または 2 個現れる. Output 得点の最大値を 1 行に出力せよ. Sample Input 1 5 10 ..A....... .......B.. .......... ..B....... ..A.CC.... Sample Output 1 4 Sample Input 2 3 3 ABC D.D CBA Sample Output 2 4 Sample Input 3 5 7 NUTUBOR QT.SZRQ SANAGIP LMDGZBM KLKIODP Sample Output 3 34
[ { "submission_id": "aoj_2299_9394837", "code_snippet": "#include <iostream>\n#include <vector>\n#include <stack>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <string>\n#include <cctype>\n#include <cmath>\nusing namespace std;\nusing ll = long long;\nconst long long INF = (1LL<<60);\nconst...
aoj_2300_cpp
Calender Colors Taro is a member of a programming contest circle. In this circle, the members manage their schedules in the system called Great Web Calender . Taro has just added some of his friends to his calendar so that he can browse their schedule on his calendar. Then he noticed that the system currently displays all the schedules in only one color, mixing the schedules for all his friends. This is difficult to manage because it's hard to tell whose schedule each entry is. But actually this calender system has the feature to change the color of schedule entries, based on the person who has the entries. So Taro wants to use that feature to distinguish their plans by color. Given the colors Taro can use and the number of members, your task is to calculate the subset of colors to color all schedule entries. The colors are given by "Lab color space". In Lab color space, the distance between two colors is defined by the square sum of the difference of each element. Taro has to pick up the subset of colors that maximizes the sum of distances of all color pairs in the set. Input The input is like the following style. N M L_{0} a_{0} b_{0} L_{1} a_{1} b_{1} ... L_{N-1} a_{N-1} b_{N-1} The first line contains two integers N and M ( 0 \leq M \leq N \leq 20 ), where N is the number of colors in the input, and M is the number of friends Taro wants to select the colors for. Each of the following N lines contains three decimal integers L ( 0.0 \leq L \leq 100.0 ), a ( -134.0 \leq a \leq 220.0 ) and b ( -140.0 \leq b \leq 122.0 ) which represents a single color in Lab color space. Output Output the maximal of the total distance. The output should not contain an error greater than 10^{-5} . Sample Input 1 3 2 0 0 0 10 10 10 100 100 100 Output for the Sample Input 1 30000.00000000000000000000 Sample Input 2 5 3 12.0 15.0 9.0 10.0 -3.0 2.2 3.5 6.8 9.0 2.1 4.4 5.9 1.2 4.0 -5.4 Output for the Sample Input 2 1003.44000000000005456968 Sample Input 3 2 1 1.0 1.0 1.0 0.0 0.0 0.0 Output for the Sample Input 3 0.00000000000000000000
[ { "submission_id": "aoj_2300_11047158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<int, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = ...
aoj_2301_cpp
Sleeping Time Miki is a high school student. She has a part time job, so she cannot take enough sleep on weekdays. She wants to take good sleep on holidays, but she doesn't know the best length of sleeping time for her. She is now trying to figure that out with the following algorithm: Begin with the numbers K , R and L . She tries to sleep for H=(R+L)/2 hours. If she feels the time is longer than or equal to the optimal length, then update L with H . Otherwise, update R with H . After repeating step 2 and 3 for K nights, she decides her optimal sleeping time to be T' = (R+L)/2 . If her feeling is always correct, the steps described above should give her a very accurate optimal sleeping time. But unfortunately, she makes mistake in step 3 with the probability P . Assume you know the optimal sleeping time T for Miki. You have to calculate the probability PP that the absolute difference of T' and T is smaller or equal to E . It is guaranteed that the answer remains unaffected by the change of E in 10^{-10} . Input The input follows the format shown below K R L P E T Where the integers 0 \leq K \leq 30 , 0 \leq R \leq L \leq 12 are the parameters for the algorithm described above. The decimal numbers on the following three lines of the input gives the parameters for the estimation. You can assume 0 \leq P \leq 1 , 0 \leq E \leq 12 , 0 \leq T \leq 12 . Output Output PP in one line. The output should not contain an error greater than 10^{-5} . Sample Input 1 3 0 2 0.10000000000 0.50000000000 1.00000000000 Output for the Sample Input 1 0.900000 Sample Input 2 3 0 2 0.10000000000 0.37499999977 1.00000000000 Output for the Sample Input 2 0.810000 Sample Input 3 3 0 2 0.10000000000 0.00000100000 0.37500000000 Output for the Sample Input 3 0.729000 Sample Input 4 3 0 2 0.20000000000 0.00000100000 0.37500000000 Output for the Sample Input 4 0.512000
[ { "submission_id": "aoj_2301_10958607", "code_snippet": "#include<bits/stdc++.h>\ntemplate <class T>\ninline bool rd(T &ret) {\n char c; int sgn;\n if(c=getchar(),c==EOF) return 0;\n while(c!='-'&&(c<'0'||c>'9')) c=getchar();\n sgn=(c=='-')?-1:1;\n ret=(c=='-')?0:(c-'0');\n while(c=getchar...
aoj_2302_cpp
On or Off Saving electricity is very important! You are in the office represented as R \times C grid that consists of walls and rooms. It is guaranteed that, for any pair of rooms in the office, there exists exactly one route between the two rooms. It takes 1 unit of time for you to move to the next room (that is, the grid adjacent to the current room). Rooms are so dark that you need to switch on a light when you enter a room. When you leave the room, you can either leave the light on, or of course you can switch off the light. Each room keeps consuming electric power while the light is on. Today you have a lot of tasks across the office. Tasks are given as a list of coordinates, and they need to be done in the specified order. To save electricity, you want to finish all the tasks with the minimal amount of electric power. The problem is not so easy though, because you will consume electricity not only when light is on, but also when you switch on/off the light. Luckily, you know the cost of power consumption per unit time and also the cost to switch on/off the light for all the rooms in the office. Besides, you are so smart that you don't need any time to do the tasks themselves. So please figure out the optimal strategy to minimize the amount of electric power consumed. After you finished all the tasks, please DO NOT leave the light on at any room. It's obviously wasting! Input The first line of the input contains three positive integers R ( 0 \lt R \leq 50 ), C ( 0 \lt C \leq 50 ) and M ( 2 \leq M \leq 1000 ). The following R lines, which contain C characters each, describe the layout of the office. '.' describes a room and '#' describes a wall. This is followed by three matrices with R rows, C columns each. Every elements of the matrices are positive integers. The (r, c) element in the first matrix describes the power consumption per unit of time for the room at the coordinate (r, c) . The (r, c) element in the second matrix and the third matrix describe the cost to turn on the light and the cost to turn off the light, respectively, in the room at the coordinate (r, c) . Each of the last M lines contains two positive integers, which describe the coodinates of the room for you to do the task. Note that you cannot do the i -th task if any of the j -th task ( 0 \leq j \leq i ) is left undone. Output Print one integer that describes the minimal amount of electric power consumed when you finished all the tasks. Sample Input 1 1 3 2 ... 1 1 1 1 2 1 1 1 1 0 0 0 2 Output for the Sample Input 1 7 Sample Input 2 3 3 5 ... .## ..# 1 1 1 1 0 0 1 1 0 3 3 3 3 0 0 3 3 0 5 4 5 4 0 0 5 4 0 1 0 2 1 0 2 2 0 0 0 Output for the Sample Input 2 77 Sample Input 3 5 5 10 #.### #.... ###.# ..#.# #.... 0 12 0 0 0 0 4 3 2 10 0 0 0 99 0 11 13 0 2 0 0 1 1 2 1 0 4 0 0 0 0 13 8 2 4 0 0 0 16 0 1 1 0 2 0 0 2 3 1 99 0 2 0 0 0 0 12 2 12 2 0 0 0 3 0 4 14 0 16 0 0 2 14 2 90 0 1 3 0 4 4 1 4 1 1 4 4 1 1 4 3 3 0 1 4 Output for the Sample Input 3 777
[ { "submission_id": "aoj_2302_10858089", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n#include <queue>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nconst int dx[]={1,0,0,-1};\nconst int dy[]={0,1,-1,0};\n\nconst int N = 55;\nqueue<in...
aoj_2306_cpp
Rabbit Party A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0. When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests. To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party. Input The first line of the input contains two integers, n and m ( 1 \leq n \leq 100 , 0 \leq m \leq 100 ). The rabbits are numbered from 1 to n . Each of the following m lines has three integers, u , v and f . u and v ( 1 \leq u, v \leq n , u \neq v , 1 \leq f \leq 1,000,000 ) stands for the rabbits' number, and f stands for their friendliness. You may assume that the friendliness of a pair of rabbits will be given at most once. Output Output the maximal possible satisfaction score of the party in a line. Sample Input 1 3 3 1 2 3 2 3 1 3 1 2 Output for the Sample Input 1 6 Sample Input 2 2 1 1 2 5 Output for the Sample Input 2 10 Sample Input 3 1 0 Output for the Sample Input 3 0 Sample Input 4 4 5 1 2 4 1 3 3 2 3 7 2 4 5 3 4 6 Output for the Sample Input 4 16
[ { "submission_id": "aoj_2306_10690753", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, M, ST, gans, ans, hg1, ohg1;\nint jiyou[120][120], val[200], oval[300];\nbool used[1000];\n\nint getans()\n{\n\tint i, res = 0, nc = 0;\n\tf...
aoj_2303_cpp
Marathon Match N people run a marathon. There are M resting places on the way. For each resting place, the i -th runner takes a break with probability P_i percent. When the i -th runner takes a break, he gets rest for T_i time. The i -th runner runs at constant speed V_i , and the distance of the marathon is L . You are requested to compute the probability for each runner to win the first place. If a runner arrives at the goal with another person at the same time, they are not considered to win the first place. Input A dataset is given in the following format: N M L P_1 T_1 V_1 P_2 T_2 V_2 ... P_N T_N V_N The first line of a dataset contains three integers N ( 1 \leq N \leq 100 ), M ( 0 \leq M \leq 50 ) and L ( 1 \leq L \leq 100,000 ). N is the number of runners. M is the number of resting places. L is the distance of the marathon. Each of the following N lines contains three integers P_i ( 0 \leq P_i \leq 100 ), T_i ( 0 \leq T_i \leq 100 ) and V_i ( 0 \leq V_i \leq 100 ) describing the i -th runner. P_i is the probability to take a break. T_i is the time of resting. V_i is the speed. Output For each runner, you should answer the probability of winning. The i -th line in the output should be the probability that the i -th runner wins the marathon. Each number in the output should not contain an error greater than 10^{-5} . Sample Input 1 2 2 50 30 50 1 30 50 2 Output for the Sample Input 1 0.28770000 0.71230000 Sample Input 2 2 1 100 100 100 10 0 100 1 Output for the Sample Input 2 0.00000000 1.00000000 Sample Input 3 3 1 100 50 1 1 50 1 1 50 1 1 Output for the Sample Input 3 0.12500000 0.12500000 0.12500000 Sample Input 4 2 2 50 30 0 1 30 50 2 Output for the Sample Input 4 0.51000000 0.49000000
[ { "submission_id": "aoj_2303_10853140", "code_snippet": "#include<bits/stdc++.h>\ntemplate <class T>\ninline bool rd(T &ret) {\n char c; int sgn;\n if(c=getchar(),c==EOF) return 0;\n while(c!='-'&&(c<'0'||c>'9')) c=getchar();\n sgn=(c=='-')?-1:1;\n ret=(c=='-')?0:(c-'0');\n while(c=getchar...
aoj_2307_cpp
Palindrome Generator A palidrome craftsperson starts to work in the early morning, with the clear air allowing him to polish up his palindromes. On this morning, he is making his pieces to submit to the International Contest for Palindrome Craftspeople. By the way, in order to make palindromes, he uses a special dictionary which contains a set of words and a set of ordered pairs of the words. Any words and any ordered pairs of consecutive words in his palindromes must appear in the dictionary. We already have his dictionary, so let's estimate how long a palindrome he can make. Input The first line in the data set consists of two integers N ( 1 \leq N \leq 100 ) and M ( 0 \leq M \leq 1\,000 ). N describes the number of words in the dictionary and M describes the number of ordered pairs of words. The following N lines describe the words he can use. The i -th line ( 1 -based) contains the word i , which consists of only lower-case letters and whose length is between 1 and 10 , inclusive. The following M lines describe the ordered pairs of consecutive words he can use. The j -th line ( 1 -based) contains two integers X_j and Y_j ( 1 \leq X_j, Y_j \leq N ). X_j describes the ( 1 -based) index of the former word in the palindrome and Y_j describes that of the latter word. Output Print the maximal length of the possible palindrome in a line. If he can make no palidromes, print "0". If he can make arbitrary long palindromes, print "-1". Sample Input 1 2 2 ab ba 1 1 1 2 Output for the Sample Input 1 4 Sample Input 2 2 2 ab ba 1 1 2 2 Output for the Sample Input 2 0 Sample Input 3 2 2 ab a 1 1 1 2 Output for the Sample Input 3 -1
[ { "submission_id": "aoj_2307_10836481", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\n#include <map>\n#include <cmath>\n#include <sstream>\n#include <iomanip>\n#include <queue>\n#include <ctime>\nusing namespace std;\nte...
aoj_2311_cpp
問題文 お菓子の魔女 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
[ { "submission_id": "aoj_2311_3524913", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <bitset>\n#include <map>\n#include <tuple>\n#include <unordered_set>\n#include <unordered_map>\n#include <list...
aoj_2305_cpp
Beautiful Currency KM country has N kinds of coins and each coin has its value a_i . The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beautiful by changing the values of some (possibly no) coins. A currency system is called beautiful if each coin has an integer value and the ( i +1)-th smallest value is divisible by the i -th smallest value for all i ( 1 \leq i \leq N-1 ). For example, the set {1, 5, 10, 50, 100, 500} is considered as a beautiful system, while the set {1, 5, 10, 25, 50, 100} is NOT, because 25 is not divisible by 10 . Since changing the currency system may confuse citizens, the king, Kita_masa, wants to minimize the maximum value of the confusion ratios. Here, the confusion ratio for the change in the i -th coin is defined as |a_i - b_i| / a_i , where a_i and b_i is the value of i -th coin before and after the structure changes, respectively. Note that Kita_masa can change the value of each existing coin, but he cannot introduce new coins nor eliminate existing coins. After the modification, the values of two or more coins may coincide. Input Each dataset contains two lines. The first line contains a single integer, N , and the second line contains N integers, {a_i} . You may assume the following constraints: 1 \leq N \leq 20 1 \leq a_1 \lt a_2 \lt... \lt a_N \lt 10^5 Output Output one number that represents the minimum of the maximum value of the confusion ratios. The value may be printed with an arbitrary number of decimal digits, but may not contain an absolute error greater than or equal to 10^{-8} . Sample Input 1 3 6 11 12 Output for the Sample Input 1 0.090909090909 Sample Input 2 3 6 11 24 Output for the Sample Input 2 0.090909090909 Sample Input 3 3 6 11 30 Output for the Sample Input 3 0.166666666667
[ { "submission_id": "aoj_2305_10867796", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\ntypedef long long ll;\nconst double inf = 1e15;\nconst double eps = 1e-8;\nconst int N = 25;\nconst int M = 200005;\nint a[N], n;\ndouble ans, dp[N][...
aoj_2309_cpp
Vector Compression You are recording a result of a secret experiment, which consists of a large set of N -dimensional vectors. Since the result may become very large, you are thinking of compressing it. Fortunately you already have a good compression method for vectors with small absolute values, all you have to do is to preprocess the vectors and make them small. You can record the set of vectors in any order you like. Let's assume you process them in the order v_1 , v_2 ,..., v_M . Each vector v_i is recorded either as is , or as a difference vector. When it is recorded as a difference, you can arbitrarily pick up an already chosen vector v_j (j<i) and a real value r . Then the actual vector value recorded is (v_i - r v_j) . The values of r and j do not affect the compression ratio so much, so you don't have to care about them. Given a set of vectors, your task is to write a program that calculates the minimum sum of the squared length of the recorded vectors. Input The input is like the following style. N M v_{1,1} v_{1,2} ... v_{1,N} ... v_{M,1} v_{M,2} ... v_{M,N} The first line contains two integers N and M ( 1 \leq N, M \leq 100 ), where N is the dimension of each vector, and M is the number of the vectors. Each of the following M lines contains N floating point values v_{i,j} ( -1.0 \leq v_{i,j} \leq 1.0 ) which represents the j -th element value of the i -th vector. Output Output the minimum sum of the squared length of the recorded vectors. The output should not contain an absolute error greater than 10^{-6} . Sample Input 1 2 3 1.0 1.0 -1.0 0.0 0.5 0.5 Output for the Sample Input 1 1.0 Sample Input 2 1 1 1.0 Output for the Sample Input 2 1.0 Sample Input 3 4 3 1.0 1.0 0.0 0.0 -1.0 0.0 -1.0 0.0 0.5 0.5 0.5 0.5 Output for the Sample Input 3 3.0
[ { "submission_id": "aoj_2309_9813617", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)...
aoj_2310_cpp
〜魔女からこの世界を守るため キュゥべえ と共に戦う 5 人の魔法少女達の物語〜 問題 A 問題文 薔薇園の魔女 GERTRUD は幅 W メートル,高さ H メートルの長方形の庭でバラを育てている.庭は一辺 1 メートルの正方形で区切られており, W\times H のブロックに分けられている.各ブロックはバラが育てられているか育てられていないかのどちらかである. ただ驚くべきことに,この庭で育てられているバラはブロックを越えて絡み合い 1 つの生命体となっている.この生命体は辺を共有しているブロック間を越えて絡み合うことができ,どこかある 1 点を共有していれば同じ生命体となる. 巴マミ は庭の左下の角から直線に必殺技「ティロ・フィナーレ」で巨大な銃を撃ち,その弾丸が通る直線上でその生命体を分割した時,生命体が出来る限り多くの分割数に分断されるようにすることによりダメージを与えたい.ティロ・フィナーレは最大でこの生物を何分割できるか求めよ. 入力形式 入力は以下の形式で与えられる. H\ W\\ f_{11} f_{12} ... f_{1W}\\ f_{21} f_{22} ... f_{2W}\\ ...\\ f_{H1} f_{H2} ... f_{HW}\\ H は庭の高さ, W は庭の幅を表す. f_{ij} は庭の各ブロックを表す文字であり,上から i 行目,左から j 列目のブロックの状態を表す.ブロックにバラが育てられている場合は f_{ij} は '#' となり,そうでない場合は '.' となる. 出力形式 生命体を直線で分割する時に,最大でいくつの部分に分割できるかを1行で出力せよ. 制約 1 ≤ H ≤ 600 1 ≤ W ≤ 600 f_{ij} は '#' , '.' のいずれかである. 少なくとも 1 つは f_{ij} = '#' となるマス f_{ij} が存在する. 2 つのブロックが辺を共有するとき,それらは 隣接している と呼ぶことにする.任意の 2 つの異なる '#' のブロック f_A, f_B に対して, f_A から隣接している '#' のブロックを辿っていって f_B に着くことができる.(すなわち,薔薇のブロックは連結である.) 入力中で 1 行目の行, H 行目の行, 1 列目の列,または W 列目の列にあるブロックを 盤面の端のブロック と呼ぶことにする.任意の '.' のブロック f_C に対して, f_C から隣接している '.' のブロックを辿っていって,ある '.' の盤面の端のブロックに着くことができる.(すなわち,入力に"空洞"のブロックは存在しない.) 入出力例 入力例 1 3 5 ##..# #..## ####. 出力例1 4 このとき以下のように 4 つの部分に分割することができる. 入力例 2 3 3 #.# ### #.# 出力例 2 3 入力例 3 10 9 ......... ......... ####..... #..#.##.. #..#..#.. #..##.### #..##...# ##..##.## ###.#..#. ###.####. 出力例 3 6 入力例 4 10 11 ########### #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# 出力例 4 7 入力例 5 25 38 ...........#...............#.......... ...........###..........####.......... ...........#####.......####........... ............###############........... ............###############........... ............##############............ .............#############............ ............###############........... ...........#################.......... .......#...#################...#...... .......##.##################..##...... ........####################.##....... ..........####################........ .........#####..########..#####....... .......######....#####....######...... ......########...#####..########.#.... ....#######..#...#####..#..########... ..#########.....#######......#######.. ...#######......#######........###.... ..####.........#########........###... ...............#########.............. ..............##########.............. ..............##########.............. ...............########............... ...............########............... 出力例 5 8 Problem Setter: Flat35
[ { "submission_id": "aoj_2310_10851592", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>&...
aoj_2308_cpp
White Bird Angry Birds is a mobile game of a big craze all over the world. You were convinced that it was a waste of time to play the game, so you decided to create an automatic solver. You are describing a routine that optimizes the white bird's strategy to defeat a pig (enemy) by hitting an egg bomb. The white bird follows a parabolic trajectory from the initial position, and it can vertically drop egg bombs on the way. In order to make it easy to solve, the following conditions hold for the stages. N obstacles are put on the stage. Each obstacle is a rectangle whose sides are parallel to the coordinate axes. The pig is put on the point (X, Y) . You can launch the white bird in any direction at an initial velocity V from the origin. If the white bird collides with an obstacle, it becomes unable to drop egg bombs. If the egg bomb collides with an obstacle, the egg bomb is vanished. The acceleration of gravity is 9.8 {\rm m/s^2} . Gravity exerts a force on the objects in the decreasing direction of y -coordinate. Input A dataset follows the format shown below: N V X Y L_1 B_1 R_1 T_1 ... L_N B_N R_N T_N All inputs are integer. N : the number of obstacles V : the initial speed of the white bird X , Y : the position of the pig ( 0 \leq N \leq 50 , 0 \leq V \leq 50 , 0 \leq X, Y \leq 300 , X \neq 0 ) for 1 \leq i \leq N, L_i : the x-coordinate of the left side of the i -th obstacle B_i : the y-coordinate of the bottom side of the i -th obstacle R_i : the x-coordinate of the right side of the i -th obstacle T_i : the y-coordinate of the top side of the i -th obstacle ( 0 \leq L_i, B_i, R_i, T_i \leq 300 ) It is guaranteed that the answer remains unaffected by a change of L_i , B_i , R_i and T_i in 10^{-6} . Output Yes/No You should answer whether the white bird can drop an egg bomb toward the pig. Sample Input 1 0 7 3 1 Output for the Sample Input 1 Yes Sample Input 2 1 7 3 1 1 1 2 2 Output for the Sample Input 2 No Sample Input 3 1 7 2 2 0 1 1 2 Output for the Sample Input 3 No
[ { "submission_id": "aoj_2308_5846707", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(...
aoj_2315_cpp
問題文 影の魔女 ELSAMARIA はすべてのものに対して祈りを続ける魔女である.影に捕まったあらゆる命はこの魔女の中に引きずり込まれてしまう. 影は触手のように動き 美樹さやか を引きずり込もうと狙っている. 美樹さやか はこの魔女に捕まることなく攻撃するために,魔女にとって予測の難しいでたらめな動きをすることを考えた.具体的には,まず頭の中で 1 から N までの整数を一様な確率で K 個作り,その合計値の距離だけ今の位置から魔女の方向へジャンプする,ということを繰り返すのである.そして魔女の位置に到達した時に攻撃するのである. さやか と魔女はある一直線上にいるとし,最初魔女は座標 0 , さやか は座標 S の位置にいるものとする.また魔女は動かないものとする.何度ジャンプした後に魔女に最初の攻撃を与えることができるだろうか.期待値を求めよ. 入力形式 入力は以下の形式で与えられる. S\ N\ K S は最初に さやか がいる座標である. N は さやか が頭の中で作る整数の範囲の上限, K は さやか が 1 回のジャンプに用いる整数の個数である. 出力形式 さやか が魔女の位置に到達するために必要なジャンプの回数の期待値を 1 行に出力せよ.もし何度ジャンプしても魔女の位置へ到達できない,もしくは期待値が有限値に収まらない場合は -1 と出力せよ. 小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 -10 9 ≤ S ≤ 10 9 1 ≤ N ≤ 10 1 ≤ K ≤ 10 入力値は全て整数である. 入出力例 入力例 1 6 6 1 出力例1 6.00000000 入力例 2 -100 7 5 出力例 2 43.60343043 入力例 3 756434182 9 10 出力例 3 15128783.80867078 Problem Setter: Flat35
[ { "submission_id": "aoj_2315_8371789", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<long double> > matmul(int N, const vector<vector<long double> >& mat1, const vector<vector<long double> >& mat2) {\n\tvector<vector<long double> > res(N, vector<long double>(N));\n\tfor (i...
aoj_2313_cpp
問題文 ハコの魔女 H.N.ELLY はとある動画サイトの熱狂的なファンである.ハコの魔女の強さはその時々のその動画サイトからの転送速度に応じて変化するのではないかと 美樹さやか は考えた.そこで動画サイトからハコの魔女の持つコンピュータまでの過去の転送速度 (=単位時間あたりのデータの転送量) を調べたい. 初期のインターネットのネットワークの構造とそれ以降のネットワークの構造の変化を表すクエリが与えられるので,各変化について変化した直後の動画サイトからハコの魔女の持つコンピュータまでの転送速度を求めよ. インターネットは複数の転送装置からなるものと見なし,各々をつなぐ回線は双方向に情報を送ることができ,その転送速度の最大は 1 であるとする.また,ネットワークは常に動画サイトからハコの魔女へ送られるデータの転送速度を最大化するように運ぶものとする. 入力形式 入力は以下の形式で与えられる. N\ E\ Q\\ F_1\ T_1\\ F_2\ T_2\\ …\\ F_E\ T_E\\ M_1\ A_1\ B_1\\ M_2\ A_2\ B_2\\ ...\\ M_Q\ A_Q\ B_Q\\ N は動画サイトとハコの魔女の持つコンピュータを含めた転送装置の数である.番号が 1 である転送装置は動画サイトであり,番号が N である転送装置はハコの魔女の持つコンピュータである. E は初期状態で接続されている転送装置の組み合わせの数であり, Q はインターネットが変化した回数である.初期のインターネットは F_i と T_i が転送速度 1 で双方向に接続されていることを表す. ネットワークの変化は時系列順に与えられ, j 番目の変化は M_j が 1 であれば A_j,\ B_j 間がつながれたことを表し, M_j が 2 であれば A_j,\ B_j 間の接続が切れたことを表す. 出力形式 各変化の直後における,動画サイトからハコの魔女の持つコンピュータまでの転送速度を出力せよ. 制約 2 \leq N \leq 500 0 \leq E \leq 20,000 1 \leq Q \leq 1,000 1 \leq F_i \leq N,\ 1 \leq T_i \leq N,\ F_i \neq T_i \ (1 \leq i \leq E) すべての \{F_i, T_i\} のペアは異なる. 1 \leq M_j \leq 2,\ 1 \leq A_j \leq N\ ,\ 1 \leq B_j \leq N\ ,\ A_j \neq B_j (1 \leq j \leq Q) ネットワークのどの段階においても次のことが成り立つ : どの 2 つの転送装置の間も高々 1 つの回線でしか繋がれていない. 2 つの転送装置の間が既に繋がれている状態でそれらの間を接続するようなクエリが来たり, 2 つの転送装置の間が回線で繋がれていない状態でそれらの間の接続を切るようなクエリが来ることはない. 入出力例 入力例 1 2 1 2 1 2 2 1 2 1 2 1 出力例1 0 1 最初の変化の後,ハコの魔女のコンピュータと動画サイトは繋がらなくなっているので転送速度は 0 である. 2 度目の変化の後は両者は直接繋がっており,転送速度は 1 になる. 入力例 2 3 0 4 1 2 3 1 2 1 1 3 1 2 2 3 出力例 2 0 1 2 1 入力例 3 6 4 8 1 2 1 3 4 6 5 6 1 2 4 1 3 5 1 2 5 1 3 4 2 2 4 2 3 5 1 2 4 2 2 1 出力例 3 1 2 2 2 2 2 2 1 入力例 4 12 38 6 1 2 1 3 1 4 1 5 2 3 2 4 2 5 2 6 2 7 2 8 2 12 3 4 3 5 3 6 3 7 3 8 4 5 4 6 4 7 4 8 5 6 5 7 5 8 6 7 6 8 6 9 6 10 6 12 7 8 7 9 7 10 8 9 8 10 9 10 9 11 9 12 10 11 11 12 2 6 12 2 9 12 1 9 12 1 6 12 2 6 12 1 6 12 出力例 4 3 2 3 4 3 4 Problem Setter: Flat35
[ { "submission_id": "aoj_2313_9805512", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)...
aoj_2314_cpp
問題文 落書きの魔女 ALBERTINE は W 列 H 行の正方形のセルからなる長方形の結界の入り口に落書きをしている.この世界では落書きは必ずセルを白色か黒色で塗ることによって成り立つ. すでに使い魔 ANJA の手によって落書きの一部が描かれているのだが,落書きの魔女は残りすべてのセルを塗り落書きを完成させたい.落書きの魔女にはあるこだわりがある.そのこだわりとは次のようなものである. 2 つの異なるセルが辺を共有するとき, 2 つのセルは隣接していると呼ぶことにする.完成された落書きにおいては,任意の 2 つの黒いセルについて,片方からもう片方への隣接した黒いセルをたどるパスがちょうど 1 つだけ存在していなければならない. どの 2 つの白色のセルも隣接してはならない. また,落書きの魔女は黒色が好きなので,こだわりを満たした状態でなおかつ最も黒色のセルが多い落書きを完成させたい.果たして落書きの魔女は最大で何個のセルが黒色に塗られたこだわりの落書きを描くことができるだろうか. 入力形式 入力は以下の形式で与えられる. H\ W\\ c_{11} c_{12} ... c_{1W}\\ c_{21} c_{22} ... c_{2W}\\ ...\\ c_{H1} c_{H2} ... c_{HW}\\ H は結界の入り口の縦幅, W は結界の入り口の横幅を表す. c_{ij} は各セルの状態を表す文字であり,上から i 行目,左から j 列目のセルの状態を表す.セルに黒色が塗られている場合は c_{ij} は '#' となり,白色が塗られている場合は '.' ,まだ色が塗られていない場合はそうでない場合は '?' となる. 出力形式 魔女のこだわりを満たした状態でとりうる黒色のセルの個数の最大値を 1 行で出力せよ. もしどのように塗っても魔女のこだわりを満たすように出来ない場合, -1 と出力せよ. 制約 1 ≤ H ≤ 11 1 ≤ W ≤ 11 c_{ij} は '#' , '.' , '?' のいずれかである. 入出力例 入力例 1 3 4 .##. #??# .#?# 出力例1 8 入力例 2 5 5 ##?## #???# ????? #???# ##?## 出力例 2 19 入力例 3 9 2 ## .. ?? ?? ?? ?? ?? ?? ?? 出力例 3 -1 入力例 4 7 9 ??#####?? ?#?????#? ?#?????#? ?#??#??#? ?#???#?#? ??#####?? ???????#? 出力例 4 44 入力例 5 11 11 ??????????? ???####???? ???#???#??? ???#???#??? ???#???#??? ???####???? ???#???#??? ???#???#??? ???#???#??? ???####???? ??????????? 出力例 5 86 Problem Setter: Flat35
[ { "submission_id": "aoj_2314_10712143", "code_snippet": "// AOJ #2314 Scribbling witch\n// 2025.5.30\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MAX = 11;\n\nint h, w;\nchar B[MAX][MAX+1];\nmap<ll,int> memo[MAX][MAX];\nchar mapto[16];\nint nxt(ll, int, int, char);\n\n...
aoj_2318_cpp
問題文 キュゥべえ はワルプルギスの夜に向けて魔女の合成に明け暮れている.ワルプルギスの夜のために,特別な グリーフシード (魔女の卵) を使って手に入れやすい魔女をうまく合成し目的である舞台装置の魔女を創り上げなければならないからである. キュゥべえ は手に入れやすい種類の魔女の グリーフシード から魔女の素となるものを取り出し,空の特別な グリーフシード に入れることで,その グリーフシード に魔女を宿らせることができる.また,この世界には,ある組み合わせで複数の魔女を合成すると新たな 1 つの魔女を生み出すことができるという合成法則がいくつかある.そこで,いくつかの特別な グリーフシード を使い,それらの中にある魔女の素となるものを合成し, 1 つの特別な グリーフシード に新たな魔女を宿らせることができる.また,合成した結果空となった特別な グリーフシード は再利用することができる.例えば, 3 個の特別な グリーフシード に入った魔女を合成すると,それらの 3 個の グリーフシード は,新たな魔女が宿った 1 個の特別な グリーフシード と 2 個の空の特別な グリーフシード となる.しかし,同じ種類の魔女を同時に複数の グリーフシード に入れることは出来ない. キュゥべえ は魔女の特別な グリーフシード への寄宿と魔女の合成を繰り返すことにより舞台装置の魔女を創りだしたい.舞台装置の魔女を創りだすにはいくつの特別な グリーフシード が必要か求めよ. 入力形式 入力は以下のような形式で与えられる. N\ E\ T\\ W_1\ W_2\ …\ W_N\\ G_1\ C_1\ S_{1,1}\ S_{1,2}\ …\ S_{1,C_1}\\ G_2\ C_2\ S_{2,1}\ S_{2,2}\ …\ S_{2,C_2}\\ …\\ G_E\ C_E\ S_{E,1}\ S_{E,2}\ …\ S_{E,C_E}\\ N は魔女の種類の個数, E は合成法則の個数, T は舞台装置の魔女の番号である. W_i は i 番目の魔女が手に入れやすい魔女であるかどうかを表し, 1 ならば手に入れやすい魔女であり, 0 ならば手に入れやすい魔女ではない. 続く E 行に合成法則が次のように表される:「番号が S_{i,1}\ S_{i,2}\ …\ S_{i,C_i} の魔女を合成することによって G_i の番号の魔女が得られる.」 出力形式 舞台装置の魔女を創りだすのに最低限必要な特別な グリーフシード の個数を出力せよ.創りだすことができない場合は -1 を出力せよ. 制約 1 \lt N \leq 300 0 \leq E \leq 1000 1 \leq T \leq N 0 \leq W_i \leq 1\ (1 \leq i \leq N) 1 \leq G_i \leq N\ (1 \leq i \leq E) 2 \leq C_i \leq 10\ (1 \leq i \leq E) 1 \leq S_{i,j} \leq N\ (1 \leq i \leq E,\ 1 \leq j \leq C_i) \{G_i, S_{i,1}\ S_{i,2}\ …\ S_{i,C_i}\} は全て異なる. 入力中に同一の合成法則が 2 回以上現れることはない. 入出力例 入力例 1 3 1 3 1 1 0 3 2 1 2 出力例1 2 入力例 2 5 0 1 1 1 1 1 1 出力例 2 1 入力例 3 18 5 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 2 3 4 5 2 2 6 7 3 3 8 9 10 4 4 11 12 13 14 5 4 15 16 17 18 出力例 3 5 入力例 4 6 4 6 1 1 1 0 0 0 4 2 1 2 5 2 2 3 6 2 4 5 6 3 1 2 4 出力例 4 3 入力例 5 13 9 13 1 1 0 1 1 0 1 1 0 1 1 0 0 3 2 1 2 6 2 4 5 9 2 7 8 12 2 10 11 6 2 2 3 9 2 5 6 12 2 8 9 3 2 11 12 13 4 3 6 9 12 出力例 5 5 入力例 6 4 4 4 1 1 0 0 1 2 2 3 2 2 3 1 3 2 1 2 3 3 1 2 4 出力例 6 -1 Problem Setter: Flat35
[ { "submission_id": "aoj_2318_9799752", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)...
aoj_2317_cpp
問題文 委員長の魔女 PATRICIA は,蜘蛛のような糸を吐き結界の中で暮らしている.使い魔の MATHIEU は,委員長の魔女が吐いた糸を引っぱり自在に空を飛んでいる.糸は空間上の線分とみなすことにする. 暁美ほむら は委員長の魔女に対して攻撃を仕掛けようとし,爆弾と間違えて花火玉を投げ込んでしまった.その結果,それぞれの糸について,魔女から距離 p_1, ..., p_M の位置にある部分が切れるだけとなってしまった.花火玉を投げる前,それぞれの糸について,糸の 2 つの端点と魔女はこの順番で同一直線上に並んでおり,端点のどちらか一方を使い魔が引っ張っていた.爆発で糸は切れ,使い魔が持っていた部分とそこから一つ置きの部分を残して糸の一部は消えてしまった. ほむら は次の戦略を考えるために,残った糸についての情報が知りたいと思っている.残った糸の長さの合計値を求めよ. 入力形式 入力は以下の形式で与えられる. N\ M\\ s_1\ t_1\\ ...\\ s_N\ t_N\\ p_1\\ ...\\ p_M\\ N は空間上の糸の個数, M は花火玉によって発生した糸の切断の箇所の個数である. s_i, t_i は糸の情報であり, s_i は使い間が引っ張っていた側の端点, t_i はそうでない側の端点の魔女からの距離である. p_i は花火玉によって切断された位置の情報である. 出力形式 花火玉を投げた後,残った糸の長さの合計値を1行に出力せよ. 制約 1 ≤ N ≤ 10 5 1 ≤ M ≤ 10 5 1 ≤ s_i ≤ 10^9 1 ≤ t_i ≤ 10^9 1 ≤ p_i ≤ 10^9 入力値は全て整数である. s_1, ..., s_N, t_1, ..., t_N, p_1, ..., p_M は全て相異なる. 入出力例 入力例 1 2 3 1 8 20 5 3 7 15 出力例1 10 爆発の後, 1 つ目のひもは端点の魔女からの距離がそれぞれ (1,3), (7,8) の 2 つのひもに, 2 つ目のひもは (20, 15), (7, 5) の 2 つのひもに分かれる.結果,残ったひもの長さの合計は 2+1+5+2 = 10 となる. 入力例 2 1 1 100 1 70 出力例 2 30 入力例 3 6 8 1 10 11 23 99 2 56 58 66 78 88 49 5 15 25 35 45 55 65 75 出力例 3 99 Problem Setter: Flat35
[ { "submission_id": "aoj_2317_5550696", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1100000000;\nint main(){\n int N, M;\n cin >> N >> M;\n vector<int> s(N), t(N);\n for (int i = 0; i < N; i++){\n cin >> s[i] >> t[i];\n }\n vector<int> p(M);\n for (int i = 0; i ...
aoj_2319_cpp
問題文 キュゥべえ はなかなか契約を結んでくれない 鹿目まどか にしびれを切らせて,ソウルジェムゲームと称して 佐倉杏子 と 美樹さやか のソウルジェムの中身 (魂) を W 列 H 段のロッカーの中に隠してしまった.そこで, まどか は 杏子 と さやか を助け出したい.しかし,契約を結ばずに助けるにはロッカーボックスの壁を操作して 2 人のソウルジェムの中身を,指定されたロッカーボックスに正しく移さなければならない.ロッカーはロッカーボックスが W 列 H 段に並ぶことで構成され,隣り合うロッカーボックスは壁で仕切られている. まどか は, 1 回の操作でいずれかの隣りあった 2 つのロッカーボックスの壁を取り除くか取り付けることができる.ただし,ロッカーボックスの壁を開けるには制約があり,各ロッカーボックスから隣接する上下左右の壁のうち,高々 2 枚までの壁しか同時に開けておくことはできない.ロッカーボックスには上の段から下の段に向かって重力が働いている.ソウルジェムの中身は液体と同じような動きをし,隣りあった 2 つのロッカーボックスの壁が取り除かれている時,同じ高さであれば互いに同じ量になり,違う高さであれば低い方へ移動する.すべての操作が終わった時に指定された 2 つのロッカーボックスに正しい組み合わせでソウルジェムの中身がすべて入っていれば まどか の勝利となる.また,一部のロッカーボックスは穢れており,その中へソウルジェムの中身が入ってしまうと大変なことになってしまうので,そのロッカーボックスはソウルジェムの中身の移動のために使うことはできない. 最小で何度の操作を行えば まどか は 2 人を救いだすことができるかを求めよ. 入力形式 入力は以下の形式で与えられる. H\ W\\ s_{11}\ s_{12}\ … s_{1W}\\ s_{21}\ s_{22}\ … s_{2W}\\ …\\ s_{H1}\ s_{H2}\ … s_{HW}\\ H はロッカーの段数, W はロッカーの列数である. s_{ij} は上から i 段目の左から j 列目のロッカーボックスの状態を表す. 杏子 のソウルジェムの中身が入っているときは 'K' であり, さやか のソウルジェムの中身が入っているときは 'S' , 杏子 のソウルジェムの中身を移すべきロッカーボックスであるときは 'k' , さやか のソウルジェムの中身を移すべきロッカーボックスであるときは 's' ,穢れたロッカーボックスであるときは '#' ,それ以外のロッカーボックスであるときは '.' である. 出力形式 2 人を救い出すために必要な操作の最小回数を出力せよ.どのように操作しても救い出すことが出来ない場合は CONTRACT と出力せよ. 制約 1 ≤ H ≤ 10 1 ≤ W ≤ 10 s_{ij} は '#' , '.' , 'S' , 'K' , 's' , 'k' のいずれかである. s_{ij}\ =\ 'S' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 'K' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 's' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 'k' であるような i, j がちょうど 1 つだけ存在する. 入出力例 入力例 1 4 4 .S.. K... .... ..sk 出力例1 9 入力例 2 5 5 .S.K. ..#.. .###. ..#.. .k.s. 出力例 2 CONTRACT 入力例 3 8 5 S.... ####K ##... #..## #..## ##k## #...# #.s.# 出力例 3 19 入力例 4 8 5 S.... ####K ##... ##.## ##.## ##k## #...# #.s.# 出力例 4 CONTRACT 入力例 5 10 10 .......... ##......## #K......S# .......... .......... .#..##..#. ..##..##.. .......... .......... ....s.k... 出力例 5 22 入力例 6 3 3 S.s K.. ..k 出力例 6 CONTRACT Problem Setter: Flat35
[ { "submission_id": "aoj_2319_5955599", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int...