P2613-逆元

P2613

题解

分数取余非常好办,求出 b b b的逆元即可,但是 a , b a,b a,b都比较大,于是考虑先取模,边读入边取模,最后 e x g c d exgcd exgcd求逆元即可

代码

#include
#define int long long
using namespace std;
const int mod=19260817;
int read(){
	int f=1,re=0;char ch;
	for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
	if(ch=='-'){f=-1,ch=getchar();}
	for(;isdigit(ch);ch=getchar()) re=(re<<3)+(re<<1)+ch-'0',re%=mod;
	return re*f; 
}
int exgcd(int a,int b,int &x,int &y){
	if(b==0){
		x=1,y=0;
		return a;
	}int d=exgcd(b,a%b,x,y);
	int tmp=x;
	x=y,y=tmp-(a/b)*y;
	return d;
}
signed main(){
	int a=read(),b=read(),x,y;
	if(b==0){
		printf("Angry!");
		return 0;
	}exgcd(b,mod,x,y);
	x=(x%mod+mod)%mod;
	printf("%lld\n",x*a%mod);
	return 0;
} 

你可能感兴趣的:(数论)