結果

問題 No.3133 法B逆元
ユーザー daiota
提出日時 2025-05-02 23:17:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 748 bytes
コンパイル時間 1,592 ms
コンパイル使用メモリ 161,896 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-05-02 23:17:17
合計ジャッジ時間 2,634 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long L;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i=0;i<ll(n);i++)


ll gcd(ll a,ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}


ll f(ll a,ll b,ll & x,ll & y){
	ll g;
	if(b!=0){
		g=f(b,a%b,y,x);
		y-=(a/b)*x;
	}else{
                g=a;
		x=1; y=0;
	}
	  return g;
}


ll mod_inverse(ll a,ll m){
    ll x,y;
    f(a,m,x,y);
    return (x%m+m)%m;
}


int main(void){
	cin.tie(nullptr);  ios_base::sync_with_stdio(false);
	ll i,j;





	L N,B;
	cin >> N >> B;
	L A=N%B;

	A=(ll)A;
	B=(ll)B;

	if(gcd(N,B)!=1){
		cout << "NaN" << endl;
		return 0;
	}



	ll x,y;
	ll g=f(A,B,x,y);




	cout << mod_inverse(A,B) << endl;







	return 0;

}



0