QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#534360#5. 在线 O(1) 逆元surgutti100 ✓4719ms14752kbC++14971b2024-08-27 04:47:192024-11-05 22:03:28

Judging History

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

  • [2024-11-05 22:03:28]
  • 管理员手动重测本题所有提交记录
  • 测评结果:100
  • 用时:4719ms
  • 内存:14752kb
  • [2024-08-27 04:47:20]
  • 评测
  • 测评结果:100
  • 用时:4065ms
  • 内存:14404kb
  • [2024-08-27 04:47:19]
  • 提交

answer

#include "inv.h"

#include <cmath>
#include <cassert>
#include <cstdio>

constexpr int mod = 998244353;
constexpr int n = 1024;
constexpr int n2 = n * n;
constexpr int mod_n = mod / n;

int p[n2 + 1];
int f[n2 + 1];
int inv_[mod_n + 1];

void init(int _p) {
	assert(_p == mod);

	for (int y = 1; y < n; y++) {
		for (int x = 0; x <= y; x++) {
			int i = x * n2 / y;
			if (!p[i]) {
				p[i] = x * n + y;
			}
		}
	}

	int f_cnt = 0;
	for (int i = 0; i <= n2; i++) {
		if (p[i]) {
			f[f_cnt++] = p[i];
		}
		p[i] = f_cnt;
	}

	inv_[1] = 1;
	for (int i = 2; i <= mod_n; i++)
		inv_[i] = mod - (long long) (mod / i) * inv_[mod % i] % mod;
}

int inv(int a) {
	int i = p[(long long) a * n2 / mod];
	int x = f[i] / n;
	int y = f[i] % n;
	int u = a * y - mod * x;

	if (abs(u) > mod_n) {
		i--;
		x = f[i] / n;
		y = f[i] % n;
		u = a * y - mod * x;
	}
	
	return (long long) y * (u < 0 ? mod - inv_[-u] : inv_[u]) % mod;
}

Details


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 16ms
memory: 14316kb

Test #2:

score: 20
Accepted
time: 485ms
memory: 13796kb

Test #3:

score: 30
Accepted
time: 2274ms
memory: 13040kb

Test #4:

score: 20
Accepted
time: 3880ms
memory: 13560kb

Test #5:

score: 20
Accepted
time: 4719ms
memory: 14752kb