QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#650674#6188. Elliptic Curve ProblemecotteaAC ✓1611ms3868kbC++1412.5kb2024-10-18 16:04:552024-10-18 16:04:56

Judging History

你现在查看的是最新测评结果

  • [2024-10-18 16:04:56]
  • 评测
  • 测评结果:AC
  • 用时:1611ms
  • 内存:3868kb
  • [2024-10-18 16:04:55]
  • 提交

answer

#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<1000000007>;
//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; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
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(...)
#define dump_list(v)
#define dump_mat(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(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);
}


//【切り上げ除算】O(1)
/*
* a, b の正負によらず,数学的な ceil(a / b) を返す.
*/
template <class T>
T ceil_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 - 1) / b で切り上げになる.
	if (a >= 0) return (a + b - 1) / b;
	// 分子が負の場合は,左右反転して切り捨て商を計算し,再度左右反転する.
	else return -((-a) / b);
}


//【格子点の個数】O(log|x2 - x1|)(の改変)
/*
* x 軸以上かつ (x1, y1) と (x2, y2) を結ぶ閉線分以下にある格子点の個数を返す.
* 条件:x1 ≠ x2, y1 ≧ 0, y2 ≧ 0
*/
template <class T>
T count_lattice_point_le(T x1, T x2, T y2, T y1, T g) {
	// verify : https://atcoder.jp/contests/typical90/tasks/typical90_ao

	//T res = (x2 - x1 + 1) * (y2 - y1 + 1);
	//res += g + 1;
	//res /= 2;
	//res += (x2 - x1 + 1) * y1;
	//res -= y1 + 1;
	//res -= x2 - x1;

	T res = (g - y1 + y2 + (x2 - x1) * (y1 + y2 - 1)) / 2;

	return res;
}


template <class T>
T naive(T p, T l, T r) {
	T res = 0;

	repi(i, 1, (p - 1) / 2) {
		T val = T(i) * i % p;
		res += l <= val && val <= r;
	}

	return res;
}


//【凸集合の中の格子点の凸包(第一象限)】O(?)(の改変)
/*
* 第一象限 [0..∞)×[0..∞) 内の凸集合 S で境界が y 軸から x 軸までを単調減少に結ぶものを考える.
* S 内の格子点の上側凸包を成す点 {x, y} を x 座標昇順に並べたリストを返す.
*
* bool insideQ(T x, T y) :
*	点 (x, y) が S に属するかを返す.
*
* bool crossQ(T x, T y, T dx, T dy) :
*	点 (x, y) を通り (dx, dy) 方向に伸びる直線が S と共有点を持つかを返す.
*
* T next_in(T x, T y, T dx, T dy) :
*	点 (x, y)∈S に対し,(x + k dx, y + k dy)∈S となる最大の k を返す.
*
* T next_out(T x, T y, T dx, T dy) :
*	点 (x, y)!∈S に対し,閉線分 (x, y)-(x + k dx, y + kdy) が S と共有点をもつ最小の k を返す.
*/
template <class T, class F1, class F2, class F3, class F4>
ull convex_hull_in_convex_set(const F1& insideQ, const F2& crossQ, const F3& next_in, const F4& next_out) {
	// 参考 : https://259-momone.hatenablog.com/entry/2021/07/25/025655
	// verify : https://mojacoder.app/users/YSatUT/problems/fermats_4nplus1_theorem

	T x = T(0);
	T y = T(0) + T(1) * next_in(T(0), T(0), T(0), T(1));

	stack<tuple<T, T, T, T>> SBT;
	SBT.emplace(T(0), T(-1), T(1), T(0));
	SBT.emplace(T(1), T(0), T(1), T(0));

	ull res = 0;

	while (true) {
//		dump("---------------------------------");

//		dump(x, y); dump(SBT); dump(res);
		
		auto [dx_in, dy_in, dx_out, dy_out] = SBT.top(); SBT.pop();

		auto k = next_in(x, y, dx_in, dy_in);
		if (y + k * dy_in < 0) {
			k = y / (-dy_in);
			if (k > 0) {
				ll nx = x + k * dx_in;
				ll ny = y + k * dy_in;
				res += count_lattice_point_le(x, nx, y, ny, k);
			}
			break;
		}

		ll nx = x + k * dx_in;
		ll ny = y + k * dy_in;
		res += count_lattice_point_le(x, nx, y, ny, k);

		x = nx;
		y = ny;

		while (!SBT.empty()) {
			tie(dx_in, dy_in, dx_out, dy_out) = SBT.top();
			if (insideQ(x + dx_in, y + dy_in)) break;
			SBT.pop();
		}
		if (SBT.empty()) break;

//		dump(x, y); dump(SBT); dump(res);

		while (y + dy_out >= T(0)) {
			T dx_io = dx_in + dx_out;
			T dy_io = dy_in + dy_out;

			if (y + dy_io >= T(0) && insideQ(x + dx_io, y + dy_io)) {
				T k = next_in(x + dx_io, y + dy_io, dx_out, dy_out);

				T ndx_in = dx_io + k * dx_out;
				T ndy_in = dy_io + k * dy_out;
				T ndx_out = dx_io + (k + T(1)) * dx_out;
				T ndy_out = dy_io + (k + T(1)) * dy_out;

				dx_in = ndx_in;
				dy_in = ndy_in;
				dx_out = ndx_out;
				dy_out = ndy_out;
			}
			else if (crossQ(x + dx_out, y + dy_out, dx_in, dy_in)) {
				T k = next_out(x + dx_out, y + dy_out, dx_in, dy_in);
				if (k <= 0) break;

				T ndx_in = dx_in * k + dx_out;
				T ndy_in = dy_in * k + dy_out;
				if (y + ndy_in < T(0) || !insideQ(x + ndx_in, y + ndy_in)) break;

				T ndx_out = dx_in * (k - T(1)) + dx_out;
				T ndy_out = dy_in * (k - T(1)) + dy_out;

				dx_in = ndx_in;
				dy_in = ndy_in;
				dx_out = ndx_out;
				dy_out = ndy_out;
			}
			else break;

			SBT.emplace(dx_in, dy_in, dx_out, dy_out);
	
//			dump(x, y); dump(SBT); dump(res);
		}
	}

	return res;
}


template <class T>
ull solve_sub(ll p, T c) {
	T pp = T(p) * p;

	auto insideQ = [&](T x, T y) {
		return p * y <= -x * x + c;
	};

	auto crossQ = [&](T x, T y, T dx, T dy) {
		T D = dy * dy * pp + 4 * dx * (c * dx + dy * p * x - dx * p * y);
		return D >= 0;
	};

	auto next_in = [&](T x, T y, T dx, T dy) {
		if (dx == 0) {
			return (ll)floor_div(c - x * x - p * y, dy * p);
		}
		T D = dy * dy * pp + 4 * dx * (c * dx + dy * p * x - dx * p * y);
		long double kd = ((long double)(-dy * p - 2 * dx * x) + sqrtl((long double)D)) / (long double)(2 * dx * dx);
		ll k = (ll)floor(kd);
		return k;
	};

	auto next_out = [&](T x, T y, T dx, T dy) {
		if (dx == 0) {
			return (ll)ceil_div(c - x * x - p * y, dy * p);
		}
		T D = dy * dy * pp + 4 * dx * (c * dx + dy * p * x - dx * p * y);
		long double kd = ((long double)(-dy * p - 2 * dx * x) - sqrtl((long double)D)) / (long double)(2 * dx * dx);
		ll k = (ll)ceil(kd);
		return k;
	};

	return convex_hull_in_convex_set<ll>(insideQ, crossQ, next_in, next_out);	
}


template <class T>
ull solve(ll p, ll l, ll r) {
	T R = (p - 1) / 2;
	dump("R:", R);

	T s = (R * R + p - 1) / p;

	ull res = solve_sub(p, p * s + r);
	dump(res);
	
	res -= solve_sub(p, p * s + l - 1);

	return res;
}


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

	ll p, l, r;
	cin >> p >> l >> r;

	dump("p, l, r:", p, l, r); dump("----------------");

	if (0) {
		auto start = chrono::system_clock::now();

		auto res_naive = naive<ll>(p, l, r);
		cout << res_naive << endl;

		auto now = chrono::system_clock::now();
		auto msec = chrono::duration_cast<chrono::milliseconds>(now - start).count();
		cout << msec << " msec" << endl;
	}

	dump("----------------");

	if (0) {
		auto start = chrono::system_clock::now();

//		mute_dump = 1;
		auto res_solve = solve<__int128>(p, l, r);
//		mute_dump = 0;
		
		cout << res_solve << endl;

		auto now = chrono::system_clock::now();
		auto msec = chrono::duration_cast<chrono::milliseconds>(now - start).count();
		cout << msec << " msec" << endl;
	}

	cout << solve<__int128>(p, l, r) << endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3868kb

input:

11 3 8

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 76ms
memory: 3604kb

input:

998244353 11451400 919810000

output:

454174074

result:

ok 1 number(s): "454174074"

Test #3:

score: 0
Accepted
time: 1576ms
memory: 3656kb

input:

96311898227 25437319919 55129361817

output:

14846091352

result:

ok 1 number(s): "14846091352"

Test #4:

score: 0
Accepted
time: 1530ms
memory: 3528kb

input:

93361455259 23166562299 23393760915

output:

113606479

result:

ok 1 number(s): "113606479"

Test #5:

score: 0
Accepted
time: 1556ms
memory: 3608kb

input:

95670332497 15858139735 18812394512

output:

1477133816

result:

ok 1 number(s): "1477133816"

Test #6:

score: 0
Accepted
time: 1541ms
memory: 3616kb

input:

94221254297 78612110347 90331192055

output:

5859602618

result:

ok 1 number(s): "5859602618"

Test #7:

score: 0
Accepted
time: 1524ms
memory: 3532kb

input:

92756073587 18915851957 32881684894

output:

6982950261

result:

ok 1 number(s): "6982950261"

Test #8:

score: 0
Accepted
time: 1539ms
memory: 3524kb

input:

93651628361 3508055978 32362767220

output:

14427310592

result:

ok 1 number(s): "14427310592"

Test #9:

score: 0
Accepted
time: 1572ms
memory: 3528kb

input:

97506758381 48269906857 58513759044

output:

5121898347

result:

ok 1 number(s): "5121898347"

Test #10:

score: 0
Accepted
time: 1609ms
memory: 3656kb

input:

99954950231 20710324571 44996152988

output:

12142951150

result:

ok 1 number(s): "12142951150"

Test #11:

score: 0
Accepted
time: 1603ms
memory: 3580kb

input:

99367158071 38747300608 85189731653

output:

23221174712

result:

ok 1 number(s): "23221174712"

Test #12:

score: 0
Accepted
time: 1611ms
memory: 3644kb

input:

99936206351 6721710119 93710740278

output:

43494512281

result:

ok 1 number(s): "43494512281"