P2613 有理数取余

题面:https://www.luogu.org/problemnew/show/P2613

分析题目,发现c可以转化为a∗b^−1,即本题就是求b在mod p的意义下的逆元。特别的,当gcd(a,p)≠1,方程无解,因此a也无逆元。

Code:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
#define int LL

#define Mod 19260817
char st[10005];
int read()
{
    scanf("%s",st);
    int len=strlen(st),num=0;
    for (int i=0;i<len;i++)
        num=(num*10+st[i]-'0')%Mod;
    return num;
}
int d,x,y;
void exgcd(int a,int b,int &d,int &x,int &y)
{
    if (b==0)
    {
        d=a;x=1;y=0;
        return;
    }
    exgcd(b,a%b,d,y,x);
    y-=a/b*x;
}
signed main()
{
    int a,b;
    a=read();b=read();
    exgcd(b,Mod,d,x,y);
    if (d!=1)
        printf("Angry!");
    else
    {
        cout<<a*((x%Mod+Mod)%Mod)%Mod;
    }
    return 0;
} 
posted @ 2019-07-16 12:43  prestige  阅读(124)  评论(0编辑  收藏  举报