QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282422#5. 在线 O(1) 逆元uzu_2370 1159ms3556kbC++14600b2023-12-11 23:39:272023-12-11 23:39:27

Judging History

This is a historical verdict posted at 2023-12-11 23:39:27.

  • [2024-11-05 21:56:06]
  • 管理员手动重测本题所有提交记录
  • Verdict: 60
  • Time: 5527ms
  • Memory: 3880kb
  • [2023-12-11 23:39:27]
  • Judged
  • Verdict: 70
  • Time: 1159ms
  • Memory: 3556kb
  • [2023-12-11 23:39:27]
  • Submitted

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;
}


详细

Test #1:

score: 30
Accepted
time: 13ms
memory: 3512kb

Test #2:

score: 40
Accepted
time: 1159ms
memory: 3556kb

Test #3:

score: 0
Time Limit Exceeded