結果

問題 No.3054 Modulo Inequalities
ユーザー ecottea
提出日時 2025-04-16 18:31:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 13,505 bytes
コンパイル時間 4,564 ms
コンパイル使用メモリ 260,180 KB
実行使用メモリ 83,308 KB
最終ジャッジ日時 2025-04-16 18:32:40
合計ジャッジ時間 41,152 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 TLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

using mint = modint998244353;
//using mint = static_modint<(int)1e9 + 7>;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif


//【商列挙(組)】O(√max(n1,n2))
/*
* 区間 [1..max(n1,n2)] を (n1/i, n2/i) = (q1, q2)(切り捨て)となる半開区間 i∈(il..ir] に分割し,
* i について昇順にそれぞれに対して f(il, ir, q1, q2) を呼び出す.
* なお各範囲においては (n1 mod i, n2 mod i) は公差 (-q1, -q2) の等差数列を成す.
*/
template <class T, class FUNC>
void quotient_range(T n1, T n2, const FUNC& f) {
	// verify : https://atcoder.jp/contests/tupc2022/tasks/tupc2022_i

	T n = max(n1, n2);

	T sqrt_n = (T)(sqrt(n) - 1e-12);

	// どちらかの q に対応する i が高々 1 個の部分は i ごとに愚直に考える.
	T i_max = n / (sqrt_n + 1);
	for (T i = 1; i <= i_max; ++i) f(i - 1, i, n1 / i, n2 / i);

	// そうでない部分は (q1, q2) ごとにまとめて考える.
	T q1 = n1 / (i_max + 1), q2 = n2 / (i_max + 1);
	while (q1 > 0 || q2 > 0) {
		// (il1..ir1] : n1/i = q1 となる i の範囲
		T il1 = n1 / (q1 + 1), ir1 = (q1 > 0 ? n1 / q1 : (T)INFL);

		// (il2..ir2] : n2/i = q2 となる i の範囲
		T il2 = n2 / (q2 + 1), ir2 = (q2 > 0 ? n2 / q2 : (T)INFL);

		// 両区間の共通部分を求める.
		T il = max(il1, il2), ir = min(ir1, ir2);
		if (il < ir) f(il, ir, q1, q2);

		if (ir1 < ir2) q1--;
		else q2--;
	}

	/* f の定義の雛形
	using T = ll;
	auto f = [&](T il, T ir, T q1, T q2) {

	};
	quotient_range(n1, n2, f);
	*/
}


//【切り捨て除算】O(1)
/*
* a, b の正負によらず,数学的な floor(a / b) を返す.
*/
template <class T>
T floor_div(T a, T b) {
	// verify : https://atcoder.jp/contests/abc315/tasks/abc315_g

	Assert(b != 0);

	// 分母が負の場合は,分子と分母に -1 を掛けて分母を正にする.
	if (b < 0) { a *= -1; b *= -1; };

	// 分子が非負の場合は,a / b で切り捨てになる.
	if (a >= 0) return a / b;
	// 分子が負の場合は,左右反転して切り上げ商を計算し,再度左右反転する.
	else return -((-a + b - 1) / b);
}


int TLE(int n, vi a, vi b) {
	int M = (int)1e6 + 1;
	dump(M = 100);

	vi imos(M + 1);

	rep(i, n) {
		dump("-------------- i:", i, "----------------");

		using T = int;
		auto f = [&](T il, T ir, T q1, T q2) {
			dump("il, ir, q1, q2:", il, ir, q1, q2);
			T b1 = a[i] % ir;
			T b2 = b[i] % ir;
			T a1 = q1;
			T a2 = q2;
			T D = ir - il;

			// a1 x + b1 < a2 x + b2  (0 <= x < D)
			// (a1 - a2)x < (b2 - b1)
			if (a1 == a2) {
				if (b2 - b1 > 0) {
					imos[ir]++;
					imos[il]--;
					dump(il, ir);
				}
			}
			else if (a1 > a2) {
				T x_max = floor_div(b2 - b1 - 1, a1 - a2);
				chmin(x_max, D - 1);
				if (x_max >= 0) {
					imos[ir]++;
					imos[ir - x_max - 1]--;
					dump(ir - x_max - 1, ir);
				}
			}
			else {
				T x_lb = floor_div(b1 - b2, a2 - a1);
				chmax(x_lb, -1);
				if (x_lb <= D - 2) {
					imos[ir - x_lb - 1]++;
					imos[il]--;
					dump(il, ir - x_lb - 1);
				}
			}
		};
		quotient_range(a[i], b[i], f); // さすがに間に合わなさそう

		if (a[i] < b[i]) {
			imos[M]++;
			imos[b[i]]--;
			dump(b[i], M);
		}
	}

	repir(j, M - 1, 0) imos[j] += imos[j + 1];
	dump(imos);

	int f_max = -INF, m_max = -1;

	repi(m, 1, M) if (chmax(f_max, imos[m])) m_max = m;

	return m_max;
}


//【切り上げ除算】O(1)
/*
* a, b の正負によらず,数学的な ceil(a / b) を返す.
*/
template <class T>
T ceil_div(T a, T b) {
	// verify : https://atcoder.jp/contests/abc378/tasks/abc378_b

	Assert(b != 0);

	// 分母が負の場合は,分子と分母に -1 を掛けて分母を正にする.
	if (b < 0) { a *= -1; b *= -1; };

	// 分子が非負の場合は,(a + b - 1) / b で切り上げになる.
	if (a >= 0) return (a + b - 1) / b;
	// 分子が負の場合は,左右反転して切り捨て商を計算し,再度左右反転する.
	else return -((-a) / b);
}


int naive(int n, vi a, vi b) {
	int M = (int)1e6 + 1;
	dump(M = 10);

	ll f_max = -INFL; int m_max = -1;

	repi(m, 1, M) {
		ll f = 0;
		rep(i, n) {
			f += ceil_div(b[i] - a[i], m);
			f -= b[i] / m;
			f += a[i] / m;
		}
		dump("m:", m, "f:", f);

		if (chmax(f_max, f)) m_max = m;
	}

	return m_max;
}


//【余りの和】
/*
* Mod_sum(vl a) : O(n log n)
*	整数列 a[0..n) で初期化する.
*
* ll mod_sum(ll m) : O(max(a) log(n) / m)
*	Σi∈[0..n) (a[i] mod m) を返す.
*
* ll lack_sum(ll m) : O(max(a) log(n) / m)
*	a[0..n) を m で割った不足の和を返す.
*
* ll floor_sum(ll m) : O(max(a) log(n) / m)
*	Σi∈[0..n) floor(a[i] / m) を返す.
*
* ll ceil_sum(ll m) : O(max(a) log(n) / m)
*	Σi∈[0..n) ceil(a[i] / m) を返す.
*/
template <class T>
class Mod_sum {
	int n;
	vector<T> a;
	vl acc;

public:
	// 整数列 a[0..n) で初期化する.
	Mod_sum(const vector<T>& a_) : n(sz(a_)), a(a_), acc(n + 1) {
		// verify : https://atcoder.jp/contests/arc126/tasks/arc126_c

		sort(all(a));
		rep(i, n) acc[i + 1] = acc[i] + a[i];
	}
	Mod_sum() : n(0) {}

	// Σi∈[0..n) (a[i] mod m) を返す.
	ll mod_sum(T m) const {
		ll res = 0;

		T v = a[0] - smod(a[0], m);
		int pi = 0;

		while (pi < n) {
			int i = lbpos(a, v + m);
			res += (acc[i] - acc[pi]) - (ll)v * (i - pi);

			v += m;
			pi = i;
		}

		return res;
	}

	// a[0..n) を m で割った不足の和を返す.
	ll lack_sum(T m) const {
		// verify : https://atcoder.jp/contests/arc126/tasks/arc126_c

		ll res = 0;

		T v = a[0] + smod(-a[0], m);
		int pi = 0;

		while (pi < n) {
			int i = ubpos(a, v);
			res += (ll)v * (i - pi) - (acc[i] - acc[pi]);

			v += m;
			pi = i;
		}

		return res;
	}

	// Σi∈[0..n) floor(a[i] / m) を返す.
	ll floor_sum(T m) const {
		return (acc[n] - mod_sum(m)) / m;
	}

	// Σi∈[0..n) ceil(a[i] / m) を返す.
	ll ceil_sum(T m) const {
		return (acc[n] + lack_sum(m)) / m;
	}
};


int TLE2(int n, vi a, vi b) {
	int M = (int)1e6 + 1;
//	dump(M = 10);

	vi d(n);
	rep(i, n) d[i] = b[i] - a[i];

	Mod_sum A(a), B(b), D(d);

	ll f_max = -INFL; int m_max = -1;

	repi(m, 1, M) {
		ll f = 0;
		f += D.ceil_sum(m); // 無駄な log がある.たぶんだめ.
		f -= B.floor_sum(m);
		f += A.floor_sum(m);
//		dump("m:", m, "f:", f);

		if (chmax(f_max, f)) m_max = m;
	}

	return m_max;
}


//【整数商畳込み(分母ごと)】O(m + n log n)
/*
* 与えられた a[0..n), b[0..m) に対して
*       c[k] = Σ(floor(i/k) = j) a[i] b[j]
* なる c[0..n] を返す.
*/
template<class T>
vector<T> floordiv_convolution_by_dnm(const vector<T>& a, const vector<T>& b) {
	//【方法】
	// 和の範囲についての条件は
	//		floor(i/k) = j
	//		⇔ j ≦ i/k < j+1
	//		⇔ j k ≦ i < j k + k
	// と書き直せるので,
	//		c[k]
	//		= Σj∈[0..m) Σi∈[j k..j k + k) a[i] b[j]
	//		= Σj∈[0..m) b[j] Σa[j k..j k + k)
	// となる.a の累積和を前計算しておけば,
	// k ごとに独立に c[k] を計算しても計算量は調和級数で抑えられる.

	int n = sz(a), m = sz(b);

	vector<T> acc_a(n + 1);
	rep(i, n) acc_a[i + 1] = acc_a[i] + a[i];

	vector<T> c(n + 1);
	repi(k, 1, n) {
		repi(j, 1, min(m - 1, n / k)) {
			c[k] += b[j] * (acc_a[min(j * k + k, n)] - acc_a[j * k]);
		}
	}

	return c;
}


int solve(int n, vi a, vi b) {
	int M = (int)1e6;
//	dump(M = 10);

	vi d(n);
	rep(i, n) d[i] = b[i] - a[i];

	vl cA(M + 1), cB(M + 1), cD(2 * M + 1);
	rep(i, n) {
		cA[a[i]]++;
		cB[b[i]]++;
		cD[d[i] + M]++;
	}

	vl id(2 * M + 1);
	iota(all(id), 0LL);

	auto fA = floordiv_convolution_by_dnm(cA, id);
	auto fB = floordiv_convolution_by_dnm(cB, id);
	//auto fD = floordiv_convolution_by_dnm(cD, id);
	Mod_sum D(d);

	ll f_max = -INFL; int m_max = -1;

	repi(m, 1, M + 1) {
		ll f = 0;
		f += D.ceil_sum(m); // 無駄な log がある.たぶんだめ.
		f -= fB[m]; // とりあえずこっちは直した
		f += fA[m];
//		dump("m:", m, "f:", f);

		if (chmax(f_max, f)) m_max = m;
	}

	return m_max;
}


void zikken() {
	repi(x, 1, 10) repi(y, 1, 10) repi(z, 1, 10) {
		if (x / y != z && x / z != y) continue;
		dump(x, y, z, ":", x / y == z, x / z == y);
	}

	exit(0);
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

//	zikken();

	int n;
	cin >> n;

	vi a(n), b(n);
	rep(i, n) cin >> a[i] >> b[i];

//	dump(TLE(n, a, b)); dump("=====");
//	dump(naive(n, a, b)); dump("=====");

	EXIT(solve(n, a, b));
}
0