group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_1465_cpp
Scheduling Two Meetings You are the chief judge of the International Collegiate Quiz Contest (ICQC) this year. You want to hold judge meetings twice for preparing the problem set of the contest. You proposed candidate schedules for the meetings, and all the judges answered for each of the time slots whether they would ...
[ { "submission_id": "aoj_1465_11055654", "code_snippet": "#include <bits/stdc++.h>\n#include <numeric>\nusing namespace std;\n\nusing i64 = int64_t;\n\n#define rep(i, n) for(i64 i = 0; i < (n); i++)\n#define each(x, a) for(auto& x: a)\n\ntemplate<class T, T id, T (*op)(T,T)>\nstruct SegTree{\n\ti64 n=1;\n\tv...
aoj_1463_cpp
Greatest of the Greatest Common Divisors You are given a sequence of integers and a number of intervals in the sequence. The intervals are specified by their leftmost and rightmost positions. An interval consisting of $k$ integers has $k(k − 1)/2$ pairs of integers at different positions, which have their greatest comm...
[ { "submission_id": "aoj_1463_11052822", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a ...
aoj_1506_cpp
Problem G: Dice サイコロ3兄弟は三つ子であり一心同体、3つでひとつとも言ってもよい。三つ子であるため見分けがつかないほど似ている。そんなサイコロ3兄弟はA君のお気に入りのおもちゃだ。A君はいつも3つ1セットでサイコロを転がして遊んでいる。このようにA君が楽しく遊ぶおもちゃだが、実は大きな秘密があった。彼らは実は生きていて、話したり自由に動いたりできるのだ。子ども部屋は r × c の大きさのグリッドで表すことができ、とても大きいものが散乱している。 サイコロ3兄弟は室内を東西南北の4方向へ進行方向に向かって転がりながら移動する。その形ゆえに斜め移動ができないのが、少し気の毒である。進行方向にとても大きいものがある場合...
[ { "submission_id": "aoj_1506_10349794", "code_snippet": "// AOJ #1506 Dice\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 10000000;\n\nint R, C;\nchar g[25][25];\nint goalX[3], goalY[3], goalTop[3];\n\nint tableArr[6][4] = {\n {1, 3, 4, 2},\n {0, 2, 5, 3},\n {0, 4, ...
aoj_1502_cpp
Problem C : War A国とB国という2つの国が戦争をしている。A国の軍人であるあなたは n 人の兵士を率いて、B国の領土を占領する事になった。 B国の領土は2次元グリッドで表されている。あなたが最初に占領する場所は二次元グリッド上のある1マスである。 あなたが率いている兵士たちは、それぞれ h i の体力を持っている。 それぞれの兵士は体力を1を消費して移動することができる。 現在いるマスを(a,b)とすると、(a+1,b),(a-1,b),(a,b+1),(a,b-1)の4方向を移動先として選ぶことが可能である。 兵士は体力が0になったらそこから動くことができなくなる。 一人以上の兵士が通過したマスはすべて占領すること...
[ { "submission_id": "aoj_1502_7916695", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n\nusing namespace std;\nusing ll = long long;\n\nconst int MAX_N = 1e7+10;\nint main(){\n\tint N; cin >> N;\n\tvector<int> H(N);\n\trep(i, N) cin >> H[i];\n\n\tvector<ll> num(M...
aoj_1466_cpp
Peculiar Protocol The Kingdom of Icpca has a peculiar protocol in wedding ceremonies. That is, amounts of monetary gifts should be a multiple of a fixed quantity plus a fixed extra. When the fixed quantity is $d$ and the fixed extra is $r$, courteous amounts of wedding gifts are $k \times d + r$ for any non-negative in...
[ { "submission_id": "aoj_1466_10437156", "code_snippet": "// AOJ #1486 Peculiar Protocol\n// 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = (ll)1e18;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0, c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc...
aoj_1501_cpp
Problem B : Grid r × c の2次元グリッド上の2つの座標 ( a 1 , a 2 ) と ( b 1 , b 2 ) が与えられる。 あるマス (e,f) から (e+1,f)、(e-1,f)、(e,f+1)、(e,f-1) のどれかのマスに移動するコストを1とする。 また、(e,c-1) と (e,0)、(r-1,f) と (0,f) の間もコスト1で移動することができる。 この時に1つ目の座標から2つ目の座標へ最短コストで移動できる経路の数を求めよ。 Input 入力は以下のフォーマットで与えられる。 r c a 1 a 2 b 1 b 2 入力は以下の制約を満たす 1 ≤ r , c ≤ 1,000 0 ≤...
[ { "submission_id": "aoj_1501_7916754", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i =0; i<(n); i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 100000007;\nconst int inf ( 1<<30);\n\nint main(){\n\tint r,c; cin>>r>>c;\n\tvector<vector<ll>> ...
aoj_1504_cpp
Problem E : Connect r × c のグリッドが与えられる。 グリッドのいくつかのマスには1から8までの数字が書かれている。 数字が書かれているマスと他の数字が書かれているマスを線で結ぶ必要がある。 数字が書かれているマスについて、そのマスに書かれている本数だけ、他のマスとの間に線を結ぶ必要がある。 ただし、線を結べるのは上下左右方向だけで、一つの方向について、最大2本までである。 他の数字を跨いで線で結ぶことはできない。 また、次のように交差してしまう結び方をすることはできない。 グリッドが入力として与えられるので、数字が書かれているマス全てを正しく結ぶ方法が、何通りあるのかを数えて欲しい。 Input 入力は以下...
[ { "submission_id": "aoj_1504_10344187", "code_snippet": "// AOJ #1504 Connect\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 100000007;\n\nint dp[2][3][60005];\nint d[11];\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int H, W;\n...
aoj_1500_cpp
Problem A : ID A大学ではIDの入力ミスが多発していた。 そこで、A大学は入力ミス防止のため新しいIDを発行することにした。 新しいIDには入力ミス防止のためにIDが正しいかどうかチェックする方法がある。 ・全ての桁の数字の総和を求める。 ・ただし、右端の桁を1番目として、偶数番目の桁の数字を2倍にする。 ・2倍することによって数字が10以上になった時、1の位の数字と10の位の数字を加算した数字をその桁の数字とする。 ・総和が10で割り切れれば正しいID、そうでなければ間違いとする。 例として、53579というIDをチェックする。 全ての桁の数字の総和を求めるので、 5 + 3 + 5 + 7 + 9 ただし、偶数番目...
[ { "submission_id": "aoj_1500_6006661", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <stdio.h>\n#include <vector>\nusing namespace std;\nconst int maxn=100010;\n\nint a[10]={0,2,4,6,8,1,3,5,7,9};\nint choose[10];\nint sum=0,sum2=0,sum3=0;\nint ans=0;\nint n,m;\nvoid dfs2(int x,int ss)...
aoj_1505_cpp
Problem F : Dungeon (I) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、 m 本ある。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。 生成されたダンジョンではいくつかの通路を経由して、ある部屋から他のすべての部屋へ行くことが可能である。 そして、プレイヤーがゲームを行う際に、0番の部屋がスタート地点、 n-1 番の部屋が...
[ { "submission_id": "aoj_1505_10000615", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,int> P;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define INF 10000000000000000\nint n,m;\nl...
aoj_1503_cpp
Problem D : Numbers n が与えられるので、 n 個の連続した正の整数を求めよ。 ただしすべての数が、1とその数自身以外の約数をもたなくてはならない。 Input 入力は以下のフォーマットで与えられる。 n 入力は以下の制約を満たす。 1 ≤ n ≤ 1,500 Output 最初の行に、あなたが選んだ連続した n 個の正の整数の中で一番小さいものを出力せよ。 2行目から n+1 行目に、それぞれの値に対する約数を出力せよ。 約数は1かその数自身でなければどの値を出力しても良い。 1行目に出力した数を x として、i行目には x+i-2 の約数を出力せよ。 出力する値は5,000桁を超えてはいけない。 Sample...
[ { "submission_id": "aoj_1503_3430839", "code_snippet": "#include<bits/stdc++.h>\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/rational.hpp>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long...
aoj_1507_cpp
Problem H : Dungeon (II) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 ゲームの仕様として、プレイヤーに予めダンジョンの危険度を提示し、生成されたダンジョンを探索するのか、それとも新しくダンジョンを生成しなおすかを、選択できるようにしたい。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、合計で n-1 本存在している。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。...
[ { "submission_id": "aoj_1507_10772260", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long, long long>\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, ...
aoj_1509_cpp
Problem A: Rental DVD Shop NEO Problem 僕はレンタルDVDショップ、「NEO」でアルバイトを始めた。まずはこの店の料金システムを勉強することになった。 レンタルDVDは旧作、準新作、新作の3種類存在し、1本のレンタル料金がそれぞれ a 円、 b 円、 c 円である。 会計の際に以下に示すセットレンタルを複数回適用することができる。 まだセットレンタルを適用していないDVDを数本選ぶ。 選んだDVDの本数が d 本以上の場合、選んだDVDの合計金額が、 (選んだDVDの本数) * e 円を超える場合は、それらを (選んだDVDの本数) * e 円でレンタル出来る。 選んだDVDの本数が d 本未満...
[ { "submission_id": "aoj_1509_8293237", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nlong long A, B, C, D, E;\nlong long NA, NB, NC;\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tcin >> A >> B >> C >> D >> E; if (A + B + C + D + E == 0)...
aoj_1510_cpp
Problem B: Independent Research Problem 僕は夏休みの自由研究のテーマとして、生き物観察を選択し、生き物観察キットを購入した。 この生き物は3次元グリッド状の空間を好んで生息する。 各セルには最大1匹までしか入れない。 周囲の環境に合わせ1日が経過するごとに誕生と死滅を繰り返す。 誕生と死滅の条件は、セルに隣接する生き物の数に依存する。 ここで、あるセルに生き物が隣接するとは、あるセルと生き物が生息しているもう一つのセルが面、辺、または点を共有していることを言う。 誕生と死滅のルールは次のようになる。 生き物が生息していないセルにおいて、隣接する生き物の数が a i (1 ≤ i ≤ M 1 )...
[ { "submission_id": "aoj_1510_2169979", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nint f[2][5][5][5], m1, a[27], m2, b[27];\nint main() {\n\tint n, cnt = 1;\n\twhile (scanf(\"%d\", &n), n) {\n\t\tif (cnt != 1)printf(\"\\n\");\n\t\trep(i, 5)rep(j,...
aoj_1511_cpp
Problem C: General of Taiko Problem とあるゲームセンターには、曲に合わせて流れてくる譜面通りに和太鼓を叩くゲームがあります。 譜面は長さ L のセルからなり、各セルには何もない、またはノートと呼ばれるプレイヤーがとるべき行動を表した記号があります。 ノートは2種類ありそれぞれ、和太鼓の面を叩く「トン」、和太鼓の縁を叩く「コツ」があります。 これらのノートに合わせて和太鼓を叩くと得点を得ることができます。 この得点の合計が10000点以上であればクリアとなります。 このとき、プレイヤーが曲をクリアできる確率を求めなさい。ただし、プレイヤーは常に最適な行動をとることにします。 プレイヤーが譜面通りに和...
[ { "submission_id": "aoj_1511_9613108", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cassert>\n#include <random>\n#include <iomanip>\n#pragma GCC optimize(\"O3\...
aoj_1512_cpp
Problem D: Smartphone Game Problem 人気のスマフォゲームの一機能を実装してみよう。ゲームは以下の仕様に基づく。 5*5のマスに、5種類のブロックが設置されている。 それぞれの種類のブロックには得点が設定されている。 得点には、ブロックの得点の他に、ボーナス得点が設定されている。最初のボーナス得点は1である。 ブロックを任意に1つだけ決めて、最大で n 回まで上下左右に移動できる。移動先のブロックは移動元のブロックのあった場所に移動する。つまり、隣接したブロックを交換する事になる。 移動した後、同じ種類のブロックが縦3個以上又は横3個以上に並んだ場合、並んだブロックは全て消える。 消えるべきブロックが...
[ { "submission_id": "aoj_1512_10235110", "code_snippet": "// AOJ #1512 Smartphone Game\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int NROWS = 5, NCOLS = 5;\nint dr[4] = {-1,1,0,0};\nint dc[4] = {0,0,-1,1};\n\nvector<int> blockScore(6);\n\nint simulateChain(vector<vector<int>> bo...
aoj_1514_cpp
Problem F: Ballon Contest Problem 空の様子がいつもと違う。色とりどりの多彩な熱気球が空を覆っていた。今日は熱気球の大会だ。 熱気球から落とされた得点付きボールを参加者全員で取り合うらしい。 せっかくなので優勝者を予想してみることにした。 レクリエーションにはN人参加する。 N 人の参加者はそれぞれ自分の位置が与えられる。複数の参加者に同じ位置が与えられる事はない。 上空の熱気球からM個のボールが1個ずつ落下する。 参加者は全員同じタイミングで走り始め、ボールに向かって同じ速度で一直線に走る。 ボールの落下位置に一番早く辿り着けた人がボールを取得できる。同時に複数人辿り着いた場合は、一様な確率で取得で...
[ { "submission_id": "aoj_1514_9729453", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\nusing namespace std;\n\n#define EPS 1e-8\n#define INF 1e100\n\ntypedef complex<double> P;\ntypedef vector<P> vP;\n\nconst P iu(0.0, 1...
aoj_1522_cpp
Planarian Regeneration Problem 皆さん、「プラナリア」って知っていますか? プラナリアとは、日本では川の上流の石や枯葉などの裏に張り付いて生息している水生生物です。 プラナリアの最も優れた能力はその「再生能力」です。プラナリアの再生能力は著しく、例えば、三等分するとそれぞれが再生し、数週間後には元の完全な状態のプラナリアが3匹できあがります。 この度、会津の山奥の上流にて発見された新種のプラナリアの姿は長方形をしていて、プラナリアはある法則により再生します。 このとき、プラナリアは下の図のように配置し、2つの手段でプラナリアの切断実験を行います。 まず1つ目の手段として、垂直方向に切ることを考えます。一...
[ { "submission_id": "aoj_1522_2102518", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-7;\n\nld getans(ld l,ld r, int b,ld c) {\n\tif (b == 0||abs(r-l)<eps) {\n\t\treturn (1-l)*(r - l);\n\t}\n\tld aa = getans(l,(1-c)*l+c*r, b - 1, c);\n\tld ab = ...
aoj_1515_cpp
Problem G: The Humans Braving the Invaders Problem 現在、地球は宇宙からの侵略者であるインベーダーの攻撃を受け、人類で生き残った者は基地にいる我々だけとなった。 奴らに対抗できる戦力もほとんど残されていない。 だが、ここで我々は諦めるということはしない。 インベーダを全滅させることが、我々人類が生き残る最後の手段である。 今から最後になるであろう作戦の内容を説明する。 まず、インベーダーの戦力に比べ我々の戦力はあまりにも少ないので、この基地に立てこもり、籠城戦を行う。 この基地は高い山に囲まれていて、インベーダーが侵攻するには基地正面にある真っ直ぐな道を通る他無い。 この道をフィール...
[ { "submission_id": "aoj_1515_8293599", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\nusing namespace std;\n\nclass BIT {\npublic:\n\tint size_ = 1;\n\tvector<int> dat;\n\n\tvoid init(int sz) {\n\t\tsize_ = sz + 2;\n\t\tdat.resize(size_ + 2, 0);\n\t}\n\n\tvoid...
aoj_1508_cpp
Problem I : RMQ n 個の数字 a 0 , a 1 , ... , a n-1 と q が与えられる。 q 個のクエリーに対して適切な処理を行なって欲しい。 クエリーは以下の3種類の操作が存在する。 値のシフトを行う l と r のペアが与えられる。( l < r ) a l から a r までの値を Circular Shiftさせる。 0 1 2 3 4 5 6 7 8 9 に対してl=2,r=5というクエリーが与えられたとする。 シフトを行った数字列は 0 1 5 2 3 4 6 7 8 9 となる。 最小値を求める l と r のペアが与えられる。( l ≤ r ) a l から a r までの値の中で最小の値...
[ { "submission_id": "aoj_1508_11059198", "code_snippet": "// Begin include: \"../../template/template.hpp\"\nusing namespace std;\n\n// intrinstic\n#include <immintrin.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cfenv>\n#include <cfloat>\n...
aoj_1513_cpp
Problem E: Doragoso Ball Problem L 個集めるとドラゴソが現れどんな願いでも一つだけ叶えてくれるというドラゴソボールがとある迷宮に散らばっている。入口からスタートし、途中で立ち止まることなくすべてのボールを回収し、それらを迷宮内にある祭壇に奉納し、そのまま呪文を唱えるとドラゴソが現れ願いを叶えてくれるという。ただし、呪文を唱え始める時間は入口からボールを奉納するまでのルートのうち、移動時間が K 番目に短いルートで移動したときの時間でなければならない。 この迷宮は部屋とわたり通路からなり、部屋と部屋はわたり通路を通る事によって行き来できる。ドラゴソボールはそれらの数ある部屋のどこかに落ちている。 なお...
[ { "submission_id": "aoj_1513_10235149", "code_snippet": "// AOJ #1513 Doragoso Ball\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nstruct Edge { int to, cost; };\n \nstruct State { ll cost; int room, mask; };\n \nstruct StateCompare {\n bool operator()(const St...
aoj_1520_cpp
Problem E: Light Source Problem 二次元平面上に n 個の円状の光源が配置されている。ある光源が光を受けると、下図のようにその光源の中心点から扇状に光が放出される。 光源の中心点は( x , y )、半径は r で表される。 光となる扇形は角 β を中心に- θ /2, + θ /2の方向へ対称に広がり、その半径は α である。 光源の円が扇型の光に完全に覆われるとき、光源はその光を受けたとみなされる。光源から放出される光の強さは、受けた光の強さの合計となる。ただし、放出される光の強さには上限があり、上限を超えた場合、光の強さは上限値と同じになる。なお、ある光や光源によって光が遮られることはない。 ある...
[ { "submission_id": "aoj_1520_2964704", "code_snippet": "#include <bits/stdc++.h>\n#define PI M_PI\n#define EPS 1e-10\nusing namespace std;\ntypedef long long ll;\ntypedef complex<double> P;\n\nstruct light {\n P pos;\n double r, t, a, b;\n ll p;\n};\n\ndouble cross(P a, P b) { return a.real() * b.imag() ...
aoj_1518_cpp
Problem C: Last One Problem あなたは、集合好きな友人Aと一風変わったゲームをしてみる事にした。 n個の要素からなる重複が許される非負の整数の集合Sが与えられる。集合Sに含まれる各要素は非負の p i 進数である。集合Sに対して 以下のStep1~3を1つのターンとして、あなたと相手の二人が交互にターンを繰り返す。各ターンではこの番号の順に操作を行う。 Step 1: 集合Sに含まれる全ての要素を一度取り除き、その取り除いた各要素を2進数に変換して、0を消して1が1つ以上連続する部分に分割し、その分割された部分を全て集合Sに加える。 Step 2: このとき集合Sが空になっていたならば、現在このターンを...
[ { "submission_id": "aoj_1518_5003973", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\nusing namespace std;\n\nint ctoi(char c){\n if('A' <= c && c <= 'Z'){\n return c - 'A' + 10;\n }else if('a' <= c && c <= 'z'){\n return c - 'a' + 36;\n ...
aoj_1525_cpp
Problem J: J's Final Problem Problem 不思議なダンジョンとは構造の変化を伴うダンジョンである。不思議なダンジョンは階層が深いものから浅いものまで様々なものがあり、深い階層には凶悪なモンスターが生息していたり、財宝が眠っていたりする。ジェイは不思議なダンジョンを研究している研究者である。ある日、新しいダンジョンを掘っていたところ、とても大きく深いダンジョンに繋がってしまった。ジェイはこのダンジョンを「ジェイの最終問題」と名付けた。このダンジョンの調査が、優秀な冒険者であるあなたに依頼されることになった。冒険者の目的はできるだけ深い層のフロアに到達することである。このダンジョンの特徴を以下に述べる。 ...
[ { "submission_id": "aoj_1525_3086993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nstruct SCC{\n int n;\n vector<vector<int> > G,rG,T,C;\n vector<int> vs,used,belong;\n SCC(){}\n SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}\n \n void add_edge(int f...
aoj_1529_cpp
Problem D: Cheat Case Problem 算数のテストを受けることになった俺達は、今から勉強しても赤点を取ってしまうことを確信し、カンニングで乗り切ることを決意した。 算数のテスト問題を作成するA先生はテスト用紙を手書きで作ることで知られている。彼がテスト問題を作成している時の筆跡情報を、近所の博士に作ってもらった筆跡盗聴鉛筆で盗み取る作戦だ。だが肝心の筆跡情報から問題を復元するプログラムは博士の技術では作ることができなかった。しかも、たとえ問題が復元できたとしても、俺達の頭脳では問題を解くことができない。どうすることもできなくなった俺達は、プログラマーであるあなたに、筆跡情報から問題を復元し、それを解くプログラム...
[ { "submission_id": "aoj_1529_894238", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cassert>\nusing namespace std;\ntypedef pair<int,int> P;\n\nnamespace NS {\n string s;\n int p;\n int exp();\n int term();\n int fact();\n \n int exp() {\n int x = term();...
aoj_1533_cpp
Problem H: Caterpillar とある天空都市に住む大学生のGは、芋虫のいも太郎を飼っている。 彼は、いも太郎に最短歩数で全てのえさを順番に食べるように躾けをした。彼の友人であるあなたは、彼からいも太郎が本当に躾けられているかどうかを調べて欲しいと依頼されたので、プログラムを書くことにした。 Problem 複数の障害物とエサがグリッド状のエリア内に存在する。頭と 5 つの胴体を持つ芋虫がこのエリアを探索する。芋虫が順番に最後までエサを食べたときの最小の歩数を求めよ。 ただし全てのエサを食べることが不可能な場合は -1 を出力すること。なお、エサの上に芋虫の頭が重なったとき、エサを食べることができる。 エリアは以下の...
[ { "submission_id": "aoj_1533_893191", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint H,W,N;\nchar field[51][51];\nconst int dy[]={-1,0,0,1};\nconst int dx[]={0,-1,1,0};\npii esas[11];\nint d[11][11][11][4][4][4][4][4];\n\nint getAng(int cy,int cx,int n...
aoj_1528_cpp
Problem C: Warping Girl Problem 会津魔法学校は, 魔法を使える者が集まる学校である. その学校の生徒の一人であるハルカは, 魔法陣の上でワープの魔法を使う事ができる. 彼女の家から学校には, 長さ L の一直線状の道がある. また, この道には所々魔法陣が描かれている. 彼女は毎日この道を使って学校に通っているため, できるだけ短い時間で学校に辿り着きたいと思っている. そこでプログラマーであるあなたは, 彼女に学校まで行くときにかかる最小の時間を教えてあげることにした. ハルカは, 1 分で距離 1 だけ歩いて先に進むことができる ( *戻ることはできない).また, ワープの魔法陣が書いてある位置 ...
[ { "submission_id": "aoj_1528_5851546", "code_snippet": "#include<bits/stdc++.h>\n\nusing Int = long long;\nusing namespace std;\n\nconstexpr Int inf = 1e10;\n\nint main(){\n Int L,N;\n cin >> L >> N;\n vector<tuple<Int,Int,Int>>edge;\n for(int i = 0; i < N; ++i){\n Int p,d,t;\n cin...
aoj_1519_cpp
Problem D: Room of Time and Spirit Problem 20XX年、とある科学者がバイオテクノロジーによって強力な人造人間を開発してしまった。この人造人間はコンピュータにより、戦闘の達人たちの細胞を組み合わせて作られているため非常に強力である。 このままでは地球は人造人間に支配されてしまうので、 N 人の戦士たちは人造人間と戦うことにした。しかし今の戦士たちでは到底人造人間に敵わないため、修行をしなければならない。また、 N 人の戦士たちはそれぞれ 1 ~ N の番号が付いている。 この時代、天空都市AIZUには SRLU室と呼ばれる修行のための特殊な部屋が存在した。この部屋での1年は外界の時間の1日と...
[ { "submission_id": "aoj_1519_9725871", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nint par[100001], D[100001], sum[100001];\n\nint find(int x) {\n if (par[x] != x) {\n int px = par[x];\n par[x] = find(par[x]);\n D[x] += D[px];\n return par[x];\n }\n ...
aoj_1537_cpp
Problem L: WW Problem 19XX年、nつの国で連合国軍が結成された。連合国軍に属する各国では、敵軍の侵略から自国を守るために国内に1つ以上の拠点を作り、周囲を警戒している。図1は連合国の例を示し、長方形が各国を、その中にある円がその国の拠点を表す。 図1 敵軍が侵略してきた場合、それを一番最初に発見した拠点からその情報を連合国軍に属する他国または自国の拠点に発信することになっている。 情報を他の拠点に伝えるには正の整数のコストがかかる。 また、各国では自国の拠点のうち、連合国軍翻訳通信部(ATIS : Allied Translator and Interpreter Section)に属する兵士が配属されている...
[ { "submission_id": "aoj_1537_893213", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#...
aoj_1535_cpp
Problem J: Hanimon ハニカムモンスターはハニカム模様の六角形状の不思議な生き物である。 ハニカムモンスターには様々なサイズのものがあり、一辺が N 個の正六角形のマスから成るハニカムモンスターをサイズ N のハニカムモンスターとする。 一方、聖なる模様はハニカムモンスターと同じく、六角形として定義され1辺が P 個の正六角形のマスから成る聖なる模様をサイズ P の聖なる模様とする。 ハニカムモンスターと聖なる模様の形の例を以下に示す。サイズは1辺の正六角形のマスの数に対応する。 図1: サイズの例 図2: ハニカムモンスターと聖なる模様の例 (Sample Input 1) ハニカムモンスターと聖なる模様の各マスに...
[ { "submission_id": "aoj_1535_890620", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__...
aoj_1541_cpp
Problem A: Yu-kun Likes an Integer Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい数字が大好きだ。そんなゆう君は最近、表に0から9までのいずれかの数字の書かかれたプレートを n 枚購入して n 桁の数を作る遊びに熱中している。 今月もゆう君は貰ったお小遣いで n 枚のプレートを買いに行こうと考えていた。 Problem プレートは表に書かれている数字によって値段が異なる。 それらを使って作れる n 桁の数をできる限り小さくしたい。 ゆう君の所持金額と各プレートの値段、購入するプレートの枚数が与えられるの...
[ { "submission_id": "aoj_1541_9659771", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<vector<int> > combinations;\n\nvoid gen(int idx, vector<int> &comb) {\n if (idx == 0) {\n combinations.push_back(comb);\n return;\n }\n for (int i = 0; i < 10; i++) {\n ...
aoj_1531_cpp
Problem F: Remainder Zero Problem 整数の集合A,Bについて,次の条件を満たす整数 x がいくつ存在するか答えよ。 A i mod x = 0 かつ x mod B j = 0 がすべての i (1 ≤ i ≤ N), j (1 ≤ j ≤ M) について成り立つ。 ( a mod b は a を b で割ったときの余りを意味する) Input N M A 1 A 2 ... A N B 1 B 2 ... B M Constraints 入力は以下の条件を満たす。 1 ≤ N , M ≤ 5 1 ≤ A i , B j ≤ 10 14 (1 ≤ i ≤ N , 1 ≤ j ≤ M ) Output ...
[ { "submission_id": "aoj_1531_10314507", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\n\nusing ll=long long;\n\nll modpow(ll a,ll n,ll p){\n\tll r=1;\n\twhile(n>0){\n\t\tif(n%2)r=(r*a)%p;\n\t\tn/=2;\n\t\ta=(a*a)%p;\n\t}\n\treturn r;\n}\n\nint main(){\n\t\n\tcin.tie(nullptr);\n\tios::sync_w...
aoj_1543_cpp
Problem C: Changing Grids Background A君とB君は、『Changing Grids』というゲームに熱中している。このゲームは2人用で、プレイヤー1がステージを構成し、プレイヤー2がそのステージに挑戦しゴールを目指すというものである。 今、A君とB君はこのゲームを何度かプレイしているが、A君の連勝でB君は1度も勝つことができていない。そこであなたは、B君にこのゲームを攻略するためのヒントを教えてあげることにした。 Problem 時刻 T 0 = 0における縦 H ×横 W の大きさの二次元グリッドの状態が Area 0 として与えられる。次に、このグリッドの状態は時刻 T i において、状態 Ar...
[ { "submission_id": "aoj_1543_3577848", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nstruct state{\n int i,j,t,d;\n};\nbool operator...
aoj_1545_cpp
Problem E: Rooted Tree Game Problem 初期状態として複数の根付き木が与えられる。これに対しAliceとBobはゲームを行う。ゲームは2人交互に行い、先手がAliceで後手がBobである。ターンが回ってきたプレイヤーは以下の行動を取る。 根(親を持たない頂点)を1つ選択する。この頂点を S とする。 S を根とする根付き木に含まれる頂点を選択する(ここでは S も選択可能)。この頂点を T とする。 S から T への経路上にある頂点を、 S と T も含めすべて削除する。また、削除された頂点が端点であるような辺もすべて削除する。 ターンが回ってきた時点で頂点がすべて削除されていた場合、そのプレイヤー...
[ { "submission_id": "aoj_1545_8295647", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, P[1 << 18];\nint M;\nint Answer[1 << 18];\nvector<int> Cand;\nvector<int> G[1 << 18];\n\nint FindMex(vector<int> vec) {\n\tvec.push_back(-1);\n\tvec.push_back(...
aoj_1546_cpp
Problem F: Coupling Problem 愛津大学では毎年大規模な合コンが行われています。 今年は N 人の男性と M 人の女性が参加します。 それぞれ男性は0から順に N −1までIDが割り振られおり、女性は0から順に M −1までIDが割り振られています。 この合コンでは自分の「大好きな人」と「そこそこ好きな人」のIDを提示します。 男性はそれぞれ女性のIDを、女性はそれぞれ男性のIDを提示します。 その後、以下のルールに従ってカップルが成立します。 男性は複数の女性、女性は複数の男性とカップルになってはいけない。 互いに「大好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作るこ...
[ { "submission_id": "aoj_1546_3578210", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n#define all(x) (x).begin(),(x).end()\n\ntypedef pair<int,int> P;\ntypedef p...
aoj_1550_cpp
Problem J: Yu-kun Likes To Play Darts Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいダーツが大好きだ。 そんなゆう君は最近ダーツにはまっていたが、普通のダーツに飽きてしまったため独自のダーツボードを作ることにした。 ダーツについては ダーツ を参照。 Problem ゆう君が考えたダーツの内容は以下の通りだ。 無限に広いダーツボードには得点の書かれた多角形が幾つかある。 プレイヤーはダーツの矢を1本だけ持っている。 プレイヤーは矢を投げ、いずれかの多角形に矢を刺すことでそこに書かれている得点を得る。...
[ { "submission_id": "aoj_1550_8296263", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ===========================================================...
aoj_1547_cpp
Problem G: Yu-kun Likes People Watching Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい人を観察することが大好きだ。 そんなゆう君は、去年の夏休みに作成した人の移動経路のデータと建物の位置情報を観察してあることを疑問に思った。 全ての人が建物内にいる時間の長さのうち、最長のものはどのくらいなのだろうか? Problem 建物と人の移動経路の情報が与えられる。 全ての人が連続して建物内にいるような時間のうち、最も長いものの長さを出力せよ。 そのような時間が存在しない場合は長さは0とする。 建物と人はそ...
[ { "submission_id": "aoj_1547_10334747", "code_snippet": "// AOJ #1547 Yu-kun Likes People Watching\n// 2025.3.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst double EPS = 1e-9;\n\nbool inPoly(double x, double y, const vector<pair<double,double>> &poly) {\n int cnt = 0...
aoj_1544_cpp
Problem D: Smell Searcher Problem 廃津大学では強烈な香りを放つ N 個の廃材が一列に並んでいます。 廃材には1から N の番号が順番にふられていて、 i 番目の廃材は強さ a i の香りを放っています。 リヒト君は仕事で、すべての廃材の放つ香りの総和を求めるよう依頼されました。 香りの総和が M 以上になると「大変きつい仕事」とみなされ特別支給金がもらえます。 この仕事を行うために、リヒト君は精度 R の香り検出器を使います。 精度 R の香り検出器を使うと i 番目の廃材の香りを測ろうとすると同時に i − R , i − R +1,..., i −1, i , i +1,..., i + R...
[ { "submission_id": "aoj_1544_10071527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nlong long computeSum(const vector<long long>& a, long long C, int R, long long M) {\n int N = (int)a.size();\n vector<long long> L(N), Rv(N);\n for(int j = 0; j < N; j++){\n L[j] = a[j...
aoj_1536_cpp
Problem K: Witch Craft Moves Problem 会津魔法学校は、魔法を使える者が集る学校である。 会津魔法学校には1~ N までの番号がついた教室と教室同士をつなぐ廊下がある。教室や廊下は何度でも通ることができる。また、会津魔法学校には任意の教室 u , v 間を0個以上の教室と廊下を一度ずつ通って移動することができるような経路がただ1つある。 各教室には魔法陣が設置してある。この魔方陣によって、教室に入る度に入った者の魔力が増減させられてしまうのだ。生徒の移動を助けるために以下の二つの質問に応えてほしい。 質問1 0 A B 教室 A から B へ移動したときの魔力の残量を出力してほしい。教室 A と B...
[ { "submission_id": "aoj_1536_10238012", "code_snippet": "// AOJ #1536 Witch Craft Moves\n// 2025.2.21\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...
aoj_1542_cpp
Problem B: Yu-kun Likes Letters in the English Alphabet Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい英小文字が大好きだ。 そんなゆう君は紙と丸と線、そして英小文字を使う新しい遊びを考え、その採点用プログラムを書くことにした。 Problem 初期状態として、紙に V 個の丸と E 本の線が書かれている。丸には0から昇順に番号がつけられており、各丸の中には英小文字が1つ書かれているか、何も書かれていない。 各線は異なる2つの丸を結んでいる。1つの丸が26本以上の線で結ばれることはな...
[ { "submission_id": "aoj_1542_8295587", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint N;\nint M, A[1 << 18], B[1 << 18];\nchar C[1 << 18];\nvector<int> G[1 << 18];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M;\n\tfor (int i = 1; i <= N; i++) cin >> C[i];\n\tfor ...
aoj_1554_cpp
Problem D: Friday the 13th Problem 今年も「立命館大学競技プログラミング合宿」が開催されることとなった。僕は毎年開催されるこの合宿をとても楽しみにしている。しかし、欲望に耐え切れず合宿前に散財してしまい、お金に余裕が無くなってしまった。そこで僕は、立命館大学の最寄り駅である南草津まで移動するのに最も安く済む青春18きっぷを使うことにした。この切符は安く済む代わりに乗り換えが多く発生し、移動に丸1日使わなければならないのでとても疲れる。そんな移動をする日が13日の金曜日であることに気づいたのは会津若松駅を出発した後だった。僕は12時間、不安を感じながら電車に揺られるはめになった。 今日は合宿2日目、2...
[ { "submission_id": "aoj_1554_5550660", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);-...
aoj_1557_cpp
Problem G: Yu-kun Likes a Treasure Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい財宝が大好きだ。ゆう君は今日も財宝を探すため洞窟を探索する予定だ。そこでゆう君は洞窟の地図から、洞窟を全て見て回る際に歩く距離を求めるプログラムを書くことにした。 Problem 洞窟の情報とゆう君の初期位置が与えられる。Fig.1のように、 洞窟は2次元平面上の多角形で表される部屋が、1つ以上繋がった集合で構成される。部屋が別の部屋と接する場合は必ず点で接し、その接点が洞窟の道となり、接点に訪れるとその道を通ったことにな...
[ { "submission_id": "aoj_1557_3561159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<...
aoj_1549_cpp
Problem I: Hard Beans Problem 大津大学では豆が盛んです。 N 個の豆が一直線上に並んでいます。 それぞれ0から順に N −1まで番号がふられており、 i 番目の豆の硬さを a i とします。 シアン君は理想の豆の硬さを D だと考えています。しかし、シアン君は面倒くさがりなのであまり遠くにある豆を取りに行きたくありません。したがって、シアン君は l 番目の豆から r 番目の豆の中で硬さが D に最も近い豆を知りたいと思っています。 シアン君は Q 個の質問をしてくるので、それぞれの質問に対し閉区間[ l , r ]番目にある | 豆の硬さ − D | の最小値を求めるプログラムを作成してください。(ただ...
[ { "submission_id": "aoj_1549_10848513", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nenum {\n NOTFOUND = 1000000000000000000LL\n};\n\nclass SuccinctBitVector {\nprivate:\n const uint64_t size; // ビットベクトルのサイズ\n static const uint64_t blockBitNum = 16;\n static const uint64_...
aoj_1552_cpp
Problem B: Mountain Climbing Problem なりよし君は山登りの計画を立てています。計画はとても大切です。高原・盆地が多いのか、峰・窪地が多いのか、山腹があるのか、状況によって用意する荷物も変わってきます。 なりよし君が持っている登山ガイドブックには、今回登山に利用する道の一定の距離毎の標高がスタート地点からゴール地点まで順に書かれています。 ガイドブックに書かれている標高をスタート地点からゴール地点まで順に a 1 , a 2 , a 3 , ..., a N とし、高原、盆地、山腹、峰、窪地を次のように定義します: 高原 a i-1 < a i = a i+1 = ... = a j > a j+1...
[ { "submission_id": "aoj_1552_5550112", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvector<pair<char,int>> runlength(vector<char> vs){\n\tvector<pair<char,int>> res;\n\tfor(auto v:vs){\n\t\tif(res.empty() || res.back().first!=v) res.emplace_back(v,0);\n\t\tres.back().second++;\n\t}\n\tr...
aoj_1553_cpp
Problem C: Manhattan Warp Machine 1 Problem とある宇宙では、1次元の整数座標点上に星が存在し、宇宙人達はマンハッタンワープ装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にコスト c i でワープすることができる。 今、点0の星にいるある宇宙人が点 x の星に行きたいと考えている。 点 x の星に辿り着くまでの最小のコストを答えよ。 辿り着けない場合は-1を出力せよ。 1次元上の整数座標点には必ず星が存在する。 点 x 1 と点 x 2 間のマンハッタン距離は| x 1 - ...
[ { "submission_id": "aoj_1553_10025912", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst int inf = 1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint d[20], c[20];\nbool v[300000];\n\nint main(){\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n \n ...
aoj_1548_cpp
Problem H: Yu-kun Likes a Directed Graph Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は、丸と矢印で沢山絵を書いてきた。矢印は必ず丸と丸を結ぶようにして描いてある。ある日ゆう君はこれらの絵がグラフであることを知る。 丸は頂点と呼ばれ、矢印は辺と呼ばれているらしい。更にゆう君は、矢印を描く際に、必ずその上に1つ正の整数を書いていた。このように、辺に重みがあり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 今日ゆう君は、新たに閉路という言葉を知った。閉路と...
[ { "submission_id": "aoj_1548_8295743", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint N, W;\nint M, A[1 << 19], B[1 << 19], C[1 << 19];\nbool dp[100009][109];\nvector<pair<int, int>> G[100009];\nqueue<pair<int, int>> Q;\n\nint main() {\n\t// Step 1. In...
aoj_1556_cpp
Problem F: Sum of Numbers Problem a 以上 b 以下の整数が書かれたカードがそれぞれ c 枚ずつあります。 これら c(b-a+1) 枚のカードの中から d 枚のカードを選択したとき、それらのカードに書かれている整数の合計が e になる場合の数を1,000,000,007で割った余りを求めてください。 Input 入力は以下の形式で与えられる。 a b c d e 1行目に、5つの整数 a b c d e が空白区切りで与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ a ≤ 1000 a ≤ b ≤ 1000 1 ≤ c ≤ 100 1 ≤ d ≤ 1000 c ≤ d ≤...
[ { "submission_id": "aoj_1556_10431881", "code_snippet": "// AOJ #1556 Sum of Numbers\n// 2025.4.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\nconst int M_SIZE = 108;\nconst int J_SIZE = 21600;\nll dp[M_SIZE][J_SIZE];\nll dp1_arr[M_SIZE][J_SIZE];\...
aoj_1560_cpp
Problem J: Yu-kun Likes a lot of Money Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお金が大好きだ。ゆう君は、今日もお金を稼ぐために財宝の眠る島を訪れた。ゆう君は事前に財宝のありかの描かれた地図を手に入れている。その地図をもとに出来るだけ多くのお金を稼ぎたい。ゆう君は最大でどのくらいお金を手に入れることができるだろうか? Problem 地図、ゆう君の初期位置、財宝の種類とそれらから得られるお金、そして小さい岩を破壊するために必要な費用の情報が与えられる。地図の情報は h マス × w マスのフィ...
[ { "submission_id": "aoj_1560_6075206", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i...
aoj_1558_cpp
Problem H: Puzzle and Hexagons Backgorund 超大人気ゲーム「パズル&ヘキサゴンズ」がついにリリースされた。このゲームは超面白すぎてドハマりする人が続出。あまりの熱中度に医師から中毒認定を受ける者も続出した。世界各国の有志達はこのゲームの中毒者達を助けるために「パズル&ヘキサゴンズ」のシミュレータを作り、危険な実機でのプレイを避けるよう促そうとした。あなたにはシミュレータ作りに協力して欲しい。 Problem 正六角形のマスを縦に H 個、横に W 個敷き詰めた盤面が与えられる。 Fig.1は H =4, W =7の時の盤面とそれに対応するマスの座標( x , y )を示す。 Fig.1 初期状...
[ { "submission_id": "aoj_1558_9166437", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに...
aoj_1562_cpp
Divisor Problem 12以下の自然数 N が与えられるので、約数の個数がちょうど N 個であるような最小の自然数を求めよ。 Input 1つの自然数 N が 1 行で与えられる。 Constraints 1 ≤ N ≤ 12 Output 約数の個数がちょうど N 個であるような最小の自然数を1行に出力せよ。 Sample Input 1 1 Sample Output 1 1 Sample Input 2 2 Sample Output 2 2 Sample Input 3 3 Sample Output 3 4
[ { "submission_id": "aoj_1562_2884395", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin>>n;\n\tint ans=0;\n\tint count[10000]={};\n\tfor(int i=1;i<=10000;i++){\n\t\tfor(int j=1;j<=10000;j++){\n\t\t\tif(i % j == 0) count[i]++;\n\t\t}\n\t\tif(count[i] == n){\n\t\t\t...
aoj_1559_cpp
Problem I: Hopping Mind Problem チエノとカカオは同じ喫茶店で働く姉妹である。2人はとても仲が良く、ある日、とあるテーブルゲームで遊ぶことになった。 ゲームは R マス× C マスの盤面と、駒としてうさぎのTPを用いる。盤面の各マスは白か黒の色が塗られている。最初にTPを盤面の右下( R , C )におき、2人で次の行動を交互に行う。TPの現在の位置を( a , b )とすると、そこからジャンプ可能な位置( i , j )を1つ選び、TPをそこにジャンプさせる。TPがジャンプ可能な位置( i , j )は以下をすべて満たす。 1 ≤ i ≤ R かつ 1 ≤ j ≤ C かつ i ≤ a かつ j ≤ ...
[ { "submission_id": "aoj_1559_10760662", "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 SIZE 1005\n\nin...
aoj_1555_cpp
Problem E: Distinct Dictionary Background 辞書をこよなく愛する者がいた。彼らは自分だけの辞書を作ることが大好きである。 そこであなたは彼らの辞書に、自由にカスタマイズする機能を付け加えてあげることにした。 Problem 初めに、何の単語も入っていない空の辞書が存在する。 N 個の文字列 S id と Q 個のクエリが与えられる。 各クエリはクエリの種類 k と文字列を指す id で与えられる。 クエリの種類は以下の3つである。 k =1のとき S id を辞書に追加する。 k =2のとき S id を辞書から削除する。 k =3のとき辞書に追加された文字列の中で S id を先頭からの部分...
[ { "submission_id": "aoj_1555_5550778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param n `0 <= n`\n// @return minimum non-negative `x` s.t. `n <= 2**x`\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U <...
aoj_1570_cpp
Lights of Apartment Problem エーちゃんとリカちゃんとハルトくんはマンションに遊びに来た。 3人は全ての部屋の電気を管理できる部屋に忍び込みいたずらをすることにした。 このマンションは n 個の立方体が1列に並んでいる形をしている。 各立方体は西から順に1辺の長さが1ずつ増えていて(1,2,3,..., n )、 i 番目の立方体は i 階あり、各階に縦 i ×横 i 個の部屋がある。 2番目以降の立方体の西側は1つ西の立方体の東側と接していて、全ての立方体の南側は真っ直ぐな道路に面している。 初め全ての部屋に電気がついている。 3人はそれぞれ次の行動をした。 エーちゃんは西から k 番目の全ての部屋の電気...
[ { "submission_id": "aoj_1570_3089072", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#incl...
aoj_1568_cpp
String Conversion Problem ジェニファーとマリアンはカーラに文字列 S をプレゼントしました。 しかし、カーラは文字列 S をもらっても嬉しくありません。 文字列 T が欲しかったのです。 3人は協力して文字列 S を文字列 T に変えることにしました。 最初にジェニファーが文字を任意の順番に並び替えます。 次にマリアンが2種類のアルファベットの小文字を交換することを任意の回数行います。 この操作では、例えば以下のように、文字列中の同じ文字が全て交換されます。 aab → a と b を交換 → bba aab → a と c を交換 → ccb 最後にカーラがある1文字を別の1文字に置換することを T にな...
[ { "submission_id": "aoj_1568_2576345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint a[26],b[26],n,ans;\nstring s,t;\n\nint main() {\n cin >> n >> s >> t;\n for(int i=0; i<n; i++) {\n a[s[i]-'a']++;\n b[t[i]-'a']++;\n }\n sort(a,a+26);\n sort(b,b+26);\n for(int i=0; i<26; ...
aoj_1567_cpp
Lucky Number Problem 0 から N -1 までの数字があります。 M +1日間のラッキーナンバーを以下の方法で選びたいと思います。 最初の日にラッキーナンバーをランダムに決めます。 i 日後のラッキーナンバーを A 、( i +1)日後のラッキーナンバーを B とすると、 B = ( A + j ) % N (ただし j は 0 ≤ j < N かつ ( j / K ) が偶数となる全ての整数である ) で求められる B のうちのいずれかからランダムに決めます。ただし、 a / b の結果は小数点以下を切り捨てた整数とし、 a % b は a を b で割った余りとします。 また K は N / K が割り切れて...
[ { "submission_id": "aoj_1567_3079308", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\...
aoj_1571_cpp
String Crossing Problem N 個の文字列{ S 1 , S 2 , ..., S N } が与えられる。 続いて Q 個のクエリが与えられる。 クエリの種類は以下の2つである。 a , b , c , d が入力され S a の b 文字目からの substring を S c の d -1文字までの substring の後に繋げる。これを新しい S c とする。 元の S c の d 文字目からの substring を S a の b -1文字までの substring の後に繋げる。これを新しい S a とする。 a , b , c が入力され S a の b 文字目を c に変更する。 例えば S 1...
[ { "submission_id": "aoj_1571_10349818", "code_snippet": "// AOJ #1571 String Crossing\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include <ext/rope>\nusing namespace __gnu_cxx;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; i...
aoj_1578_cpp
Rubik Dungeon Problem あなたは n × n × n の立方体のルービックキューブ型のダンジョンにやってきた。 あなたは現在( x 1 , y 1 , z 1 )の部屋にいる。 目標の宝は( x 2 , y 2 , z 2 )の部屋にある。 あなたは隣接している前後左右上下の部屋に単位時間で移動することができる。 各部屋には6つのボタンがあり、それぞれのボタンを押すことにより、単位時間で、ルービックキューブのように以下の操作を行うことができる: 現在いる部屋と x 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 現在いる部屋と y 座標の値が同じ全ての部屋を時計または反時計周りに90度...
[ { "submission_id": "aoj_1578_3090330", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<...
aoj_1561_cpp
Problem K: Manhattan Warp Machine 2 Problem とある宇宙では、3次元の格子点上に星が存在し、宇宙人達はマンハッタンワープ装置という装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にワープすることができる。 ワープ装置は改良され、コストがかからずに移動出来る。 今、(0, 0, 0)の星にいるある宇宙人がある M 個の星を訪れたいと考えている。 M 個の星を全て訪れるのに最短で何回ワープをする必要があるか答えよ。 もし、1つでも訪れることが不可能な星がある場合は-1を出力せよ。 ...
[ { "submission_id": "aoj_1561_5550709", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define r...
aoj_1569_cpp
Bomb Removal Problem 縦 H ×横 W のグリッド上に N 個の点火された導火線とそれぞれに対する1つの爆弾が配置されている。それぞれの導火線と爆弾は、 L i 個のマスからなる path i 上に配置され、 path i の j 番目のマスを( px ij , py ij )とする。各爆弾は path i の最後のマス( px iL i , py iL i )に存在する。導火線の火は初期状態(0秒時点)でマス( px i1 , py i1 )にあり、1秒間に1マス進む(( px i1 , py i1 ),( px i2 , py i2 ),...,( px iL i , py iL i )の順に進む)。 最初、...
[ { "submission_id": "aoj_1569_10946142", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<strin...
aoj_1574_cpp
Gossip Problem 1から n までの番号が順番に割り当てられている n 人のアイドルが横一列に並んでいる。 アイドル i は単位時間でアイドル i -1とアイドル i +1に情報を伝達することができる。ただし、アイドル1はアイドル2にしか情報を伝達できず、アイドル n はアイドル n -1にしか情報を伝達できない。 時刻0の時点で、番号が a 1 , a 2 , ..., a m である m 人のアイドルが秘密の情報を持っている。すべてのアイドルが秘密の情報を得ることのできる最小の時間を求めよ。 Input 入力は以下の形式で与えられる。 n m a 1 a 2 ... a m 1行目に2つの整数 n と m が空白区切...
[ { "submission_id": "aoj_1574_2747836", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define...
aoj_1577_cpp
Courage Test P「今日は肝試し企画の番組の収録だ。」 O型智絵里「き、きもだめし……」 四村かな子「智絵里ちゃん大丈夫?」 月野茜「さすがPさん!一行で状況が明晰判明にわかる素晴らしい解説です!さあ!火の玉に向かって走りましょう!」 P「ルールは簡単、みんなで協力してこのエリアのすべての神社にお参りしてくるだけだ。」 茜「オーソドックスですね!早速神社に向かって走りましょう!」 かな子「でもここ、日本有数の神社密度で行かなくちゃいけない神社がたくさんあるんじゃなかったっけ……」 かな子「でも茜ちゃんがいれば心強いよ!」 P「ちなみにスタートは全員違う神社からスタート、合流も禁止だ。それじゃあ智絵里とかな子は初期位置につい...
[ { "submission_id": "aoj_1577_10239296", "code_snippet": "// AOJ #1577 Courage Test\n// 2025.2.22\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\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 == '-') {\t...
aoj_1583_cpp
Equivalent Vertices Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。 園児の一人であるゆう君は,プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は丸と六角形と矢印で沢山絵を書いてきた。 ある日ゆう君はこれらの絵がグラフであることを知る。 丸と六角形は頂点と呼ばれ,矢印は辺と呼ばれているらしい。 ゆう君は2つの頂点間を結ぶようにして矢印を描き、更にその上に0か1を書く。 このように,辺に重み(0または1)があり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 また,頂点と数字 x (0または1)が与えられた時,その頂点から出ている矢印のうち, x ...
[ { "submission_id": "aoj_1583_10240168", "code_snippet": "// AOJ #1583 Equivalent Vertices\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, m;\n cin >> n >> m;\n\n vector<int> typ(n), nxt0(n), nxt1(n), grp(n);\n ...
aoj_1579_cpp
Array Update 2 Problem 項数 N 、初項 a 、公差 d の等差数列 A がある。 以下の形式で、数列を書き換える M 個の命令文が与えられるので、与えられた順序で M 回 数列 A を書き換えたときの数列 A の K 項目の値を求めなさい。 i 番目の命令文は3つの整数 x i , y i , z i で与えられる。(1 ≤ i ≤ M ) x i が0だった場合、 y i 項目から z i 項目までの区間において、値の順序を反転する。 x i が1だった場合、 y i 項目から z i 項目までの区間において、それぞれの値を1増加させる。 x i が2だった場合、 y i 項目から z i 項目まで...
[ { "submission_id": "aoj_1579_5995535", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef long long ll;\nusing namespace std;\n#define inf 0x3f3f3f3f\nconst int N = 2E5+1000;\nint n;\nint a,d;\nint m;\nint k;\nint x[N],y[N],z[N];\n//一切从简单的地方出发\nint sp[N],p = 0;\n...
aoj_1582_cpp
Circles and Ray Problem 2次元平面上にそれぞれ互いに共通部分をもたない N 個の円が与えられる。 また、円にはそれぞれ1から N までの番号が割り振られている。 あなたは、端点が1番目の円の円周上にあるような半直線を任意の数だけ設置することができる。 どの円も1本以上の半直線との共通部分を持っているようにするためには、最低何本の半直線を設置しなければならないかを求めなさい。 Input 入力は以下の形式で与えられる。 N x 1 y 1 r 1 x 2 y 2 r 2 ... x N y N r N 1行目に、1つの整数 N が与えられる。 2行目からの N 行のうち i 行目には i 番目の円のx座標、y座...
[ { "submission_id": "aoj_1582_10240155", "code_snippet": "// AOJ #1582 Circles and Ray\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\n\nstruct Circle { int x, y, r; };\n\nstruct RayCandidate {\n double px, py;\n double dx, dy;...
aoj_1580_cpp
Barter Problem ナナツ君は、うまか棒を x 本、ふがしを y 本持っている。 n 人のお菓子交換人がいる。 それぞれの交換人 i は、 ナナツ君のうまか棒 a i 本と交換人のふがし b i 本 ナナツ君のふがし c i 本と交換人のブタメソ d i 個 のどちらか1つの方法で1回だけ交換してくれる。 この制約の下で、最終的に所持しているブタメソの個数を最大化せよ。 Input 入力は以下の形式で与えられる。 n x y a 1 b 1 c 1 d 1 a 2 b 2 c 2 d 2 ... a n b n c n d n 1行目に整数 n が与えられる。 2行目に整数 x , y が空白区切りで与えられる。 ...
[ { "submission_id": "aoj_1580_5998864", "code_snippet": "#include <bits/stdc++.h>\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 609;\nint t[2][N * 2][N * 2], n, x, y;\nint ans = 0;\nsigned main()\n{\n\tmem...
aoj_1581_cpp
Reflection Warp Machine Problem とある宇宙では、2次元の格子点上に n 個の星が存在し、宇宙人達はReflection Warp Machineを使い、星間を移動している。 この装置は、任意の位置、角度で直線を引くことができる。 この直線を対称軸として、現在いる座標から線対称の座標に移動することができる。 ただし、星が存在しない座標に移動することはできない。 一度引いた直線は何度でも利用できる。 現在、( x 0 , y 0 )の星にいるある宇宙人はすべての星を訪れたいと考えている。 星は好きな順番に訪れることができる。 すべての星を訪れるのに最小で何本の直線を引く必要があるか求めよ。 Input n...
[ { "submission_id": "aoj_1581_3806524", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1581.cc: Reflection Warp Machine\n */\n\n#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<stack>\n#...
aoj_1589_cpp
Problem C: Unhappy Class Problem 山之御船学園の1年G組は、不幸を背負った女子生徒たちが集まるクラスである。 彼女たちは、幸福になることを目標に、日々様々な試練に立ち向かう。 彼女たちのクラスでは、幸福になるための試練の一環として、幸福実技科目を受講することができる。 月曜から金曜のそれぞれ1限から N 限までに授業科目の枠があり、 M 個の受講可能な科目がある。 科目 i は、曜日 d i ( d i = 0, 1, 2, 3, 4がそれぞれ、月曜、火曜、水曜、木曜、金曜に対応する)の a i 限目から始まり、連続する k i コマで行われ、それを受講したときに得られる幸福度は t i である。 ...
[ { "submission_id": "aoj_1589_3865197", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algor...
aoj_1588_cpp
Problem B: Potatoes Problem がっちょ君は N 面の畑と M 個の芋を所有している。各畑にはそれぞれ1から N までの番号が付けられている。がっちょ君は畑に芋を植え収穫することで、芋の数を増やしたいと考えている。 がっちょ君は一人暮らしであり、1人では K 面までの畑しか管理することができない。また、各畑の土の状態や面積は様々で、芋の収穫数や植えることのできる芋の数も様々である。芋を畑 i に植えた場合、1年後には畑 i に植えた芋1つにつき a i 個の芋を収穫することができる。ただし、畑 i には最大でも b i 個の芋しか植えることができない。 がっちょ君が K 面以内の畑に M 個以内の芋を植えた時...
[ { "submission_id": "aoj_1588_3807608", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1588.cc: Potatoes\n */\n\n#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<stack>\n#include<list>\n...
aoj_1593_cpp
Problem G: Star Problem 半径1の円に内接する正 N / K 角形の面積を求めよ。 ただし、正 N / K 角形を 「円周上に等間隔に N 個の点を取り、 K -1個おきにそれぞれの点を結んだ一番外側の図形」 と定義する。 例えば、5/2角形は次のように描くことができる。 まず、半径1の円周上に等間隔に5つの点を取る。 次に、それぞれの点を2-1=1つおきに結ぶ。 一番外側の図形が正5/2角形になる。 Input 入力は以下の形式で与えられる。 N K 2つの整数 N , K が1行に与えられる。 Constraints 入力は以下の制約を満たす。 5 ≤ N ≤ 10 6 1 < K < N /2 N , K...
[ { "submission_id": "aoj_1593_10848492", "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_1584_cpp
Sum of Sequences Problem n 個の要素を持つ数列 A 、 m 個の要素を持つ数列 B 、 それぞれが整数 c からなる q 個のクエリが与えられる。 各クエリについて、 数列 A の[ l a , r a ]を全て足した数と数列 B の[ l b , r b ]を全て足した数の差の絶対値が c になる l a , r a , l b , r b (0 ≤ l a ≤ r a ≤ n −1, 0 ≤ l b ≤ r b ≤ m −1, 配列の番号は0番から始まる) の組み合わせの数を求めよ。 Input n m q a 0 a 1 ... a n−1 b 0 b 1 ... b m−1 c 0 c 1 ... ...
[ { "submission_id": "aoj_1584_5312473", "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#define...
aoj_1585_cpp
String in String Problem 文字列 S と Q 個のクエリが与えられる。 i 番目のクエリ(0 ≤ i ≤ Q -1)には閉区間[ l i , r i ]と文字列 M i が与えられる。 S の l i 文字目から r i 文字目までの部分文字列の中に文字列 M i はいくつ存在するか出力せよ。 Input 入力は以下の形式で与えられる。 S Q l 0 r 0 M 0 l 1 r 1 M 1 . . . l Q−1 r Q−1 M Q−1 1行目に文字列 S とクエリの数 Q が空白区切りで与えられる。 続く Q 行に整数 l i , r i , M i が空白区切りで与えられる。 Constraints 1...
[ { "submission_id": "aoj_1585_10072703", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n int next[26];\n int fail;\n vector<int> out;\n Node() : fail(0) {\n for(int i=0;i<26;i++) next[i]=-1;\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n ...
aoj_1597_cpp
Problem K: Escape of Lappin the Phantom Thief Problem 怪盗ラッパンは宝石を盗みにやってきた。簡単に宝石を手にすることができたが、宝石にはセンサーが仕込まれており、警備ロボットに囲まれてしまった。 警備ロボットは宝石に向かって移動するように仕組まれている。センサーは簡単に外せそうになかったので、宝石を置いて逃走することにした。宝石をできるだけ警備ロボットから遠くに置いて、逃げるための時間稼ぎをすることにした。 敷地は n × m のマスからなる長方形の形をしていて、障害物はない。 k 体の警備ロボットは、それぞれマス( x i , y i )(0 ≤ x i ≤ n −1, 0 ≤...
[ { "submission_id": "aoj_1597_10240229", "code_snippet": "// AOJ #1597 Escape of Lappin the Phantom Thief\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct Robot { int x, y, u, v; };\n\nstruct Event { int v, L, R, delta; };\n\nstruct SegTree {\n int n;\n v...
aoj_1594_cpp
Problem H: Hogemon Get Problem がっちょ君は人気のゲームHogemon Getに熱中している。がっちょ君が住んでいる会津国はそれぞれ1から N の番号がついている N 個の町からなる。また、会津国には M 本の道があり、すべての道は異なる2つの町を結んでいる。がっちょ君は道を双方向に移動することができるが、道以外を通って、ある町から別の町に行くことはできない。 Hogemon Getでは、町 i でボールを d i 個入手することができる。ただし、ある町で再びボールを入手するためには、最後にその町でボールを入手してから15分以上経過している必要がある。なお、がっちょ君は町1、町 N を含むすべての町を何...
[ { "submission_id": "aoj_1594_10501828", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int inf = 1e9;\n\nint main() {\n\n ios_base::sync_with_stdio(0);\n cin.tie(0), cout.tie(0); \n\n int n, m, R;\n cin >> n >> m >> R;\n\n vector<int> d(n);\n for (auto& x : d...
aoj_1595_cpp
Problem I: Traffic Tree Problem それぞれ1から N までの番号が付いた N 個の頂点が、 N -1本の無向辺によって繋がれたグラフが与えられる。各頂点について、その頂点からスタートしてすべての頂点を訪れるための最短のステップ数を出力せよ。 ただし、ある頂点から1本の辺をたどって別の頂点に移動することを1ステップとする。 Input 入力は以下の形式で与えられる。 N u 1 v 1 . . . u N−1 v N−1 1行目に、1つの整数 N が与えられる。 続く N -1行のうち i 行目には i 番目の辺の両端の頂点番号を表す整数 u i , v i が空白区切りで与えられる。 Constrain...
[ { "submission_id": "aoj_1595_10965798", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <iomanip>\n#include <limits.h>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <string>\n#include <utility>\n#include <vector>\n...
aoj_1596_cpp
Problem J: Char Swap Problem 長さが偶数の文字列 S が与えられる。 あなたは、文字列 S の隣り合った2つの文字を入れ替える操作を何回でも行うことができる。 文字列 S を回文にするためには、最小で何回の操作を行う必要があるだろうか? 回文にすることが不可能な場合は-1を出力せよ。 Input 入力は以下の形式で与えられる。 S 1行に文字列 S が与えられる。 Constraints 2 ≤ | S | ≤ 4 × 10 5 文字列はすべて英小文字で構成されている 文字列の長さは偶数である Output 文字列 S を回文にするための最小の操作回数を1行に出力する。回文にすることが不可能な場合は代わり...
[ { "submission_id": "aoj_1596_10702848", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// Ordered Set (PBDS)\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, less<T>,...
aoj_1590_cpp
Problem D: One-Time Path Problem N 個の島と M 本の橋がある。 N 個の島にはそれぞれ1から N までの番号が割り振られている。 M 本の橋にもそれぞれ1から M までの番号が割り振られている。 がっちょ君は現在(時刻0の時点で)、1番目の島にいる。 がっちょ君は i 番目の橋を利用することにより、 a i 番目の島から b i 番目の島へと単方向に移動することができる。 しかし、時刻0の時点では、すべての橋は潮が満ちていて海の中に沈んでしまっている。 i 番目の橋は、時刻 c i になると潮が引いて渡れるようになる。 そして、時刻 c i を過ぎると、すぐにまた潮が満ちて i 番目の橋は再び沈ん...
[ { "submission_id": "aoj_1590_10853810", "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_1599_cpp
Problem M: Dial Problem ダイヤル式の南京錠がある。この南京錠には、5つのダイヤルと、決定ボタンが用意されている。 5つあるダイヤルを回転させて16進数で5ケタの整数を作り、決定ボタンを押して入力すると、入力した値とパスワードが一致していれば開く仕組みになっている。5つのダイヤルそれぞれには 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2, ... の順で文字が現れるように書かれている。 持ち主はパスワードを忘れてしまった。しかし、パスワードの5ケタのうち、1つのケタだけが偶数で残り4つは奇数であったことは覚えている。 ...
[ { "submission_id": "aoj_1599_3663954", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n//BEGIN CUT HERE\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(...
aoj_1598_cpp
Problem L: RedBlue Story 天空都市AIZUのUZIA高校では、競技プログラミングの部活動がとても盛んである。 この部活には、 n 人のRed Coderと n 人のBlue Coderが所属している。 ある日の部活の時間に、Red CoderとBlue Coderでペアを組み、この部活から n 組、KCPというコンテストに参加することとなった。この高校では、ペアを組む学生同士は握手するという習わしがあるので、部員たちは今すぐ自分のパートナーを見つけることにした。 部員たちは全速力で走るため、まっすぐにしか進めない。また、部員たちは、それぞれの部員が移動する距離の総和をできるだけ小さくしたいと思っている。 なお...
[ { "submission_id": "aoj_1598_8305781", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long LL;\ntypedef long double LD;\nconst int N = 205;\nconst int M = 100005;\nconst LD INF = 1e18;\nconst LD eps = 1e-10;\n\ninline int sgn(LD x) {\n return x > eps? 1 : x < -eps? -1 : 0;\...
aoj_1604_cpp
Deadlock Detection In concurrent processing environments, a deadlock is an undesirable situation where two or more threads are mutually waiting for others to finish using some resources and cannot proceed further. Your task is to detect whether there is any possibility of deadlocks when multiple threads try to execute...
[ { "submission_id": "aoj_1604_10851564", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstring>\nusing namespace std;\n#define rep(i,n) for(int i = 0; i < n; i++)\nbool dp[1024][10];\nvoid solve(int n){\n string s;\n cin >> s;\n int state = 0;\n memset(dp,0,sizeof(dp));\n ...
aoj_1607_cpp
Development of Small Flying Robots You are developing small flying robots in your laboratory. The laboratory is a box-shaped building with K levels, each numbered 1 through K from bottom to top. The floors of all levels are square-shaped with their edges precisely aligned east-west and north-south. Each floor is divide...
[ { "submission_id": "aoj_1607_9405300", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing...
aoj_1606_cpp
Complex Paper Folding Dr. G, an authority in paper folding and one of the directors of Intercultural Consortium on Paper Crafts, believes complexity gives figures beauty. As one of his assistants, your job is to find the way to fold a paper to obtain the most complex figure. Dr. G defines the complexity of a figure by ...
[ { "submission_id": "aoj_1606_10853765", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld I...
aoj_1603_cpp
500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people a...
[ { "submission_id": "aoj_1603_11044339", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define rep...
aoj_1609_cpp
Look for the Winner! The citizens of TKB City are famous for their deep love in elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Now the voting has just been closed and the counting is going to start. The TKB citizens have strong desire to know the winner as...
[ { "submission_id": "aoj_1609_10810381", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint N;\nchar S[100009];\n\nvoid Testcase() {\n pair<int, char> votes[26], votes2[256];\n for (int i = 0; i < 26; i++) votes[i] = make_pair(0, 'A' + i);\n\n ...
aoj_1610_cpp
Bamboo Blossoms The bamboos live for decades, and at the end of their lives, they flower to make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in blossom in his travel to Tsukuba. He liked the flower so much that he was tempted to make a garden where the bamboos bloom annually. Dr. ACM sta...
[ { "submission_id": "aoj_1610_10848308", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\nusing ll = long long;\nconst double pi = acos(-1);\n#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)\n#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)\n#define R...
aoj_1614_cpp
Warp Drive The warp drive technology is reforming air travel, making the travel times drastically shorter. Aircraft reaching above the warp fields built on the ground surface can be transferred to any desired destination in a twinkling. With the current immature technology, however, building warp fields is quite expens...
[ { "submission_id": "aoj_1614_10579461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){...
aoj_1612_cpp
3D Printing We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction. First, usi...
[ { "submission_id": "aoj_1612_10578406", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct...
aoj_1611_cpp
Daruma Otoshi You are playing a variant of a game called "Daruma Otoshi (Dharma Block Striking)". At the start of a game, several wooden blocks of the same size but with varying weights are stacked on top of each other, forming a tower. Another block symbolizing Dharma is placed atop. You have a wooden hammer with its ...
[ { "submission_id": "aoj_1611_11035458", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n\nint main(){\n\n while(1){\n int n;\n cin >> n;\n if (n == 0) break;\n vector<int> w(n);\n for (int i=0; i<n; i++) cin >> w[i];\n\n vector<...
aoj_1605_cpp
Bridge Construction Planning There is a city consisting of many small islands, and the citizens live in these islands. Citizens feel inconvenience in requiring ferry rides between these islands. The city mayor decided to build bridges connecting all the islands. The city has two construction companies, A and B. The may...
[ { "submission_id": "aoj_1605_10719736", "code_snippet": "/*\n\nhttps://hitonanode.github.io/cplib-cpp/combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\n\nedit by noya2\n\n*/\n\n#line 1 \"combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\"\n#define PROBLEM \"https://j...
aoj_1615_cpp
Gift Exchange Party A gift exchange party will be held at a school in TKB City. For every pair of students who are close friends, one gift must be given from one to the other at this party, but not the other way around. It is decided in advance the gift directions, that is, which student of each pair receives a gift. N...
[ { "submission_id": "aoj_1615_10945981", "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_1613_cpp
Deciphering Characters Image data which are left by a mysterious syndicate were discovered. You are requested to analyze the data. The syndicate members used characters invented independently. A binary image corresponds to one character written in black ink on white paper. Although you found many variant images that ...
[ { "submission_id": "aoj_1613_10867681", "code_snippet": "#include <bits/stdc++.h>\n \n#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )\n#define REP(i,n) FOR(i,0,n)\n#define YYS(x,arr) for(auto& x:arr)\n#define ALL(x) (x).begin(),(x).end()\n#define SORT(x) sort( (x).begin(),(x).end() )\n#define RE...
aoj_1618_cpp
A Garden with Ponds Mr. Gardiner is a modern garden designer who is excellent at utilizing the terrain features. His design method is unique: he first decides the location of ponds and design them with the terrain features intact. According to his unique design procedure, all of his ponds are rectangular with simple ...
[ { "submission_id": "aoj_1618_10542183", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<string>\n#include<vector>\n#include<algorithm>\n#include<tuple>\n#include<map>\n#include<queue>\n#include<set>\n#include<unordered_set>\n#include<stack>\n#include<deque>\n#include<bitset>\n#include<cassert...
aoj_1624_cpp
Income Inequality We often compute the average as the first step in processing statistical data. Yes, the average is a good tendency measure of data, but it is not always the best. In some cases, the average may hinder the understanding of the data. For example, consider the national income of a country. As the term in...
[ { "submission_id": "aoj_1624_9210748", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n vector<int>ans(0);\n while(true){\n double n;\n cin >> n;\n if(n==0){\n break;\n }\n vector<double>A(n);\n for(int i=0;i<n;i++){\n...
aoj_1616_cpp
Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceeding...
[ { "submission_id": "aoj_1616_10840723", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m;\n cin >> n >> m;\n if(!n) break;\n\n vector<int> price(n);\n for(auto& i:price) cin >> i;\n\n int ans = -1;\n\n for(i...
aoj_1622_cpp
Go around the Labyrinth Explorer Taro got a floor plan of a labyrinth. The floor of this labyrinth is in the form of a two-dimensional grid. Each of the cells on the floor plan corresponds to a room and is indicated whether it can be entered or not. The labyrinth has only one entrance located at the northwest corner, w...
[ { "submission_id": "aoj_1622_10703013", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\ntemplate<typename flow>\nstruct max_flow {\n struct edge {\n int to;\n flow cap;\n int rev;\n };\n int V;\n vector<vector<edge> > G;\n vector<in...
aoj_1619_cpp
Making Lunch Boxes Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the book as possible. Enough of the ingredients for all the recipes are at hand, but they all are in vacuum packs of two. If only one of them is ...
[ { "submission_id": "aoj_1619_10849110", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<map>\n#include<vector>\n#include<string>\n#include<algorithm>\n\nusing namespace std;\n\nstring xx(string a, string b)\n{\n for(int i = 0; i < a.size(); i++)\n a[i] = (a[i]-'0')^(b[i]-'0')+'0';\...
aoj_1620_cpp
Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the Boolean expressions has terminals 0 1 a b c d - ^ * ( ) , start symbol <E> and the following production rule: <E>  ::= 0 | 1 | a | b | c | ...
[ { "submission_id": "aoj_1620_10853829", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<cstring>\nusing namespace std;\n\nconst int N = 16;\n\nchar S[N + 2];\nint T[N + 2], p;\nmap<char, int> M;\nconst char *al = \"abcd\";\nconst char *nu = ...
aoj_1623_cpp
Equivalent Deformation Two triangles T 1 and T 2 with the same area are on a plane. Your task is to perform the following operation to T 1 several times so that it is exactly superposed on T 2 . Here, vertices of T 1 can be on any of the vertices of T 2 . Compute the minimum number of operations required to superpose T...
[ { "submission_id": "aoj_1623_9769018", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v)...
aoj_1625_cpp
Origami, or the art of folding paper Master Grus is a famous origami (paper folding) artist, who is enthusiastic about exploring the possibility of origami art. For future creation, he is now planning fundamental experiments to establish the general theory of origami. One rectangular piece of paper is used in each of h...
[ { "submission_id": "aoj_1625_10854025", "code_snippet": "#include <iostream>\n#include <cstring>\n#define ll long long\n#define N 100000\n\nusing namespace std;\n\nint g[600][600], dx, dy;\n\nint main(void) {\n#ifdef _DEBUG\n\tfreopen(\"1.in\", \"r\", stdin);\n#else \n\tios::sync_with_stdio(0); cin.tie(0); ...
aoj_1628_cpp
Floating-Point Numbers In this problem, we consider floating-point number formats, data representation formats to approximate real numbers on computers. Scientific notation is a method to express a number, frequently used for numbers too large or too small to be written tersely in usual decimal form. In scientific nota...
[ { "submission_id": "aoj_1628_10670479", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(...
aoj_1627_cpp
Playoff by all the teams The Minato Mirai Football Association hosts its annual championship as a single round-robin tournament, in which each team plays a single match against all the others. Unlike many other round-robin tournaments of football, matches never result in a draw in this tournament. When the regular ti...
[ { "submission_id": "aoj_1627_10848400", "code_snippet": "#include<iostream>\n#include<cstring>\n#include<cstdio>\nusing namespace std;\nint n,m;\nint sum;\nint x,y;\nint a[100][100];\nint cnt[100];\nvoid show()\n{\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\tif(a[i][j]==-1) cout<<\"* \";els...
aoj_1626_cpp
Skyscraper "MinatoHarukas" Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent...
[ { "submission_id": "aoj_1626_10867577", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std ;\nint ans , p , n ;\nvoid deal( int x ){\n if ( ( n / x ) % 2 == 1 ){\n int y = x - ( n / x ) / 2 ;\n if ( y > 0 ){\n if ( n / x > ans ){\n ans = n / x ;\n ...