結果
| 問題 | No.3788 Floor Sum Queries |
| コンテスト | |
| ユーザー |
rieaaddlreiuu
|
| 提出日時 | 2026-11-29 23:47:42 |
| 言語 | C++14 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 713 ms / 2,000 ms |
| + 999µs | |
| コード長 | 7,064 bytes |
| 記録 | |
| コンパイル時間 | 1,375 ms |
| コンパイル使用メモリ | 198,296 KB |
| 実行使用メモリ | 9,840 KB |
| 平均クエリ数 | 267.67 |
| 最終ジャッジ日時 | 2026-11-29 23:47:50 |
| 合計ジャッジ時間 | 7,955 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 |
コンパイルメッセージ
main.cpp: In function 'll solve()':
main.cpp:143:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
143 | for (auto [a, b] : qs) Cs.push_back(ask(a, b));
| ^
main.cpp:147:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
147 | auto [a, b] = QUERIES[q];
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// sum_{i=0}^{n-1} floor((a*i + b) / m) (a, b >= 0, n >= 0, m >= 1)
ll f(ll a, ll b, ll n, ll m) {
ll result = 0;
while (true) {
if (a >= m) {
result += (n - 1) * n / 2 * (a / m);
a %= m;
}
if (b >= m) {
result += n * (b / m);
b %= m;
}
ll y_max = a * n + b;
if (y_max < m) break;
n = y_max / m;
b = y_max % m;
swap(m, a);
}
return result;
}
const ll LIM = 1000000; // N, M, A, B の上限
// 候補: n ごとに「まだあり得る m の区間 [lo, hi]」
struct Cand {
ll n, lo, hi;
};
// f(a,b,n,m) は m について単調非増加なので、[lo,hi] 内で f == C となる m は区間。
// それを求めて [lo,hi] を更新する。空なら false。
bool refine(ll a, ll b, ll C, ll n, ll &lo, ll &hi) {
// L = f <= C となる最小の m
ll l = lo, r = hi + 1;
while (l < r) {
ll mid = (l + r) / 2;
if (f(a, b, n, mid) <= C) r = mid;
else l = mid + 1;
}
ll L = l;
if (L > hi || f(a, b, n, L) != C) return false; // f == C となる m が無い
// R = f >= C となる最大の m(L 以上で探せばよい)
l = L, r = hi;
while (l < r) {
ll mid = (l + r + 1) / 2;
if (f(a, b, n, mid) >= C) l = mid;
else r = mid - 1;
}
ll R = l;
if (L > R) return false;
lo = L, hi = R;
return true;
}
// クエリ (a,b) の答えが C のとき、n に対して m があり得る範囲を O(1) で絞る。
// X = a*n(n-1)/2 + b*n とすると X/m - n < C <= X/m より X/(C+n) < m <= X/C
void quick_bound(ll a, ll b, ll C, ll n, ll &lo, ll &hi) {
ll X = n * (n - 1) / 2 * a + n * b;
lo = max(lo, X / (C + n) + 1);
if (C > 0) hi = min(hi, X / C);
}
// 最初の 2 クエリの答えから、全 n について候補区間を作る。
// 両クエリの O(1) の範囲の共通部分が空でない n だけ二分探索で精査する。
vector<Cand> init_candidates(const vector<pair<ll, ll>> &qs, const vector<ll> &Cs) {
vector<Cand> cand;
// B = LIM >= M なので各項 >= 1。よって (A,B)=(1,LIM) の答え >= N、
// (LIM,LIM) の答え = Σ_{j=1}^{N} floor(LIM*j/M) >= N(N+1)/2。n の上限をここで切る。
ll nmax = LIM;
for (size_t k = 0; k < qs.size(); k++) {
if (qs[k].second != LIM) continue;
if (qs[k].first == 1) nmax = min(nmax, Cs[k]);
if (qs[k].first == LIM) nmax = min(nmax, (ll)sqrtl(2.0L * Cs[k]) + 1);
}
for (ll n = 1; n <= nmax; n++) {
ll lo = 1, hi = LIM;
for (size_t k = 0; k < qs.size() && lo <= hi; k++) quick_bound(qs[k].first, qs[k].second, Cs[k], n, lo, hi);
if (lo > hi) continue;
// A=1 のクエリの f はすぐ終わる(ループ 2 回程度)ので、軽い後ろのクエリから精査して早めに落とす
bool ok = true;
for (size_t k = qs.size(); k-- > 0 && ok;) ok = refine(qs[k].first, qs[k].second, Cs[k], n, lo, hi);
if (ok) cand.push_back({n, lo, hi});
}
return cand;
}
void filter_candidates(vector<Cand> &cand, ll a, ll b, ll C) {
vector<Cand> next;
for (auto c : cand) {
if (refine(a, b, C, c.n, c.lo, c.hi)) next.push_back(c);
}
cand.swap(next);
}
// ---- 入出力 ----
#ifdef LOCAL
ll hidden_N, hidden_M;
int query_count;
ll ask(ll a, ll b) {
query_count++;
return f(a, b, hidden_N, hidden_M);
}
bool answer(ll n) { return n == hidden_N; }
#else
ll ask(ll a, ll b) {
cout << "? " << a << " " << b << endl; // endl で flush
ll R;
cin >> R;
if (R == -1) exit(0);
return R;
}
bool answer(ll n) {
cout << "! " << n << endl;
ll r;
cin >> r;
if (r == -1) exit(0);
return true;
}
#endif
// 投げるクエリの順番(N が確定した時点で打ち切る)
const vector<pair<ll, ll>> QUERIES = {
{LIM, LIM}, // 初手: ほとんどこれで大きく絞れる
{1, LIM}, // 2 手目: ランダムなら 2 手でほぼ確定
{LIM, 0}, // 3 手目: N が小さく M≈5e5 の紛れ込みを消す
{LIM / 2, 0}, // 以下は念のための予備
{2, LIM},
{1000, LIM},
{LIM / 2, LIM},
{3, LIM},
{1, LIM / 2},
{999983, 123457},
};
ll solve() {
// 最初の 2 クエリはまとめて投げる(R1/R2 ≈ (N+1)/2 なので、2 つ合わせると n がほぼ O(1) で絞れる)
const size_t FIRST = 2;
vector<pair<ll, ll>> qs(QUERIES.begin(), QUERIES.begin() + FIRST);
vector<ll> Cs;
for (auto [a, b] : qs) Cs.push_back(ask(a, b));
vector<Cand> cand = init_candidates(qs, Cs);
// cand は n ごとに 1 要素なので size == 異なる N の数
for (size_t q = FIRST; q < QUERIES.size() && cand.size() > 1; q++) {
auto [a, b] = QUERIES[q];
filter_candidates(cand, a, b, ask(a, b));
}
if (cand.empty()) return 1; // 起こらないはず
// 決まらなかった場合は m の区間が最も広い n を答える
auto best = *max_element(cand.begin(), cand.end(),
[](const Cand &x, const Cand &y) { return x.hi - x.lo < y.hi - y.lo; });
return best.n;
}
int main() {
#ifdef LOCAL
// テスト: ./a.out <ケース数> <seed> [smallN]
int T = 200, seed = 1;
bool smallN = false;
// 引数なしならデフォルト
if (const char *s = getenv("T_CASES")) T = atoi(s);
if (const char *s = getenv("SEED")) seed = atoi(s);
if (getenv("SMALLN")) smallN = true;
mt19937_64 rng(seed);
uniform_int_distribution<ll> dist(1, LIM);
int wrong = 0, maxq = 0;
map<int, int> qhist;
auto st = chrono::steady_clock::now();
for (int t = 0; t < T; t++) {
hidden_N = smallN ? 1 + (ll)(rng() % 60) : dist(rng);
hidden_M = dist(rng);
if (const char *s = getenv("MODE")) {
string mode = s;
ll small = 1 + (ll)(rng() % 60), big = LIM - (ll)(rng() % 60);
if (mode == "smallM") hidden_M = small;
if (mode == "bigN") hidden_N = big;
if (mode == "bigM") hidden_M = big;
if (mode == "smallN_bigM") hidden_N = small, hidden_M = big;
if (mode == "bigN_smallM") hidden_N = big, hidden_M = 1 + (ll)(rng() % 60);
if (mode == "bigN_bigM") hidden_N = big, hidden_M = LIM - (ll)(rng() % 60);
}
query_count = 0;
ll n = solve();
if (!answer(n)) {
wrong++;
printf("WRONG: N=%lld M=%lld answered %lld\n", hidden_N, hidden_M, n);
}
qhist[query_count]++;
maxq = max(maxq, query_count);
}
double sec = chrono::duration<double>(chrono::steady_clock::now() - st).count();
printf("T=%d wrong=%d maxq=%d time/case=%.4fs\n", T, wrong, maxq, sec / T);
for (auto [k, v] : qhist) printf(" %d queries: %d\n", k, v);
#else
int T;
cin >> T;
while (T--) answer(solve());
#endif
}
rieaaddlreiuu