QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#526522#5. 在线 O(1) 逆元December456100 ✓4417ms19504kbC++14617b2024-08-21 17:01:322024-11-05 22:03:11

Judging History

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

  • [2024-11-05 22:03:11]
  • 管理员手动重测本题所有提交记录
  • 测评结果:100
  • 用时:4417ms
  • 内存:19504kb
  • [2024-08-21 17:01:32]
  • 评测
  • 测评结果:100
  • 用时:5931ms
  • 内存:19508kb
  • [2024-08-21 17:01:32]
  • 提交

answer

#include "inv.h"

constexpr int INIT_BOUND = 4000000;
constexpr int P = 998244353;

int _inv[INIT_BOUND];

void init(int p) {
    _inv[1] = 1;
    for (int i = 2; i < INIT_BOUND; i ++) {
        _inv[i] = (long long){p - p / i} * _inv[p % i] % p;
    }
}

int inv(int x) {
    int ret = 1;

    while (x >= INIT_BOUND) {
        int y = P % x;
        if (y << 1 <= x) {
            ret = (long long){P - P / x} * ret % P;
            x = y;
        } else {
            ret = (P / x + 1ll) * ret % P;
            x = x - y;
        }
    }

    return (long long){ret} * _inv[x] % P;
}

Details


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 24ms
memory: 19356kb

Test #2:

score: 20
Accepted
time: 455ms
memory: 19320kb

Test #3:

score: 30
Accepted
time: 2155ms
memory: 19456kb

Test #4:

score: 20
Accepted
time: 3442ms
memory: 19308kb

Test #5:

score: 20
Accepted
time: 4417ms
memory: 19504kb