結果

問題 No.1999 Lattice Teleportation
ユーザー MasKoaTSMasKoaTS
提出日時 2022-03-21 22:05:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,860 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 89,860 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-04-25 22:59:33
合計ジャッジ時間 4,473 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 128 ms
8,660 KB
testcase_13 AC 173 ms
8,704 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 171 ms
8,832 KB
testcase_16 AC 64 ms
6,944 KB
testcase_17 AC 107 ms
8,192 KB
testcase_18 AC 100 ms
7,680 KB
testcase_19 AC 103 ms
8,192 KB
testcase_20 AC 166 ms
8,832 KB
testcase_21 AC 164 ms
8,628 KB
testcase_22 AC 170 ms
8,704 KB
testcase_23 AC 10 ms
6,940 KB
testcase_24 AC 38 ms
6,940 KB
testcase_25 AC 94 ms
7,808 KB
testcase_26 AC 77 ms
6,940 KB
testcase_27 AC 64 ms
6,944 KB
testcase_28 AC 113 ms
8,448 KB
testcase_29 AC 51 ms
6,940 KB
testcase_30 AC 23 ms
6,940 KB
testcase_31 AC 18 ms
6,944 KB
testcase_32 AC 71 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const ll mod = 1000000007;


ll floor_sum(ll n, ll m, ll a, ll b) {
	if (m == 0) {
		return 0;
	}
	ll res = 0;
	while (true) {
		if (a >= m or a < 0) {
			res += n * (n - 1) * (a / m) / 2;
			a %= m;
		}
		if (b >= m or b < 0) {
			res += n * (b / m);
			b %= m;
		}
		ll y_max = a * n + b;
		if (y_max < m) {
			break;
		}
		n = y_max / m;	b = y_max % m;
		m = a;	a = m;
	}
	return res;
}

ll gcd(ll a, ll b) {
	a = abs(a);	b = abs(b);
	while (b != 0) {
		ll r = a % b;
		a = b;
		b = r;
	}
	return a;
}

bool arg_sort(const V<ll>& v1, const V<ll>& v2) {
	ll x1 = v1[0], y1 = v1[1];
	ll x2 = v2[0], y2 = v2[1];
	return (x1 * y2 > x2 * y1);
}


int main(void) {
	int tmp, n = 0;	cin >> tmp;
	VV<ll> v(tmp, V<ll>(2));
	rep(i, 0, tmp) {
		ll x, y;	cin >> x >> y;
		if (x == 0 and y == 0) {
			continue;
		}
		if (y < 0) {
			x = -x;	y = -y;
		}
		v[n] = { x,y };
		n++;
	}
	v.resize(n);
	sort(all(v), arg_sort);

	ll ans = 1;
	for (auto& p : v) {
		ans += gcd(p[0], p[1]);
		ans %= mod;
	}

	ll last = 0;
	for (auto itr = rbegin(v); itr != rend(v); ++itr) {
		ll x = (*itr)[0], y = (*itr)[1];
		ll a = 1;
		ll s = 0;
		if (x < 0) {
			a = -1;
			x = -x;
		}
		s += floor_sum(x, x, y, 0) % mod + x * last % mod;
		s %= mod;
		ans += mod + a * s;
		ans %= mod;
		last += mod + y;
		last %= mod;
	}

	last = 0;
	for (auto& p : v) {
		ll x = p[0], y = p[1];
		ll a = -1;
		ll s = 0;
		if (x < 0) {
			a = 1;
			x = -x;
		}
		s += floor_sum(x, x, y, 0) % mod + x * last % mod;
		s %= mod;
		ans += mod + a * s;
		ans %= mod;
		last += mod + y;
		last %= mod;
	}

	cout << ans << endl;
	return 0;
}
0