洛谷——P2613 【模板】有理数取余

P2613 【模板】有理数取余

 

读入优化预处理

$\frac {a}{b}\mod 19620817$ 

也就是$a\times b^{-1}$

$a\times b^{-1}\mod 19620817=a\times b^{19620815}\mod 19620817$

除法转化为了乘法,同余的性质。。。

求一个逆元即可,根据费马小定理,由于$19620817$是一个质数

#include<bits/stdc++.h>

#define LL long long
using namespace std;
const LL mod=19260817;
void in(LL &x){
    char c=getchar();x=0;int f=1;
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0',x%=mod;
    x*=f;
}

LL a,b;

LL pow(LL x,LL y){
    LL s=1;
    for(;y;y>>=1,x=x*x%mod)
        if(y&1) s=s*x%mod;
    return s;
}

int main()
{
    in(a),in(b);
    if(!b) printf("Angry!");
    else printf("%lld\n",a*pow(b,mod-2)%mod);
    
    return 0;
}

 

posted @ 2018-10-18 17:04  清风我已逝  阅读(211)  评论(0编辑  收藏  举报