QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#166791#5. 在线 O(1) 逆元OneWan60 4442ms22744kbC++201.6kb2023-09-06 18:15:532024-11-05 21:52:15

Judging History

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

  • [2024-11-05 21:52:15]
  • 管理员手动重测本题所有提交记录
  • 测评结果:60
  • 用时:4442ms
  • 内存:22744kb
  • [2023-09-06 18:15:54]
  • 评测
  • 测评结果:70
  • 用时:662ms
  • 内存:22880kb
  • [2023-09-06 18:15:53]
  • 提交

answer

#include <bits/stdc++.h>
#include "inv.h"
using namespace std;

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,unroll-loops")

// 2023 OneWan

struct Inverse {
	int B, B2, mod;
	vector<int> inv, pre, suf;
	vector<pair<int, int>> s;
	Inverse() = default;
	Inverse(int mod) : mod(mod) {
		B = pow(mod, 0.3333333333);
		B2 = B * B;
		inv.resize(mod / B + 1);
		inv[1] = 1;
		for (int i = 2, j = mod / B ; i <= j ; i++) {
			inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
		}
		s.resize (B2 + 1);
		pre.resize(B2 + 1);
		suf.resize(B2 + 1);
		s[0] = {0, 1};
		s[B2] = {1, 1};
		pre[B2] = suf[B2] = B2;
		for (int i = 2 ; i <= B ; i++) {
			for (int j = 1 ; j < i ; j++) {
				int pos = 1LL * j * B2 / i;
				if (pre[pos]) continue;
				pre[pos] = suf[pos] = pos;
				s[pos] = {j, i};
			}
		}
		for (int i = 1 ; i <= B2 ; i++) {
			if (pre[i] == 0) {
				pre[i] = pre[i - 1];
			}
		}
		for (int i = B2 ; i > 0 ; i--) {
			if (suf[i] == 0) {
				suf[i] = suf[i + 1];
			}
		}
	}
	int calc(int T, pair<int, int> x) {
		long long pos = 1LL * T * x.second;
		if (llabs(pos - 1LL * mod * x.first) > mod / B) return -1;
		pos %= mod;
		if (pos <= mod / B) return 1LL * x.second * inv[pos] % mod;
		return mod - 1LL * x.second * inv[mod - pos] % mod;
	}
	int operator()(int x) {
		x %= mod;
		if (x <= mod / B) return inv[x];
		int pos = 1LL * x * B2 / mod, res = calc(x, s[pre[pos]]);
		if (res == -1) res = calc(x, s[suf[pos]]);
		return res;
	}
};

Inverse invs(998244353);

void init(int p) {
}

int inv(int x) {
	return invs(x);
}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 23ms
memory: 22744kb

Test #2:

score: 20
Accepted
time: 908ms
memory: 22708kb

Test #3:

score: 30
Accepted
time: 4442ms
memory: 22724kb

Test #4:

score: 0
Time Limit Exceeded

Test #5:

score: 0
Time Limit Exceeded