結果

問題 No.3133 法B逆元
ユーザー kakel-san
提出日時 2025-05-02 21:50:08
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,209 bytes
コンパイル時間 7,346 ms
コンパイル使用メモリ 168,440 KB
実行使用メモリ 187,168 KB
最終ジャッジ日時 2025-05-02 21:50:22
合計ジャッジ時間 9,601 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (92 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static ulong[] NList => ReadLine().Split().Select(ulong.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        if (c[1] == 1) WriteLine(0);
        else if (c[0] == 0) WriteLine("NaN");
        else
        {
            var (g, x, _) = XGcd(c[0], c[1]);
            if (g != 1) WriteLine("NaN");
            else WriteLine((x + c[1]) % c[1]);
        }
    }
    // 拡張ユークリッド互除法 ax + by = gcd(a, b) を満たす x, y を求める
    public static (ulong g, ulong x, ulong y) XGcd(ulong a, ulong b)
    {
        ulong x0 = 1, y0 = 0, x1 = 0, y1 = 1;
        while (b != 0)
        {
            var q = a / b;
            var prevA = a;
            a = b;
            b = prevA % b;
            var prevX0 = x0;
            var prevY0 = y0;
            x0 = x1;
            x1 = prevX0 - q * x1;
            y0 = y1;
            y1 = prevY0 - q * y1;
        }
        return (a, x0, y0);
    }
}
0