結果

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

ソースコード

diff #

#nullable enable

using System.Numerics;

void Run()
{
    var nan = "NaN";
    var n = ulong.Parse(String());
    var b = Int();
    if (b == 1)
    {
        Out(0);
        return;
    }
    var m = (long)(n % (uint)b);
    if (m == 0)
    {
        Out(nan);
        return;
    }
    var q = Extensions.ModDiv(1, m, b);
    if (q.HasValue) Out(q.Value);
    else Out(nan);
}

#region
AtCoderIO _io_;
var _backend_ = new StandardIOBackend();
_io_ = new(){ Backend = _backend_ };
Run();
_backend_.Flush();

string String() => _io_.Next();
int Int() => int.Parse(String());
void Out(object? x, string? sep = null) => _io_.Out(x, sep);

class AtCoderIO
{
    public required StandardIOBackend Backend { get; init; }

    Memory<string> _input = Array.Empty<string>();
    int _iter = 0;
    public string Next()
    {
        while (_iter >= _input.Length) (_input, _iter) = (Backend.ReadLine().Split(' '), 0);
        return _input.Span[_iter++];
    }

    public void Out(object? x, string? separator = null)
    {
        if (x == null) return;
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable a and not string)
        {
            var objects = a.Cast<object>();
            if (separator == Environment.NewLine && !objects.Any()) return;
            x = string.Join(separator, objects);
        }
        Backend.WriteLine(x);
    }
}

class StandardIOBackend
{
    readonly StreamReader _sr = new(Console.OpenStandardInput());
    readonly StreamWriter _sw = new(Console.OpenStandardOutput()) { AutoFlush = false };
    public string ReadLine() => _sr.ReadLine()!;
    public void WriteLine(object? value) => _sw.WriteLine(value);
    public void Flush() => _sw.Flush();
}
#endregion

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();

    public static (long d, long x, long y) ExtendedGCD(long a, long b)
    {
        var (x0, y0, x1, y1) = (1L, 0L, 0L, 1L);
        while (b != 0)
        {
            var q = a / b;
            (a, b) = (b, a - q * b);
            (x0, y0, x1, y1) = (x1, y1, x0 - q * x1, y0 - q * y1);
        }
        return (a, x0, y0);
    }

    public static long? ModDiv(long a, long b, long m)
    {
        if (b == 0) throw new DivideByZeroException();
        var (d, t, _) = ExtendedGCD(b, m);
        if (a % d != 0) return null;
        if (d < 0) (d, t) = (-d, -t);
        var dm = m / d;
        var y = (long)((Int128)a / d * t % dm);
        if (y < 0) y += dm;
        return y;
    }

}
0