group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_0259_cpp
すべての数は6174に通ず 0 から 9 の数字からなる四桁の数 N に対して以下の操作を行う。 N の桁それぞれの数値を大きい順に並べた結果得た数を L とする N の桁それぞれの数値を小さい順に並べた結果得た数を S とする 差 L-S を新しい N とする(1回分の操作終了) 新しい N に対して 1. から繰り返す このとき、全桁が同じ数字(0000, 1111など)である場合を除き、あらゆる四桁の数はいつかは 6174になることが知られている。例えば N = 2012の場合 1回目 (N = 2012): L = 2210, S = 0122, L-S = 2088 2回目 (N = 2088): L = 8820, S ...
[ { "submission_id": "aoj_0259_9621832", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define MAX 1000000007\n\nint main(void){\n string s;\n while(true){\n cin >> s;\n if(s == \"0000\") break;\n if(s[0] == s[...
aoj_0260_cpp
パイプつなぎ職人の給料 ぼくはパイプつなぎ職人です。パイプをつなぐジョイントとパイプさえもらえれば、どんなパイプだってつないでみせます。ぼくは毎日、親方からパイプとジョイントを渡されて、それをつないで親方に渡します。でも、パイプの数が多すぎるときは、1日でそれを全部つなげることはできません。そんなときでも親方はにっこり笑って、ぼくに給料を渡してくれます。 ところで、あるとき変なことに気がつきました。全部のパイプをつなげられたときより、つなげられなかったときの方が給料が多いことがしょっちゅうあるんです。あんまり変なので、ある日、親方が来たときに、給料の計算方法が書いてあるメモをこっそり見ちゃいました。そしたら、なんと "給料は「パイプ...
[ { "submission_id": "aoj_0260_10867597", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i < t ; ++i)\n#define ALL(a) a.begin(),a.end()\n#define SORT(a) sort(ALL(a))\n#define UNIQ(a) a.erase(unique(ALL(a)),a.end())\n#define debug(a) cerr<<#a\":=\"<<a<<endl;\n...
aoj_0262_cpp
すごろくを作る 太郎君は、子供会の催しでみんなで遊べるようにすごろくを作りました。ゲームをおもしろくするために、「ふりだし」と「あがり」以外のすごろくのマスのいくつかに「6つ進む」、「5つ戻る」のように指示を書き込んでいきました。ルーレットを回して出た数だけ進み、止まったマスに指示が書き込んであれば、その指示に従って移動します。ただし、指示に従って進んだ先のマスの指示には従いません。 ルーレットは1からある数までの間の数を等確率で出すことができるものとします。また、「あがり」に達するより大きな数が出たときや、指示に従うと「あがり」より先に進んでしまうときは「あがり」に移動します。指示に従って戻るときに「ふりだし」より前に戻ってしまう...
[ { "submission_id": "aoj_0262_9597880", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rrep(i,n) for(int i=n-1;i>=0;i--)\n#define Rrep1(i,n) for(int i=n;i>0;i--)\n#define all(a) a.begin(),a.end()\nusin...
aoj_0263_cpp
ビートパネル 遊太君は、近所のゲームセンターで人気のゲーム「ビートパネル」にはまっています。このゲームは図のようなグリッド状に並べられた 4×4 の計16個のパネル型のボタンからなります。 図のように、左上から右下にボタン1, ボタン2, …, ボタン16の順にボタンが並んでいます。ゲームでは一定間隔でビート音が鳴り最後に終了音が鳴ります。ビート音が鳴ると同時に複数のボタンが光ります。1個も光らない場合もあります。プレイヤーは、ビート音が鳴った直後から次の音が鳴るまでの間に両手の指を使って複数のボタンを1度だけ同時に押すことができます。何も押さないということも可能です。終了音が鳴ったと同時にゲームは終了します。 遊太君は c 通りの...
[ { "submission_id": "aoj_0263_10848448", "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_0264_cpp
有限体電卓 0以外のすべての実数の逆数は実数になりますが、整数の逆数が整数になるとは限りません。C言語などで 3.0/2.0*2.0 = 3.0 なのに 3/2*2 = 2 になってしまうのはこのためです。ですが、素数で割った余りが等しい整数を同じものとみなすと、0以外のすべての整数が逆数をもつようにできます。 整数 x と y を p で割った余りが等しいとき、x ≡ y (mod p) と書きます。p が素数のとき、このような x と y を同じものとみなすなら、すべての整数 n は0から p−1 までのどれかの整数 x に対して x ≡ n (mod p) となるので、けっきょく { 0, 1, 2, ..., p−1 } と...
[ { "submission_id": "aoj_0264_10865771", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i < t ; ++i)\n#define ALL(a) a.begin(),a.end()\n#define SORT(a) sort(ALL(a))\n#define UNIQ(a) a.erase(unique(ALL(a)),a.end())\n#define debug(a) cerr<<#a\":=\"<<a<<endl;\n...
aoj_0267_cpp
ブロックの三角形 図a のように積まれたブロックに対し、以下の並べ替え操作を繰り返す。 一番下のブロック全て(図a 中の白のブロック)を右端に新しく積み上げる(残りのブロックは自動的に1段下に落ち、図b のようになる)。 ブロックの間に隙間ができたら、左に詰めて隙間をなくす(図 b から図c のようになる)。 1 以上の整数 k に対して、k×(k+1)/2 で表される数 (例:1, 3, 6, 10, ...)を三角数という。ブロックの総数が三角数の場合、上記の並べ替えを繰り返すと、左端の高さが1 で右に向かって1つずつ高くなっていくような三角形になると予想されている(図d は総数が15 個の場合)。 ブロックの最初の並びが与えら...
[ { "submission_id": "aoj_0267_9568704", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nconst int MAX_CNT = 10000;\nconst int MAX_SET = 20;\n\nint main()\n{\n int N;\n int setcount = 0;\n\n\n while (cin >> N && N != 0 && setcount < MAX_SET)\n {\n if (N < 1 ...
aoj_0265_cpp
ねこまっしぐら あるところに、高い塀に囲まれた大きなお屋敷がありました。そのお屋敷の主人は猫がとても大好きで、時折やってくる猫たちのために、いつもおいしいごはんを用意していました。お腹を空かせた猫たちは、高い塀をひょいと飛び越え、お屋敷の至る所に置いてあるごはんにまっしぐらに駆け寄るのでした。 ある日主人は、屋敷の中で何匹かの猫が倒れているのを見つけました。猫たちはごはんを探して屋敷の中を縦横無尽に走り回ったので、ぶつかったり転んだりしてしまったのです。主人は猫たちの安全を考えて、ごはんの置き場所を工夫することにしました。 上空から見ると、このお屋敷の塀は多角形をなしています。主人は猫たちがごはんを見つけやすいように、敷地内の中でも...
[ { "submission_id": "aoj_0265_5433557", "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 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\n#defi...
aoj_0268_cpp
金剛の型 以下のように、右から 7 ビット分が小数部、続く24 ビット分が整数部で、一番左の 1 ビット分を符号部とする 32 ビット実数型を考える(b 1 , ... , b 32 は 0 か1 を表す)。 この形式を、人が理解しやすい 10 進数表現に直すときは、以下のように解釈する。 (-1) 符号部 × (整数部 + 小数部) 上の表現で、整数部の値は b 8 + 2 1 ×b 9 + 2 2 ×b 10 + ... + 2 23 ×b 31 になる。例えば整数部が のようになっていた場合、整数部の値は以下のように計算される。 1 + 2 1 + 2 3 = 1 + 2 + 8 = 11 一方、小数部の値は(0.5) 1...
[ { "submission_id": "aoj_0268_10848234", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n int Q;\n cin >> Q;\n while(Q--) {\n string s;\n cin >> s;\n int n = 0;\n for(int i=0; i<8; ++i) {\n n <<= 4;\n ...
aoj_0269_cpp
風よ、私の梅の香りを届けておくれ! 引っ越しが決まり、この地を去ることになった。この土地自体に未練は無いが、1つだけ気になることがある。それは、庭に植えた梅の木のことだ。私は毎年、この梅が花を咲かすことを楽しみにしていた。ここを離れた後は春の楽しみが1つ減ってしまう。私の梅の香りだけでも風に乗って引っ越し先の家まで届き、春を楽しませてはくれないものか。 日本には春を象徴する3つの花がある。梅・桃・桜の3つだ。引っ越し先には、私の梅以外にも、これらの花の香りが届くだろう。しかし、私の梅の香りだけが届く日数が最も多い家に住みたい。 図のように、花の香りは扇状に広がり、その領域は風の向かう方向と強さによって決まる。扇形は風の向かう方向 w...
[ { "submission_id": "aoj_0269_4985319", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1.0);\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0.0), y(0.0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(...
aoj_0272_cpp
寂しがり屋のついた嘘 パケットモンスターというゲームがあります。モンスターを捕まえ、育て、戦わせたり交換したりするゲームで、日本中で大流行しています。もちろん、わたしのクラスでも。だからわたしもこのゲームでみんなと遊べるようにモンスターを育てているけど、悲しいことに対戦する相手がいません。わたしは積極的に人に話しかけるのが得意なタイプではないからです。だから今日も、クラスメイトが誘ってくれるのを待ちながら、独り寂しくモンスターのレベルを上げています。 そしてついに、待ち望んでいた瞬間が訪れました。クラスメイトの一人が対戦しないかとわたしに話を持ちかけてきたのでした。彼女は自慢気に、彼女のモンスターたちを見せてくれました。 一般的なパ...
[ { "submission_id": "aoj_0272_10774072", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconstexpr int N = 40000;\nint a[N], b[N];\nint cnt[N];\nint n;\n\nvoid solve() {\n for (int i = 0; i < n; ++i) cin >> a[i];\n for (int i = 0; i < n; ++i) cin >> b[i];\n sort(a, a + n);\n ...
aoj_0270_cpp
モジュロ・クエリ あなたに N 枚のカードを渡します。どのカードにも一つだけ自然数が書いてあります。 これから質問として、適当な自然数を言います。あなたが持っているカードに書いてある数を私が言った数で割ったときに得られる余りのうち最も大きなものを答えてください。 たとえば、あなたは 3 枚のカードを持っていて、それぞれ 9、3、8 と書いてあるとします。私が「4」と言ったら、9 と 3 と 8 をそれぞれ4 で割った余りを求めてください。余りはそれぞれ 1、3、0 ですが、この中でもっとも大きな余りは3 なので、3 が正しい答えになります。 では始めましょうか。え? カードがいっぱいあるとたいへんだ? しょうがないですね。それではコ...
[ { "submission_id": "aoj_0270_10853187", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, Q;\n cin >> N >> Q;\n vector<int> c(N);\n int size = 0;\n for(int i=0; i<N; ++i) {\n cin >> c[i];\n size = max(size, c[i]);\n }\n size += 1;\n ...
aoj_0261_cpp
マヤの大予言 真也君はテレビで「マヤの大予言!2012年で世界が終る?」という番組を見ました。結局、世界が終るかどうかはよくわかりませんでしたが、番組で紹介されていたマヤの「長期暦」という暦に興味を持ちました。その番組では以下のような説明をしていました。 マヤ長期暦は、右の表のような単位からなる、全部で13バクトゥン(187万2000日)で構成される非常に長い暦である。ある計算法では、この暦は紀元前3114年8月11日に始まり2012年12月21日に終わると考えられていて、このため今年の12月21日で世界が終るという説が唱えられている。しかし、13バクトゥンで1サイクルとなり、今の暦が終わったら新しいサイクルに入るだけという考えもあ...
[ { "submission_id": "aoj_0261_9597837", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rrep(i,n) for(int i=n-1;i>=0;i--)\n#define Rrep1(i,n) for(int i=n;i>0;i--)\n#define all(a) a.begin(),a.end()\nusin...
aoj_0274_cpp
図画工作 イヅア大学附属小学校は日本有数の競技プログラマー養成校として有名です。この学校の教員は幅広い アルゴリズムの知識を持ち、日々それを活用しています。教員であるあなたは、今年は図画工作の授業 を担当することになりました。この授業では、児童全員がそれぞれ1年間で一つの課題を完成させることになっています。授業の概要は以下のとおりです。 1年間で授業は D 回(同じ日に2 回以上授業はない)あり、その全てが課題制作に充てられる。 制作する課題は M 種類用意されている。 それぞれの児童に、M 種類の中から課題を1つずつ割り当てる。 児童は N 人であり、N 人それぞれに異なる課題が割り当てられる。 児童は、K 種類ある部品のうちいく...
[ { "submission_id": "aoj_0274_9267197", "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_0273_cpp
ねこまっしぐら2 あるところに大きな古いお屋敷があり、1匹のねこが住み着いていました。図のように、お屋敷は上空から見ると凸多角形になっており、まっすぐな壁で囲まれたいくつかの部屋で造られています。1枚の壁はその両端にある柱によって支えられています。お屋敷はとても古いため、どの壁にもねこが通ることができるくらいの穴が1つ空いています。ねこは壁の穴を通ってお屋敷の部屋と部屋または部屋と外を行き来することができます。 お屋敷のご主人は、ねこに餌をあげるために、口笛を吹いてねこをお屋敷の外へ呼び出します。ねこはとても賢く、ご主人が口笛を吹くと、「最良の選択」でお屋敷の外へ抜け出します。つまり、最も少ない回数だけ穴を通って外へ出ます。 ねこは...
[ { "submission_id": "aoj_0273_10848620", "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_0282_cpp
プログラミングコンテスト 白虎大学では毎年プログラミングコンテストが開催されています。チームの総数をNとしたとき、チームには1からNのチームIDがそれぞれ割り当てられています。コンテストは全てのチームの得点が0の状態から開始し、L秒間連続して行われます。 今年のコンテストは、テレビで中継されることになりました。コンテストの間テレビに映るのは、その時点で最も得点の高いチームだけです。ただし、該当するチームが複数いるときは、その中でチームIDの一番小さいチームが映ります。映すべきチームが変わると、瞬時にカメラが切り替わります。 あなたはコンテストのログを入手しました。ログには、あるチームの得点が変化した時のレコードがすべて含まれています...
[ { "submission_id": "aoj_0282_10853284", "code_snippet": "// 7\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for (int i =(a); i <(b); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(), (v).end()\n#define fi first\n#define se second\ntemplate<typename A, typename B> inline bo...
aoj_0275_cpp
鉄道路線 ある国の鉄道網に、自動改札を導入することになりました。導入にあたって難しい問題の一つは、与えられた切符で、指定された駅の間を移動できるかどうかを判定することです。それぞれの切符には乗車駅と降車駅が記載されています。この切符でできるのは、「乗車駅で乗って、降車駅で降りる」ことだけではなく、途中乗車や途中下車も許されています。 この鉄道網にはS 個の駅があり、そのうちR 組の駅は隣り合っていて、他の駅を経由せずに双方向に鉄道で移動することができます。隣り合った駅を結ぶ線路はひとつしかありません。隣り合った駅の間の距離は、この線路に沿って測った距離です。ある駅からある駅までの経路は鉄道網の形状によっては複数通り考えられますが、そ...
[ { "submission_id": "aoj_0275_10170526", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)\n#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define ...
aoj_0281_cpp
陣形 プログラマー養成校であるアカベ高校では、チーム戦の競技プログラマーの役割を以下の3タイプに分けています。 C: コーダー 言語を熟知しており、コーディングを行う。 A: アルゴリズマー 論理的思考が得意であり、アルゴリズムを考える。 N: ナビゲーター 読解力に長けており、問題の分析・デバッグをする。 この高校では以下のいずれかの陣形で3人一組のチームを構成しています。 CCA: バランスがとれている安定型 CCC: スピードを見込めるがリスクの高い速攻型 CAN: 正確に問題を解く慎重型 競技プログラミング部のコーチであるあなたは、これらの部員をうまく組み合わせ、できるだけ多くのチームを結成できるよう、毎年気を配っています。...
[ { "submission_id": "aoj_0281_9637717", "code_snippet": "#include<bits/stdc++.h>\n\n#define ll long long\nusing namespace std;\n\n\n\nint main() {\n//#ifndef ONLINE_JUDGE\n// freopen(\"Input.txt\", \"r\", stdin);\n// freopen(\"Output.txt\", \"w\", stdout);\n//#endif\n int t;\n cin >> t ;\n whi...
aoj_0279_cpp
おそろいの景品 ジョウ君とヤエさんは仲の良いカップルです。ジョウ君はカプセル玩具自販機(ガチャポン)の景品を集めており、二人で出かけたときも、ガチャポンを見つけると何度かやってみるほどの熱の入りようです。ヤエさんは楽しそうなジョウ君をそばで見ているだけでしたが、ジョウ君の今度の誕生日プレゼントにガチャポンの景品をひとつあげることにしました。ヤエさんはガチャポン自体にはあまり興味がわきませんでしたが、できればジョウ君とおそろいの景品が欲しいと思っています。 ヤエさんがやってみようと思うガチャポンは、1回のチャレンジで景品がひとつ出ます。品切れのものも含め景品が何種類あるのかと、それぞれの景品がいくつ残っているのかはわかります。しかし、...
[ { "submission_id": "aoj_0279_10867650", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <map>\n#include <numeric>\n#include <bitset>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\ntypedef long long ll;\n#define FOR...
aoj_0283_cpp
勉強会 プログラマー養成校のアカベ高校には、生徒自身で運営するユニークな勉強会があります。プログラマーは新しい技術を常に取り入れることが大切なので、この勉強会を通して自学自習の習慣を身につけることがこの活動のねらいです。 生徒は全部で N 人おり、それぞれが入学時のプログラミングコンテストの結果で得られたスコアを持っています。勉強会では N 人の生徒のうち何人かがリーダーになり、各リーダーがそれぞれのグループを運営するとともに、自らの運営するグループに参加します。 リーダー以外の生徒は、自分のスコアよりも低いスコアのリーダーが運営するグループには参加できません。また、0以上のある値 r を1つ決め、グループに参加する生徒とリーダーの...
[ { "submission_id": "aoj_0283_10852139", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0284_cpp
ハッピーエンド問題 「ハッピーエンド問題」と呼ばれる数学の未解決問題に関連したプログラムを書いてみましょう。平面上に与えられた N 個の点から、ちょうど k 個の点を結んでできる凸多角形のうち、最も面積の小さいものを見つけるプログラムを作成してください。ただし、N個の点の座標を与えられた後、質問として凸多角形の角の個数 k がいくつか与えられます。 (補足:ハッピーエンド問題について) 平面上にどの3点も同じ直線上に乗らないように N 個の点を置きます。そのとき、どのように点を置いても、k個の点をうまく選ぶとk個の角をもつ凸多角形が必ず作れると予想されています。 今のところ、三角形ならば N =3、四角形ならば N =5、五角形なら...
[ { "submission_id": "aoj_0284_10864669", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first;\n#define se second;\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntemp...
aoj_0271_cpp
イヅア国語辞典 あなたはイヅア国の公用語であるイヅア語の国語辞典と、イヅア語のアルファベット(文字の一覧表) を手に入れました。イヅア語のアルファベットにはN 種類の文字があります。イヅア語の国語辞典に現れる単語の順番は、イヅア語のアルファベット順に並んでいます。 辞典を見ると、載っているどの単語もN 文字で、しかも、N 種類の文字をひとつずつ含んでいることがわかりました。さらに調べてみると、辞典にはN 種類の文字の可能な並び方がすべて書かれていることを発見しました。 この発見から、あなたはある単語が辞典の何番目に出てくるかわかるようになりました。この知識を利用してイヅア国の人を驚かせてみましょう。まず、N 種類の文字をひとつずつア...
[ { "submission_id": "aoj_0271_10568001", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int mod = 1e9 + 7;\n\nint mul(int x, int y) {\n return (1ll * x * y) % mod;\n}\n\nconst int MAXN = 1e5 + 5;\n\nint fact[MAXN];\n\nvoid precalc() {\n fact[0] = 1;\n for (int i = 1; i < ...
aoj_0287_cpp
塵劫記 大きな数を表そうとすると、文字数も多くなるし、位取りがわからなくなってしまうので、なかなか面倒です。大きな数をわかりやすく表すために、人々は数の単位を使ってきました。江戸時代に書かれた「塵劫記」という本の中では、数の単位が次のように書かれています。 たとえば、2の100乗のようなとても大きな数は、126穣7650(じょ)6002垓2822京9401兆4967億320万5376と表せます。それでは、正の整数 m と n が与えられたとき、 m の n 乗を塵劫記の単位を使って上のように表すプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与...
[ { "submission_id": "aoj_0287_3201960", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<cmath>\nusing namespace std;\ntypedef vector<int> vi;\nconst string units[]={\n \"\",\"Man\",\"Oku\",\"Cho\",\"Kei\",\"Gai\",\"Jo\",\"Jou\",\"Ko\",\"Kan...
aoj_0286_cpp
親方の給料計算 ワシはパイプつなぎ組合の親方じゃ。職人を工事現場に派遣し、現場でパイプをつながせておる。去年は工事が増えて大儲けするかと思ったのじゃが、ちょっと給料の出し方がまずくてのぅ。ウチとしては大赤字になってしまったのじゃよ…。そこで、今年は職人たちへの給料の出し方を工夫したいのじゃ。 職人たちの給料は、工事の種類とこなした回数で決めておる。つまり、 職人の給料 = 種類 1 の単価 × 種類 1 をこなした回数 + 種類 2 の単価 × 種類 2 をこなした回数 .... + 種類 M の単価 × 種類 M をこなした回数 となるのじゃ。これを計算するために、ウチでは、どの職人がどの種類の工事を何回こなしたかを次のような表に...
[ { "submission_id": "aoj_0286_11031173", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N, M;\n\tcin >> N >> M;\n\tvecto...
aoj_0291_cpp
古代遺跡の謎 古代遺跡から超大型あみだくじが発見された。学者達はこれをVLA(Very Long Amidakuji)と命名した。分析の結果、VLAは N 本の縦棒を持ついくつかの部品の組み合わせで構成され、多くの場合繰り返しを含んでいることがわかった。例えば、図のVLAは2種類の部品AとBを使った4つの部品から構成されている。 このVLAの上端に左から順番に 1,2,3,4,5 と番号をつけ、それぞれの番号からVLAをたどり(あみだくじのたどり方については問題文最後の補足を参照してください)、下端にたどり着いた場所の縦棒にその番号を振る。すべての番号についてたどり終えた後で、下端に振られた番号を左から読むと 4,2,5,1,3 に...
[ { "submission_id": "aoj_0291_10852950", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint H, N, Q, p[30][30]; vector<int>r[30], J; string c[30]; string S;\n\nvector<int>amida_add(vector<int>a, vector<int>b) {\n\tvector<int>c = a, p = a, q = a;\n\tfor (in...
aoj_0288_cpp
巨樹の刻み手 あなたは N 種類の道具を使って、目の前の巨樹を切り倒そうとしています。はじめは、樹の耐久力は D 、あなたの経験値は 0 ですが、道具 i で1回樹を叩くと樹の耐久力は a i 減り、あなたは e i の経験値を得ます。ただし、道具 i を使うためには、あなたは r i 以上の経験値を持っていなければなりません。樹の耐久力が 0 以下になると樹は倒れます。 樹の耐久力と道具についての情報が与えられたとき、樹を切り倒すには最低何回樹を叩かなければいけないかを求めるプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 D N...
[ { "submission_id": "aoj_0288_10848293", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing P = pair<int, int>;\n\nconstexpr int INF = 1e9;\n\nint main() {\n int D, N;\n while(cin >> D >> N, D) {\n using tool = tuple<int, int, int>;\n vector<tool> v(N);\n for(...
aoj_0294_cpp
アカ・ベコ捕獲作戦 怪盗アカ・ベコは大胆にも、ツルガジョーから銀のシャチホコを盗み去った。警部であるあなたは、アカ・ベコが仲間のコボウ氏にシャチホコを渡すらしいという情報を手に入れた。ただ、コボウ氏はアカ・ベコのアジトまで出向けないので、アジトの外で渡すらしい。あなたはアカ・ベコを待ち伏せして捕まえることにした。この町は古い城下町なので、道が複雑に入り組んでいて、どの道も一方通行になっている。アカ・ベコがアジトから受け渡し場所までどの経路を通るのかはわからないが、人員不足のため、待ち伏せ場所は1つに設定しなければならない。 あなたは、アジトを含むいくつかの地点を選び、それらをつなぐ道を調べ、地図を作った。地図上の地点のどこかで受け渡...
[ { "submission_id": "aoj_0294_8775198", "code_snippet": "#include <iostream>\n#include <string.h>\n#include <vector>\n#include <queue>\nusing namespace std;\nconst int LIMIT=100003;\nint ans[LIMIT];\nint ans2[LIMIT];\nint count[LIMIT];\nvector<int> vs[LIMIT];\nqueue<int> qs;\nint main() {\n\t// your code goe...
aoj_0292_cpp
壁 2XXX年、突然出現した天敵の侵入を防ぐために、人類は壁を作りその中に逃げ込んだ。その結果、人類の活動領域はその壁で囲まれた範囲に限定されてしまった。この領域は、上空から見ると W × H の長方形である。領域内部には x 軸あるいは y 軸に対して平行な壁がいくつか設置されている。活動領域の例を下図に示す。 人類は活動領域内を自由に移動することができるが、壁を越えるためには一定量の資源を消費しなければならない。ただし、壁を越えることはできるが(図中の (1))、壁の交点を越えること (2)、壁や活動領域の境界の上を移動すること (3)、活動領域外に出ること (4) はできない。 領域内部の壁の情報といくつかの始点と終点の組を入...
[ { "submission_id": "aoj_0292_5958818", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> dx = {1, 0, -1, 0};\nvector<int> dy = {0, 1, 0, -1};\nint main(){\n int W, H, M;\n cin >> W >> H >> M;\n vector<int> px(M), py(M), qx(M), qy(M);\n for (int i = 0; i < M; i++){\n cin >> ...
aoj_0293_cpp
アルゴリズム検定試験 あなたはイズア地区におけるアルゴリズム検定試験を運営しており、運営費を最小にしたいと考えています。運営費は、会場使用料、バスの使用料、移動補助金の総和です。 今年は N 人の受験者登録があり、それぞれ c i 人まで受け入れ可能な M 個の会場を確保しました。あなたは各受験者を必ずどこかの会場に割り当てなければなりません。1 人以上割り当てた会場については、人数にかかわらず f i 円の会場使用料を支払う必要があります。 イズア地区は図に示すように東西方向と南北方向に 1 km 間隔で道が走っており、受験者の家と会場は交差点上にあると考えます。各受験者は家から会場まで道に沿って徒歩で移動できます。また、この検定...
[ { "submission_id": "aoj_0293_9263225", "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_0290_cpp
微生物発電 飯沼博士は磐梯山の噴気孔でふしぎな微生物を見つけました。この微生物の雄と雌1体ずつが合体すると、電気エネルギーを放出します。この微生物を研究すれば、将来のエネルギー危機から我々を救えるかもしれません。 観察を続けると、微生物は合体したときだけ電気エネルギーを発生させることと、合体した微生物のさらなる合体はないことがわかりました。さらに観察を続けると、合体で放出される電気エネルギーは、微生物が体内に持つ未知の粒子(博士はこれを磐梯山にちなんでB粒子と名づけました)の量で決まることがわかりました。合体する雄と雌が体内に持つB粒子の量をそれぞれ bm と bw とすると、合体により放出される電気エネルギーは | bm - bw...
[ { "submission_id": "aoj_0290_5934872", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\nint dp[13][(1<<12)+2];\nvoid solve(int m,int w){\n vector<int>bm(max(m,w)),bw(max(m,w));\n for(int i = 0; max(m,w) > i; i++){\n bm[i] = bw[i] = -1;\n }\n for(int i = 0; m > i; i++){\n...
aoj_0299_cpp
鉄道路線II わたしの住む街には、図のような N 個の駅からなる円環状の鉄道路線があります。この路線の各駅には 0 から N -1 までの番号が順番に割り当てられていて、隣の駅まで100 円で移動することができます。移動はどちらの方向にでも可能です。 わたしはこの路線上のいくつかの駅で買い物をしたいと思っています。そこで、プログラムを作成して移動費を計算することにしました。ある駅を始点として、買い物をするすべての駅を訪問する最小の費用(円)を求めるプログラムを作成してください。ただし、同じ駅を何度訪問しても良く、どのような順番で駅を訪問しても構いません。 入力 入力は以下の形式で与えられる。 N M p d 1 d 2 : d M ...
[ { "submission_id": "aoj_0299_4988855", "code_snippet": "#include <iostream>\n#include <stdio.h>\n\nusing namespace std;\n\nint main()\n\n \n\n{ \n int N, M, p;\n int d[100000];\n int i, j, tmp;\n \n scanf(\"%d %d %d\", &N, &M, &p);\n for (i=0; i<M; ++i)\n {\n scanf(\"%d\", &...
aoj_0300_cpp
フロッピーキューブ フロッピーキューブをプログラミングで解いてみましょう。フロッピーキューブは図のように表面に色のついた9個の立方体から構成されている立体パズルで、キューブの列を回転させることによって、6つの各面の色をそろえます。 フロッピーキューブに対しては下図のような4種類の操作を行うことができ、一回の操作で、端にある3つの隣接したキューブを180度回転することができます。わかりやすいように、図では、上面に+(赤色)、下面に*(緑色)、右前面に□(黄色)、左前面に●(青色)、右奥面に○(水色)、左奥面に■紫色) の記号が付いている状態を初期状態としています。 フロッピーキューブの初期状態が与えられるので、パズルを解くために必要な...
[ { "submission_id": "aoj_0300_10850143", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0308_cpp
デッドロックを検出せよ コンピュータにおける「データベース」とは、情報を管理するための入れ物で、「データベースマネジメントシステム(DBMS)」とは、その管理をする仕組みです。複数のユーザが利用するデータベースでは、DBMS は慎重に構築する必要があります。 例えば、倉庫から商品を1つ取り出した人が、データベースに対して以下の処理を行うとしましょう。 (1) 商品の個数 N をデータベースから読む。 (2) 新たな商品の個数 N -1 をデータベースに書き込む。 ユーザ1が(1)を終えて(2)を始める前に、別のユーザ2が倉庫から商品を取り出して(1)を行ったとします。ユーザ2もユーザ1と同じ個数を読むので、2人が(2)を終えたときに...
[ { "submission_id": "aoj_0308_2719446", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define MAX_V 10000000\n#define MAX_E 10000000\n#define INF 1111111111\nstruct edge{int from,to,cost;};\n\nedge es[MAX_E]; \nint d[MAX_V]; \nint V=202,E;\n\nbool find_negative_loop(){\n memset(d,0,sizeo...
aoj_0303_cpp
力持ち 力持ちたちが集う力持ち学園がありました。力持ち学園の運動会では、力持ちたちが隊列を組んで行進します。 力持ちたちは常に自分たちの力を誇示したい一方で、彼らの大半は自分で歩きたくありません。そこで彼らの一部が一番下になり、その上に大勢の人を縦一列に持ちあげて歩くことで、実際に歩く人数を減らそうと考えました。 はじめに、 N 人の力持ちは地面の上に横一列に並んでいて、それぞれ左側から 1,2,3,..., N と通し番号で呼ばれています。通し番号 i の力持ちの体重は w i で最大 c i の重量まで持つことができます。 力持ちは、以下の条件をすべて満たすときだけ、隣に立っている左右どちらかの力持ちを持ちあげることができます。...
[ { "submission_id": "aoj_0303_10691213", "code_snippet": "// competitive-verifier: PROBLEM\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#if !defined(ATCODER) && !defined(EVAL)\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr b...
aoj_0304_cpp
学食 今日はZ大学のオープンキャンパスです。毎年この日の昼休みには、大勢の高校生たちが学食に列をつくります。そこでZ大学の事務局は、行列の長さが最大でどのくらいの距離になるかを予測することにしました。事前調査の結果で、以下のことが分かっています。 行列にはそれぞれ 1 から N までの番号が振られた N 人が並びます。 C 個の高校生のペア ( a i , b i ) それぞれについて、以下の2種類の制約があります: 1つ目の制約は順序に関するもので以下のいずれかです: a i は b i よりも先、または同じ位置に並ばなくてはならない a i は b i よりも後、または同じ位置に並ばなくてはならない a i は b i より先で...
[ { "submission_id": "aoj_0304_5931301", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(long long i=0;i<(long long)(n);i++)\n#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)\n#define pb emplace_back\n#define lb(v,k) (lower_bound(all(v),(k))-v.begin())\n#defin...
aoj_0301_cpp
バトンリレーゲーム アカベ高校では、毎年全校生徒が参加するゲームを行っています。まず、校庭に N 人の全校生徒が円形に並びます。図のように、各生徒は 0 から N -1 までの番号が書かれたゼッケンを付けています。 ゲームではバトンを1本使い、最初はゼッケン 0 番の生徒がバトンを持っています。そこから、以下の手順を M 回繰り返します。まず、現時点でバトンを持っている生徒が適当な正の整数 a を宣言します。 a が偶数のときは時計回り、奇数のときは反時計回りに隣の生徒にバトンを渡していき、 a 番目にバトンを受け取った生徒が脱落します。脱落した生徒は、時計回りで隣の生徒にバトンを渡し、円から抜けます。 ゲームが終わった後に円に残っ...
[ { "submission_id": "aoj_0301_10876569", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0313_cpp
未知の病原菌 英世博士は未知の病原菌を発見しました。この病原菌は、アクダマキンとゼンダマキンと呼ばれる二種類の菌が、一直線に連なった鎖状の構造をしています。人類のために、この病原菌を無害化したいと考えています。 この病原菌は、長さが2以下になると力が弱まり、免疫力によって無害化されることが分かっています。英世博士は、この病原菌を任意の場所で切断して、前半と後半の2つの鎖にすることができます。また、2つの鎖を連結して1つの鎖にすることもできます。 しかし注意しなければいけないのは、アクダマキンの数が多い鎖はきわめて有害だということです。ある鎖においてアクダマキンの数がゼンダマキンの数よりも多くなると、その瞬間アクダマキンは無制限に増殖...
[ { "submission_id": "aoj_0313_10205416", "code_snippet": "// AOJ #313\n// Unknown Germ 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Chain {\n int id;\n string s;\n};\n \nbool isSafe(const string &s) {\n int countO = 0, countX = 0;\n for(char c : s) {\n if(c=='o') c...
aoj_0306_cpp
対称3進数 1 グラム、3 グラム、9 グラム、27 グラムのおもりが1つずつあれば、天びんを使って 1 グラムから 40グラムまで 1 グラム刻みで量れることが知られています。たとえば、天びんの一方の皿に重さを量りたいものと 3 グラムのおもりを載せ、もう一方の皿に 27 グラムと 1 グラムのおもりを載せて釣り合えば、量りたいものの重さは 27-3+1=25 グラムだとわかります。 さらに、1(=3 0 )グラム、3 1 グラム、... 、3 n-1 グラム、3 n グラムまでのおもりが1つずつあれば、天びんを使って(3 n+1 -1)/2グラムまで量れることが知られています。また、天びんが釣り合うようなおもりの置き方は一通りし...
[ { "submission_id": "aoj_0306_8406601", "code_snippet": "#include <algorithm>\n#include <array>\n#include <iostream>\n#include <stack>\n#include <string>\n#include <vector>\n\n\nnamespace {\n const std::array<int, 12> digits{\n 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147 };\n std...
aoj_0310_cpp
枠 画像の中から有益な情報を抽出する画像認識はコンピュータサイエンスの中でも重要な研究テーマのひとつです。デジタルカメラ、運転支援システム、防犯システムなどに幅広く応用されています。 このような研究のおかげで、私たちは画像解析を行うための多くのソフトウェアやプログラム集を使い様々な処理を行うことができます。一方、自力でプログラムを書いて解析することで、その仕組みを知り、楽しい時間を過ごすことができます。ここでは、一風変わった画像認識をしてみましょう。 画像として次のような各ピクセルが整数の値を持つ N × N のピクセルが入力として与えられます。この画像の中から、線の太さが1ピクセルの長方形の枠(わく)を1つ抽出します。 枠が覆うピ...
[ { "submission_id": "aoj_0310_11026928", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N;\n\tcin >> N;\n\tvector P(N, v...
aoj_0311_cpp
かぐや いよいよ今月末に「はやぶさ2」が打ち上げられます。4年前に「はやぶさ」が帰ってきたときは、日本中が盛り上がったのを覚えている人も多いと思います。7年前には、「かぐや」が打ち上げられ、月の周りを回りながらたくさんの鮮明な画像を地球に送ってくれました。 上の図は、地球を原点とした空間座標(z軸は紙面から垂直に下から上に向いているものとします)に、月の軌道といくつかの月の位置、月の周りを回るかぐやの軌道を描いたものです。月の軌道は、x軸とy軸を通る平面上にある、原点を中心とする円とします。月の周りを回るかぐやの軌道は、x軸とz軸を通る平面に平行な平面上にある円とし、その中心は月の中心と一致します。月は、その軌道に沿って描かれた矢印...
[ { "submission_id": "aoj_0311_8296988", "code_snippet": "#include <cmath>\n#include <iostream>\nusing namespace std;\n\nuint64_t seed = 1234567891234567891;\nuint64_t xorshift64() {\n\tseed ^= seed << 13;\n\tseed ^= seed >> 7;\n\tseed ^= seed << 17;\n\treturn seed;\n}\nfloat rand_float() {\n\treturn xorshift...
aoj_0312_cpp
ネットカフェ あなたはネットカフェを経営しています。今日あなたは、顧客に指摘され続けている問題を解決しようと取り組んでいます。その問題とは、店舗の本棚の単行本が巻数順に並んでおらず、目的の単行本を探しだすのが面倒だという苦情です。 あなたの店舗で一番巻数の多い単行本は「名探偵 赤ベコ」(通称「赤ベコ」)です。あまりに長編なので、特別な本棚を「赤ベコ」のために用意しました。 単行本の各巻の重さと厚さは様々で、本棚の各段の幅と、各段に並べることができる本の重さの上限も様々です。あなたは、次の条件を満足するように本棚に本を並べることにしました。 1 巻からある巻までの「赤ベコ」が本棚に並んでいる。 それぞれの段には、巻数順に(途中で抜けて...
[ { "submission_id": "aoj_0312_11026966", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint M, N;\n\tcin >> M >> N;\n\tvecto...
aoj_0302_cpp
天体観測 ある晴れた夜の帰り道、空を見上げるとそこには無数の星。強く輝く星々、かすかに見える星々、いろ いろな明るさの星々が夜空を彩っています。 あなたはふと思いました。この星空に自分の星座があったらどんなに素敵だろうと。そこであなたはひとつの基準を決め、部屋の窓から見える星々を適当につなげて自分の星座を作ることにしました。その基準とは、「どの2つの星を選んでも、それらの明るさの差がある定数 d 以下になるような星のグループを作り、その中で一番大きいものを自分の星座にしよう!」というものです。例えば、図のような窓から見える夜空を考えてみましょう(外側の長方形は窓枠です)。 この夜空には、明るさがそれぞれ 1,12, 2,4,1,8,...
[ { "submission_id": "aoj_0302_10876657", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0314_cpp
アカベコ国王の配慮 アカベコ国の国王には2人の王子がいます。国王は自分が退位するときに国を2つに分割し、それぞれの王子に一つずつ国を治めさせることにしました。新しい国の名前はアカ国とベコ国です。アカベコ国には N 個の町と、2つの町を繋ぐ M 本の道があります。国王は、以下の手順でアカベコ国の町と一部の道を2つの国に配分することにしました。 (1)   町を2つ選び、それぞれアカ国とベコ国に配分する。 (2)   すでに配分された町sを選ぶ。さらに、町 s から1本の道で繋がっている、まだ配分されていない町 t を選ぶ。そして、町 s 、 t 間の道と町 t を、町 s が配分された国に配分する。 (3)   (2)を、行えなくなる...
[ { "submission_id": "aoj_0314_10454075", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1e9 + 7;\n\nll modinv(ll a) {\n ll res = 1, b = MOD - 2;\n while (b > 0) {\n if (b & 1) res = res * a % MOD;\n a = a * a % MOD;\n b >>= 1...
aoj_0322_cpp
Slates Numerous stone slates have been discovered in the ruins of the ancient nation of Iwashiro. Researchers have found that each slate has a single word inscribed on it. However, due to weathering over the years, some of the slates seem to be difficult to decipher for the following reasons. In some cases, only one of...
[ { "submission_id": "aoj_0322_10958576", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\...
aoj_0321_cpp
Investigation of Club Activities Akira, the student council president of A High School, decided to investigate which club activities the A High School students belong to. A High School has N students numbered from 1 to N and M types of club activities numbered from 1 to M . There is no limit to the number of students ...
[ { "submission_id": "aoj_0321_10884265", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\n\n// Union-Find\nstruct UnionFind {\n vector<int> par, rank, siz;\n\n // 構造体の初期化\n Unio...
aoj_0320_cpp
Quality Management The fabric coasters produced and sold by Aizu Takada City are known for their symmetrical design and beauty. As part of quality control, Aizu Takada has installed cameras on the production line to automatically verify that the images obtained from each coaster are symmetrical. Each coaster is represe...
[ { "submission_id": "aoj_0320_10884337", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int INF = 1 << 30;\n\nint C, n;\nvector<vector<int>> p;\nint check(int x, int y) {\n if (p[x][y] == p[x][n-y-1] && p[x][y] == p[n-x-1][y] && p[x][y] == p[n-x-1][n-y-1]) ...
aoj_0323_cpp
Ruins The Aizu Archaeological Society has set out to investigate the ruin of the ancient nation of Iwashiro, which is submerged in the Hibarra Sea. The ruin exists somewhere in the Hibara Sea. We decided to use radar to get a rough idea of the location of the ruin from the shoreline, and to estimate how many meters awa...
[ { "submission_id": "aoj_0323_10873523", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0324_cpp
Downhill Race You are participating in a ski competition held at Mt. Bandai. In this competition, each skier will ski down the slope twice and compete for the shortest total time. There are a number of flags on the slope, and a line is set up between them for the competitors to follow. Competitors follow the lines from...
[ { "submission_id": "aoj_0324_10852452", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0326_cpp
関連商品 インターネット通販サイトでは、ユーザが現在見ている商品と同じページに、過去に他のユーザによって、現在見ている商品と一緒に買われた別の商品をいくつか表示してくれます。関連性の高いと思われる商品を提示することで、売り上げを伸ばすことができると考えられているからです。 似たようなことは、一緒に買われることが多い商品を近くに配置する、という工夫として、近所のスーパーマーケットでも目にすることができます(例えば、パンとジャムのような)。あなたの仕事は、商品配置の工夫を助けるプログラムを書くことです。今回は、ある基準となる回数を設定し、一緒に買われた回数が基準回数以上である、2つの商品の組み合わせを求めたいと思います。 一緒に買われた...
[ { "submission_id": "aoj_0326_9447550", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\nWA on test 3\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nstatic char ...
aoj_0327_cpp
虫食い算 足し算は筆算で簡単に計算できますが、もし、いくつかの数字が欠けていたら、欠けている数字を埋めるのは簡単でしょうか? 例えば、以下のような筆算において、1から9の数字が一度しか現れないという条件があるとき、CとEのマスに入る数字はいくつになるでしょう? この場合、Cに入るのは8,Eに入るのは5が正解となります。このように、数字がいくつか欠けている演算を虫食い算と呼びます。 1から9の数字が一度しか現れないという条件はそのままで、以下のようにもっとたくさんの数字が欠けていたら、正解となる数字の埋め方は一通りしかないのでしょうか? 実は、必ず一通りに決まるとは限りません。 上の図のような形をした虫食い算の、AからIの各マスの情報...
[ { "submission_id": "aoj_0327_11004401", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, l, n) for(int i = int(l); i < int(n); i++)\n#define ll long long\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T...
aoj_0328_cpp
貴金属リサイクル 会津特産の貴金属であるアイヅニウムをリサイクルするPCK社は、全国各地にネットワークを持ち、たくさんの回収車でアイヅニウムを集めてきます。この会社は、処理の効率化のために、塊の重さと個数の単位を規格で定めています。 塊の重さには「ボッコ」という単位を使います。 x ボッコのアイヅニウムの重さは 2 x グラムです。宝石で例えると、「カラット」のようなものです。また、塊の個数には「マルグ」という単位を使います。 y マルグは 2 y 個です。1箱に入っている品物の個数である「ダース」のようなものです。ただし、 x と y は 0 以上の整数でなければいけません。 回収車 i は、 a i ボッコの重さのアイヅニウムを...
[ { "submission_id": "aoj_0328_10945978", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\ntypedef long long ll;\ntypedef long dou...
aoj_0329_cpp
完全平等二国間貿易 サイバースペースにあるアイヅ国はワカマツ国と情報貿易を行っています。2つの国はお互いに有用なデータを交換することで経済発展を遂げています。博愛と平等、そして何よりも会津地方の古い言葉である、「ならぬことはならぬものです」を国是とする両国は、定期的に貿易状況の調査を行っています。 調査では、バイト単位でアイヅ国から見たデータ流入量から流出量を引いた値を、1ナノ秒ごとに求めた表が与えられます。その表から、値の総和が0になる最長の区間を見つけます。この区間が長いほど、平等性が保たれていると判断します。 貿易状況が記録された表が与えられたとき、値の総和が0になる最長の区間の長さを求めるプログラムを作成せよ。 Input ...
[ { "submission_id": "aoj_0329_11030754", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N;\n\tcin >> N;\n\tvector<ll> d(...
aoj_0333_cpp
線分配置 A大学は今年もプログラミングコンテストを開催する。作題チームの一員であるあなたは、計算幾何学の問題の入力データの作成を担当することになった。あなたが作りたい入力データは、 x 軸または y 軸に平行で、互いに触れ合うことのない線分の集合である。あなたは、次のアルゴリズムに基づいたデータ生成プログラムを開発して、入力データを生成する。 xy 平面上の線分の集合 T を空にする。 次の処理を N 回繰り返す。 x 軸または y 軸に平行な適当な線分 s を作る。 s が T 内のどの線分にも触れない場合は s を T に追加し、触れる場合は s を追加しない。 x 軸または y 軸に平行な N 本の線分を順番に入力し、各線分が...
[ { "submission_id": "aoj_0333_10203845", "code_snippet": "// AOJ #333\n// Line Segment Arrangement 2025.2.8\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\...
aoj_0331_cpp
スケジューラ あなたはユニークなオペレーティングシステム「ウンズグネ15」の開発に取り組んでおり、性能を決定付けるスケジューラの設計に頭を悩ませている。スケジューラとは、実行すべき処理をタスクという単位で表現し、それらをどの順序で実行するかを決定するプログラムである。スケジューラはタスクに1から N の番号をつけて管理する。全てのタスクは K 個の属性 f 1 , f 2 ,..., f K を持ち、各属性にはそれぞれ固有の値が設定されている。ただし、ある2つのタスクについて、対応する属性の値すべてが同じになることはない。 あるタスクには、そのタスクの実行を始める前までに実行を完了していなければならないタスクが与えられることがある。...
[ { "submission_id": "aoj_0331_11030956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N, K;\n\tcin >> N >> K;\n\tint f...
aoj_0332_cpp
消える数列、消えない数列 ただお君は頭の体操をするために、数列を使ったゲームをしています。このゲームでは、はじめに、1から9までの数字がランダムに並んだ列が与えられます。ただお君は、数列からその一部分を消していきます。ルールは、以下の通りです。 数列から、同じ数字が2つ以上並んでいる部分を適当に選ぶ。その部分を含み、連続して現れている同じ数字をすべて消す。 消した部分の右側に数列が残っていた場合は、それを左に詰めて、数列を1つにまとめる。 上の2つの操作を繰り返した結果、すべての数字が消えればゲームクリアとなる。 例えば、上の図のような 1,2,3,3,2,2,1,2,2 という数列の場合、 左から数えて、3番目、4番目の3を消すと...
[ { "submission_id": "aoj_0332_9099687", "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) { return (ull)rn...
aoj_0334_cpp
あみだくじ PCK 君はみんなでゲーム大会をしています。このゲーム大会では、大会の最後にあみだくじで順位を入れ替えます。大会には N 人のプレイヤーが参加しており、あみだくじには N 本の縦棒があります。 あみだくじは、図のように N - 1 段の部品からできており、それぞれ 1 から N -1 の番号が割り当てられています。各部品は、あみだくじの一部を横方向に切り取った部分です。各部品にはいくつかの横棒が引かれていますが、部品の中の横棒はすべて同じ高さにあります。横棒同士がつながることはありません。 大会の最後に、順位の高い人から右から左の順に縦棒が割り当てられます。PCK 君は現時点で最下位なので、左端からスタートです。例えば、...
[ { "submission_id": "aoj_0334_10853299", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <cstring>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#incl...
aoj_0330_cpp
プログラム停止判定 皆さんは、苦労して作ったプログラムを実行してみたら、無限ループになってしまった経験はありませんか? プログラムの実行が停止するかどうかを、実行しなくても事前に判定できると便利ですよね。 残念ながら、皆さんがふだん使っているプログラミング言語では、あらゆるプログラムに対してそのような判定をすることは不可能です。しかし、それよりもはるかに計算能力の低いプログラミング言語なら、その言語で書いたプログラムが停止するかどうかを判定するプログラムを書ける場合があります。 TinyPowerというプログラミング言語を考えます。この言語のプログラムは行の並びです。プログラムの各行には、先頭に行番号を書き、その後ろに文を一つ書きま...
[ { "submission_id": "aoj_0330_10946325", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <cstring>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#incl...
aoj_0350_cpp
実数既約分数化 実数のうち、小数部が循環するものと有限桁のものは分数として表すことができます。 分数で表すことができる実数が与えられたとき、その実数と等しい既約分数(それ以上約分できない分数)を出力するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 str 1行に、変換したい実数を表す文字列 str が与えられる。実数の値は 0 より大きい。文字列は数字か「.」、「(」、「)」を含む、長さが 3 以上 8 以下の文字列である。「.」は小数点、「(」は数字の循環の始まり、「)」は数字の循環の終わりを示す。整数部にも小数部にも、必ず1桁以上の数字が与えられるとする。ただし、循環小数が与えられた場合、文字列は以下の条件...
[ { "submission_id": "aoj_0350_3592199", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n#include <climits>\n#include <vector>\n#include <map>\n#include <set>\n#include <list>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <iostream>\n#include...
aoj_0348_cpp
必勝7並べ トランプを使ったゲームに「7並べ」があります。ここではそれを簡単にしたゲームを考えます。1から13の番号がそれぞれ書かれた13枚のカードを使って7並べをします。対戦は、2者だけで次のようにゲームを進めます。 「場」に7のカードを置きます。 2者には、残りのカードがランダムに6枚ずつ配布されます。 先手の手持ちのカードのうち、場にあるカードの番号と連続する番号のカードがあれば、そのうちの1枚を場に置きます。プレイヤーはカードが置ける場合には必ず置かなければいけません。無いときに限り、カードを出さずに相手の番になります。 後手も同じ要領で、手持ちのカードを場に置きます。 手順3と4を繰り返して、一方の手持ちのカードがなくなる...
[ { "submission_id": "aoj_0348_3683470", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\nusing namespace std;\n\nbool judge(vector<int> A, vector<int> B){\n do{\n int i = 0, j = 0;\n int mi = 7, ma = 7;\n while(i < 6 and j < 6){\n if(A[i] == mi-1 o...
aoj_0343_cpp
Programming Contest II Every year, Byakko University holds a programming contest. The contest starts with all teams having zero points, and points are added according to their answers. In this contest, the teams are ranked in order of their scores. When the total number of teams is N , each team is assigned a number fr...
[ { "submission_id": "aoj_0343_10867159", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0341_cpp
Repeated Spell After discovering and investigating the ancient nation of Iwashiro, our researchers finally found a temple in the center of Iwashiro. The temple housed a stone tablet dedicated to the god of Iwashiro. On the tablet, there are two strings of text, one sentence and one spell. In Iashiro, The number of time...
[ { "submission_id": "aoj_0341_9605582", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nint mod=1000000007;\n\nint dp[1009][1009];\n\nsigned main(){\n\tdp[0][0]=1;\n\tstring s,t;cin>>s>>t;\n\tint a=s.size(),b=t.size();\n\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<=...
aoj_0347_cpp
有理式最大化 N 個の異なる自然数が与えられる。その中から異なる4つを選んで、それらを $A$, $B$, $C$, $D$ としたとき、次の数式 $\frac{A + B}{C - D}$ の最大値を求めたい。 N 個の異なる自然数が与えられたとき、その中から異なる4つを選んで、上の数式の最大値を求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N a 1 a 2 ... a N 1行目に自然数の個数 N (4 ≤ N ≤ 1000) が与えられる。2行目に各自然数の値 a i (1 ≤ a i ≤ 10 8 ) が与えられる。ただし、同じ自然数が重複して現れることはない( i ≠ j について a i ≠...
[ { "submission_id": "aoj_0347_11017140", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int n;\n cin>>n;\n vector<int> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n sort(s.begin(),s.end());\n multiset<int> st;\n for(int i=0;i<n;i++) s...
aoj_0344_cpp
Game Strategy You have found an old board game in the club room of your programming club. It looks interesting, so you decide to play it. The game consists of M events, and you have to capture event i at time t i . However, your strength must be at least s i at that time. Otherwise, the game is over. At the beginning o...
[ { "submission_id": "aoj_0344_10862866", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first;\n#define se second;\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntemp...
aoj_0349_cpp
環状すごろく ユキさんは、子供会の催しでみんなで遊べるようにすごろくを作りました。このすごろくでは、環状にマスが並んでいて、それぞれのマスには1以上の整数が書いてあります。 プレイヤーは出発点としてどこかのマスを選んで自分の駒を置きます。そのマスに書いてある数だけ、時計回りに駒を進めます。止まったマスに書いてある数だけ、再び時計回りに駒を進めます。これを繰り返して、出発点に選んだマスの上で駒が止まったら「あがり」です。 実際には、マスの選び方によっては絶対に「あがり」にならない場合もあります。ユキさんは、このすごろくで「あがり」にたどり着けるマスの個数を数えようとしています。 すごろくの情報を入力とし、「あがり」にたどり着けるマスの...
[ { "submission_id": "aoj_0349_11030870", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define ll long long\n#define ld long double\n#define vec vector\n#define vi vec<int>\n#define vll vec<ll>\n#define vld vec<ld>\n#define pq priority_queue\n#define rep(i, l, r) fo...
aoj_0351_cpp
静かな町 アイヅ国では、毎年、駅伝大会が開催されています。アイヅ国には N 個の町が点在しており、それぞれ 1 から N までの番号が付いています。いくつかの町の間は、互いに直接行き来できる道でつながっています。また、どの町の間もいくつかの道を辿って行き来ができます。大会のコースは、次のようにして決めます。 2つの町のすべての組み合わせについて、最短経路の距離を求める。 それらの中で、最短経路の距離が最大になるような2つの町を、スタートの町とゴールの町とする。町の組み合わせが複数ある場合、そのうちのどれか一つを選ぶ。 選ばれたスタートの町とゴールの町の間の最短経路を大会のコースとする。最短経路が複数ある場合、そのうちのどれか一つを選...
[ { "submission_id": "aoj_0351_11017195", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int N,M;\n cin>>N>>M;\n vector<vector<pair<int,int>>> G(N);\n for(int i=0;i<M;i++){\n int a,b,c;\n cin>>a>>b>>c;\n a--;\n b...
aoj_0360_cpp
Reservation System The supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation periods are a...
[ { "submission_id": "aoj_0360_9615818", "code_snippet": "#include<bits/stdc++.h>\n#include <iostream>\nusing namespace std;\nint main(void){\n\tint a,b,n,s[101],f[101],cnt=0;\n\tcin>>a>>b>>n;\n\tfor(int i=0;i<n;i++){\n\t\tcin>>s[i];\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tcin>>f[i];\n\t}\n\tfor(int i=a+1;i<b;i++)...
aoj_0352_cpp
勢力の予想 信夫くんと静夫くんは長方形の島で領地を取り合うゲームをしています。下の図①のように、島全体は格子状に区切られた正方形の区画でできており、区画にはそこから生じる損得が整数で示されています。 このゲームでは1つの駒を動かして領地の境界線を決めていきます。ゲーム開始時、駒は島の北西端にあります(図①)。この駒を島の南東端まで動かしていったときの駒の軌跡が領地の境界線となります。二人のプレーヤーは交互に駒を動かします。駒は南隣の格子点か東隣の格子点にのみ動かすことができます(図②)。駒が島の端に到達した場合は、南か東のうち動かせる方向に動かします。駒が南東端に到達したらゲーム終了です。 ゲーム終了後の境界線から北東側の領域が先攻...
[ { "submission_id": "aoj_0352_11017206", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int H,W;\n cin>>W>>H;\n vector<vector<int>> A(H,vector<int>(W));\n vector<vector<int>> R(H+1,vector<int>(W));\n for(int i=0;i<H;i++)for(int j=0;j<W;...
aoj_0356_cpp
夕暮れ AIZU 国の夕暮れ時は、スマートフォンを西の空へ構えて立ち止まる観光客で賑わう。AIZU 国は無数のビルが建ち並ぶ大都市であり、ビルの谷間が長く連なる西の空には、ビルのシルエットとそれらから漏れ出す太陽の光による絶景が広がる。 AIZU 観光協会の若松氏によると、太陽を表す円が、その面積のちょうど半分だけ遮られているとき、格段の絶景になるという。 図のように、西の空を、地平線を x 軸、太陽の中心の軌跡を y 軸とするような x - y 平面で表し、太陽を半径 R の円、それぞれのビルのシルエットを長方形とする。 各ビルのシルエットの底辺は x 軸上にあり、太陽は十分高い位置から地平線に対して垂直に沈んでいく。太陽はビルの...
[ { "submission_id": "aoj_0356_10203506", "code_snippet": "// AOJ #356\n// Evening 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 1e-10;\nconst double PI = acos(-1.0);\n \ndouble groundIntersection(double Y, double R) {\n double circleArea = PI * R * R;\n if(Y >= R) retur...
aoj_0354_cpp
蟻 大きなチェス盤上に、それぞれ1から N までの番号が割り振られた N 匹の蟻がいます。図のように、チェス盤は H×W のマスからなる長方形で、北西角を白として、白マスと黒マスが交互に並んでいます。 最初、どの蟻もチェス盤のマスの中にいて、東または南を向いています。1つのマスの中に2匹以上の蟻がいることはありません。 今、蟻たちが一斉に動き出します。すべての蟻は 1 単位時間に向いている方向のマスに1つ移動します。ただし、移動先がチェス盤の外の場合、落下してチェス盤から姿を消します。 チェス盤上で2匹の蟻が同じマスに入ると、それらの蟻は以下のような行動をとります: そのマスの色が白ならば、東方向に進んでいた蟻は南方向へ、南方向に進...
[ { "submission_id": "aoj_0354_10865835", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define INF 2e+9\n#define mp make_pair\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\n\nint w,h,n,x[200000],y[200000],ans[200000];\nstring d[200000];\nvector<int> vec[200000...
aoj_0353_cpp
ソート 高校入学後、プログラミング部に入部したタケ子さんは、次第にアルゴリズムの面白さにのめりこんでいきました。いまでは、2年生になったらプログラミング甲子園に出場してみたいと考えています。 あるとき、ソート・アルゴリズムについて学んだタケ子さんは、ソート・アルゴリズムを自分で設計してみました。タケ子さんが作ったソート・アルゴリズムでは、入力として要素の間に重複のない、1個以上の自然数からなる列が与えられたとき、以下の処理を実行します。 はじめに、列の先頭の要素を選ぶ。 選んだ要素の直前に要素があるとき、選んだ要素とその直前の要素を比べる。直前の要素のほうが大きいなら、それを列の末尾の直後に移動させる(図)。この操作を、選んだ要素が...
[ { "submission_id": "aoj_0353_11019202", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <deque>\nusing namespace std;\n\nint find_parent(vector<int>& parent, int x) {\n if (parent[x] < 0) return x;\n return parent[x] = find_parent(parent, parent[x]);\n}\n\nvoid ...
aoj_0362_cpp
Trampoline A plurality of trampolines are arranged in a line at 10 m intervals. Each trampoline has its own maximum horizontal distance within which the jumper can jump safely. Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range. The jumper wants to repeat jum...
[ { "submission_id": "aoj_0362_10873479", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first\n#define se second\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntempla...
aoj_0355_cpp
文字列ゲーム あなたは、双子のアイ君とヅー君に文字列を使ったゲームのプログラムをプレゼントしました。このゲームでは、アイ君とヅー君が文字列の中から部分文字列をそれぞれ選び、それらを比較して小さい方を選んだ人に得点が加算されます。二人は競い合い、何度もゲームを行いました。ところが、同じ文字列に対して何度もゲームをしているうちに飽きてしまいました。そこであなたは、文字列が変化するようにプログラムを修正することにしました。 長さ N の文字列 U と Q 個の命令文が与えられたとき、以下の命令を処理するプログラムを作成せよ。 文字列 U の指定された範囲にあるすべての文字を、指定された文字に置き換える。 文字列 U の指定された2つの部分...
[ { "submission_id": "aoj_0355_10867757", "code_snippet": "#include <bits/stdc++.h>\n#define N 500500\n\ntypedef long long ll;\nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n\nchar c[N * 4];\n\nint mult(ll x, ll y) {\n return (x * y) % MOD;\n}\n\nint sm(ll x, ll y) {\n return (x + y) % MOD;\n}\n\nin...
aoj_0370_cpp
Age Difference A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference ...
[ { "submission_id": "aoj_0370_3737126", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include ...
aoj_0363_cpp
Loading Aizu Ocean Transport Company (AOTC) accepted a new shipping order. The pieces of the cargo included in this order all have the same square footprint (2m x 2m). The cargo room has a rectangular storage space of 4m width with various longitudinal extents. Each pieces of cargo must be placed in alignment with part...
[ { "submission_id": "aoj_0363_10866776", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,N) for(int i=0;i<(ll)(N);i++)\nll gcd(ll a,ll b){\n if(b==0)return a;\n return gcd(b,a%b);\n}\nint main(){\n ll H,N;cin>>H>>N;\n vector<vector<int>>G(4,vect...
aoj_0366_cpp
Road Improvement Aizu is a country famous for its rich tourism resources and has N cities, each of which is uniquely identified with a number (0 to N -1). It has a road network consisting of M one-way roads connecting a city to another. All the roads connecting the cities in Aizu have a row of cherry trees along their ...
[ { "submission_id": "aoj_0366_10873655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first\n#define se second\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntempla...
aoj_0365_cpp
Swapping Characters You are given a string and a number k . You are suggested to generate new strings by swapping any adjacent pair of characters in the string up to k times. Write a program to report the lexicographically smallest string among them. Input The input is given in the following format. s k The first line ...
[ { "submission_id": "aoj_0365_11011873", "code_snippet": "// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG//[]で配列外参照をするとエラーにしてくれる。上下のやつがないとTLEになるので注意 ABC311Eのサンプル4みたいなデバック中のTLEは防げないので注意\n// #endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\n\n\...
aoj_0364_cpp
Dungeon Bob is playing a popular game called "Dungeon". The game is played on a rectangular board consisting of W × H squares. Each square is identified with its column and row number, thus the square located in the x -th column and the y -th row is represented as ( x , y ). The left-most square in the top row is (0, ...
[ { "submission_id": "aoj_0364_10866790", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,N) for(int i=0;i<(ll)(N);i++)\n#define fi first \n#define se second\n#define all(X) (X).begin(),(X).end()\nll gcd(ll a,ll b){\n if(b==0)return a;\n return gcd(b,a...
aoj_0375_cpp
Lake Survery The Onogawa Expedition is planning to conduct a survey of the Aizu nature reserve. The expedition planner wants to take the shortest possible route from the start to end point of the survey, while the expedition has to go around the coast of the Lake of Onogawa en route. The expedition walks along the coas...
[ { "submission_id": "aoj_0375_3240241", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iomanip>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0), y(0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(y_) {};\n\tbool...
aoj_0371_cpp
Electric Metronome A boy named PCK is playing with N electric metronomes. The i -th metronome is set to tick every t_i seconds. He started all of them simultaneously. He noticed that, even though each metronome has its own ticking interval, all of them tick simultaneously from time to time in certain intervals. To expl...
[ { "submission_id": "aoj_0371_11026002", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int N;\n cin>>N;\n vector<int> A(N);\n for(int i=0;i<N;i++) cin>>A[i];\n sort(A.begin(),A.end());\n int ans = 0;\n int now = 1;\n for(int x...
aoj_0374_cpp
Paper Fortune If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written. Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take...
[ { "submission_id": "aoj_0374_10874359", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, l, n) for(int i = int(l); i < int(n); i++)\n#define ll long long\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T...
aoj_0373_cpp
Checkered Pattern You have a cross-section paper with W x H squares, and each of them is painted either in white or black. You want to re-arrange the squares into a neat checkered pattern, in which black and white squares are arranged alternately both in horizontal and vertical directions (the figure shown below is a c...
[ { "submission_id": "aoj_0373_11022526", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <iomanip>\n#include <numeric>\n#include <queue>\n#include <stack>\nusing namespace std;\nusing ll = long long;\nint main() {\n\tint W, H;cin >> W >>...
aoj_0376_cpp
Lottery Box A lottery is being held in a corner of the venue of the Aizu festival. Several types of balls are inside the lottery box and each type has its unique integer printed on the surfaces of the balls. An integer T is printed on the lottery box. In the lottery, you first declare two integers A and B , and draw up...
[ { "submission_id": "aoj_0376_10203930", "code_snippet": "// AOJ #376\n// Lottery Box 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\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 ...
aoj_0377_cpp
Party The students in a class in Akabe high-school define the relation “acquaintance” as: If A and B are friends, then A is acquainted with B. If A and B are friends and B is acquainted with C, then A is acquainted with C. They define the relation “companion” as: Suppose A is acquainted with B, and two classmates who h...
[ { "submission_id": "aoj_0377_10205417", "code_snippet": "// AOJ #377\n// Party 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\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()...
aoj_0379_cpp
Iron Bars A boy PCK had N straight iron bars, which were serially indexed. Unfortunately, the first M bars (0 ≤ M ≤ N ) among them were bent during transportation. They all suffered a perpendicular bend at one point. He is planning to make a cube using a set of bars selected using the following rules: X bars from bent ...
[ { "submission_id": "aoj_0379_10205452", "code_snippet": "// AOJ #379\n// Iron Bars 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\nstruct FastBitset {\n vector<ull> data;\n int size;\n FastBitset() {}\n FastBitset(int n) : siz...
aoj_0378_cpp
Aerial Photos Hideyo has come by two aerial photos of the same scale and orientation. You can see various types of buildings, but some areas are hidden by clouds. Apparently, they are of the same area, and the area covered by the second photograph falls entirely within the first. However, because they were taken at dif...
[ { "submission_id": "aoj_0378_10205430", "code_snippet": "// AOJ #378\n// Aerial Photo 2025.2.9\n\n#include <iostream>\n#include <vector>\n#include <string>\n#include <emmintrin.h> // SSE2 ヘッダ\nusing namespace std;\nusing ll = long long;\n\nstatic inline bool checkRowSSE(const char* a, const char* b, int le...
aoj_0367_cpp
Charging System for Network There is a network consisting of N machines (sequentially numbered from 0 to N -1) interconnected through N -1 bidirectional communication cables. Any two machines can perform bidirectional communication through one or more cables. From time to time, machines in the network are renewed. When...
[ { "submission_id": "aoj_0367_10423002", "code_snippet": "// AOJ #0367 Charging System for Network\n// 2025.4.26\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\nconst int NMAX = 200000;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c...
aoj_0385_cpp
Bozosort Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below: Randomly select two elements and swap them. Verify if all the elements are sorted in increasing order. Finish if sorted, else return to 1. To ana...
[ { "submission_id": "aoj_0385_10874892", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)\n#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)\n#define rrep2(i, a, b) for (int i = (int)(b)...
aoj_0372_cpp
Three Meals You are running a restaurant that serves dishes only three times a day. Each of your customer has his/her own separate timeframes for eating breakfast, lunch and supper. Thus, your customer enjoys your dish only when he/she visits your restaurant to meet your serving time settings. Your wish is to modify yo...
[ { "submission_id": "aoj_0372_11026034", "code_snippet": "#pragma GCC target(\"avx2\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint N;\nint A[50], B[50], C[50], D[50], E[50], F[50];\nsigned main(){\n set<int> st;\n cin>>N;\n for(int i=0;i<N;i++){\n int a,b;\n ...
aoj_0386_cpp
Transporter In the year 30XX, an expedition team reached a planet and found a warp machine suggesting the existence of a mysterious supercivilization. When you go through one of its entrance gates, you can instantaneously move to the exit irrespective of how far away it is. You can move even to the end of the universe ...
[ { "submission_id": "aoj_0386_9202923", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n//#include<atcoder/all>\n//using namespace atcoder;\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++)\nusing ll = long long;\nusing ull = unsigned long long;\nusing ...
aoj_0387_cpp
Taxi PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station. In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running f...
[ { "submission_id": "aoj_0387_10876661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n\tlong long n,m,c,i,j,sum=0;\n\tpriority_queue<int,vector<int>,greater<int>> que;\n\tcin>>n;\n\tfor(i=0;i<n;i++){\n\t\tcin>>m;\n\t\tfor(j=0;j<m;j++){\n\t\t\tcin>>c;\n\t\t\tque.push(c);\n...
aoj_0388_cpp
Points on a Straight Line The university of A stages a programming contest this year as has been the case in the past. As a member of the team in charge of devising the problems, you have worked out a set of input data for a problem, which is an arrangement of points on a 2D plane in the coordinate system. The problem ...
[ { "submission_id": "aoj_0388_10874948", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint f(int a, int b){\n if(a < b) swap(a,b);\n int x,y;\n if(b == 0){\n return a;\n }\n else{\n return f(b,a%b);\n }\n}\nsigned main(){\n int N,K;...
aoj_0390_cpp
Disc A mysterious device was discovered in an ancient ruin. The device consists of a disc with integers inscribed on both sides, a bundle of cards with an integer written on each of them and a pedestal within which the cards are placed. The disc rotates when the bundle of cards is put into the pedestal. Only one side o...
[ { "submission_id": "aoj_0390_10875152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint K;\ntemplate < typename NodeT, typename LazyT>\nstruct SEG{\n using F = function<NodeT(const NodeT&, const NodeT&)>;\n using G = function<NodeT(const NodeT&, const LazyT&...
aoj_0384_cpp
Dudeney Number A Dudeney number is a positive integer for which the sum of its decimal digits is equal to the cube root of the number. For example, $512$ is a Dudeney number because it is the cube of $8$, which is the sum of its decimal digits ($5 + 1 + 2$). In this problem, we think of a type similar to Dudeney number...
[ { "submission_id": "aoj_0384_10872265", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing dl=double;\nusing vi=vector<int>;\nusing vpi=vector<pair<int,int>>;\n\nint main(){\n int a,n,m; cin>>a>>n>>m;\n int count=0;\n for(int i=1; i<m; i++){\n int d=i;\n int...
aoj_0394_cpp
Pilling Blocks We make a tower by stacking up blocks. The tower consists of several stages and each stage is constructed by connecting blocks horizontally. Each block is of the same weight and is tough enough to withstand the weight equivalent to up to $K$ blocks without crushing. We have to build the tower abiding by ...
[ { "submission_id": "aoj_0394_10934103", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tll N, K;\n\tcin >> N >> K;\n\t\n\tll...
aoj_0389_cpp
Dungeon 2 Bob is playing a game called "Dungeon 2" which is the sequel to the popular "Dungeon" released last year. The game is played on a map consisting of $N$ rooms and $N-1$ roads connecting them. The roads allow bidirectional traffic and the player can start his tour from any room and reach any other room by way o...
[ { "submission_id": "aoj_0389_10875058", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)\n#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)\n#define rrep2(i, a, b) for (int i = (int)(b)...
aoj_0398_cpp
Beautiful Sequence Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $...
[ { "submission_id": "aoj_0398_9703022", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int mod = 1e9 + 7;\nll fast_power(ll a , ll p) {\n if(p == 0)return 1;\n ll res = fast_power(a , p / 2);\n res = res * res % mod;\n if(p & 1)res = a % mod * res % m...
aoj_0391_cpp
Meeting in a City You are a teacher at Iazu High School is the Zuia Kingdom. There are $N$ cities and $N-1$ roads connecting them that allow you to move from one city to another by way of more than one road. Each of the roads allows bidirectional traffic and has a known length. As a part of class activities, you are pl...
[ { "submission_id": "aoj_0391_10875318", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nstruct SEG{\nprivate:\n int n;\n vector<int> node;\npublic:\n SEG(int N){\n n = 1;\n while(n < N) n *= 2;\n node.resize(2*n-1);\n }\n void updat...
aoj_0399_cpp
Payroll PCK company has $N$ salaried staff and they have specific ID numbers that run from $0$ to $N-1$. The company provides $N$ types of work that are also identified by ID numbers from $0$ to $N-1$. The time required to complete work $j$ is $t_j$. To get each of the working staff to learn as many types of work as po...
[ { "submission_id": "aoj_0399_6039851", "code_snippet": "#ifdef LOGX\n#define _GLIBCXX_DEBUG\n#endif\n#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep2(i,s,n) for(int i=int(s);i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\...
aoj_0396_cpp
Treasure Map Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which...
[ { "submission_id": "aoj_0396_10934164", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint W, H;\n\tcin >> W >> H;\n\tvecto...
aoj_0395_cpp
Round Table of Sages $N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation. If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed s...
[ { "submission_id": "aoj_0395_11026284", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nint main(){\n int N;\n std::cin >> N;\n \n std::vector<int> a(N), w(N);\n for (int i = 0; i < N; i++) {\n std::cin >> a[i];\n }\n for (int i = 0; i < N; i++) {\n...
aoj_0397_cpp
Common-Prime Sort You are now examining a unique method to sort a sequence of numbers in increasing order. The method only allows swapping of two numbers that have a common prime factor. For example, a sequence [6, 4, 2, 3, 7] can be sorted using the following steps. Step 0: 6 4 2 3 7 (given sequence) Step 1: 2 4 6 3 7...
[ { "submission_id": "aoj_0397_10994353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std; \n/*#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/rational.hpp>\nusing namespace boost::multiprecision;*/\n/*#include <atcoder/all>\nusing...