QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#282422 | #5. 在线 O(1) 逆元 | uzu_23 | 60 | 5527ms | 3880kb | C++14 | 600b | 2023-12-11 23:39:27 | 2024-11-05 21:56:06 |
Judging History
answer
// inv.cpp
// inv.h
#define once
void init(int p);
int inv(int x);
#include "inv.h"
namespace {
int mod;
}
void init(int p) {
mod = p;
}
// 扩展欧几里得算法求逆元
int extGCD(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int gcd = extGCD(b, a % b, x, y);
int temp = x;
x = y;
y = temp - (a / b) * y;
return gcd;
}
int inv(int x) {
int invX, y;
extGCD(x, mod, invX, y);
invX = (invX % mod + mod) % mod; // 确保逆元为正数
return invX;
}
詳細信息
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 12ms
memory: 3880kb
Test #2:
score: 20
Accepted
time: 1108ms
memory: 3692kb
Test #3:
score: 30
Accepted
time: 5527ms
memory: 3760kb
Test #4:
score: 0
Time Limit Exceeded
Test #5:
score: 0
Time Limit Exceeded