結果

問題 No.1999 Lattice Teleportation
ユーザー 👑 NachiaNachia
提出日時 2022-07-01 22:25:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,405 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 90,388 KB
実行使用メモリ 14,624 KB
最終ジャッジ日時 2023-08-17 09:55:46
合計ジャッジ時間 3,816 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 63 ms
14,456 KB
testcase_13 AC 124 ms
13,872 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 126 ms
14,296 KB
testcase_16 AC 48 ms
8,576 KB
testcase_17 AC 80 ms
14,624 KB
testcase_18 AC 71 ms
12,968 KB
testcase_19 AC 75 ms
12,740 KB
testcase_20 AC 124 ms
13,812 KB
testcase_21 AC 124 ms
13,356 KB
testcase_22 AC 126 ms
14,504 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 30 ms
7,512 KB
testcase_25 AC 69 ms
12,924 KB
testcase_26 AC 57 ms
8,624 KB
testcase_27 AC 47 ms
8,456 KB
testcase_28 AC 82 ms
13,500 KB
testcase_29 AC 40 ms
8,196 KB
testcase_30 AC 18 ms
5,392 KB
testcase_31 AC 14 ms
4,528 KB
testcase_32 AC 53 ms
8,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


const i64 INF = 1001001001001001001;
using modint = atcoder::static_modint<1000000007>;

struct Vec2 { long long x, y; };

std::vector<int> sort_points_by_argument(const std::vector<Vec2>& points) {
    std::vector<int> case_split[9];
    for (int i = 0; i < (int)points.size(); i++) {
        int case_id = 0;
        if (points[i].x == 0) case_id += 1;
        if (points[i].x > 0) case_id += 2;
        if (points[i].y == 0) case_id += 3;
        if (points[i].y > 0) case_id += 6;
        case_split[case_id].push_back(i);
    }
    auto sort_by_argument_small = [&points](int l, int r)->bool {
        return points[l].x * points[r].y - points[l].y * points[r].x > 0;
    };
    for (int t = 0; t < 9; t++) {
        std::sort(case_split[t].begin(), case_split[t].end(), sort_by_argument_small);
    }
    std::vector<int> res;
    res.reserve(points.size());
    for (int case_id : { 0, 1, 2, 4, 5, 8, 7, 6, 3 }) {
        std::copy(case_split[case_id].begin(), case_split[case_id].end(), std::back_inserter(res));
    }
    return res;
}

i64 GCD(i64 a, i64 b){
	return b ? GCD(b, a%b) : a;
}

i64 countLatticePointOnSegment(Vec2 p){
	return 1 + GCD(abs(p.x), abs(p.y));
}

int main(){
	int N; cin >> N;
	vector<Vec2> A(N);
	rep(i,N){
		cin >> A[i].x;
		cin >> A[i].y;
	}
	rep(i,A.size()){
		if(A[i].x == 0 && A[i].y == 0){
			swap(A[i], A.back());
			A.pop_back();
			i--;
		}
	}

	if(A.empty()){ cout << "1\n"; return 0; }
	if(A.size() == 1){ cout << countLatticePointOnSegment(A[0]) << '\n'; return 0; }

	for(int i=(int)A.size()-1; i>=0; i--) A.push_back({ -A[i].x, -A[i].y });
	vector<Vec2> B;
	for(auto i : sort_points_by_argument(A)) B.push_back(A[i]);

	modint ans = 0;

	modint x = 0;
	modint y = 0;
	for(auto p : B){
		ans += x * (y + p.y) - y * (x + p.x);
		x += p.x;
		y += p.y;
	}
	
	for(auto p : B){
		if(p.x == 0 || p.y == 0) ans += abs(p.x) + abs(p.y);
		else ans += GCD(abs(p.x), abs(p.y));
	}

	ans += 2;
	ans /= 2;

	cout << ans.val() << '\n';
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0