有理数取余
【模板】有理数取余
题目描述
给出一个有理数 \(c=\frac{a}{b}\),求 \(c \bmod 19260817\) 的值。
这个值被定义为 \(bx\equiv a\pmod{19260817}\) 的解。
有理数取余
\[\frac{a}{b} \mod p = a \times b^{-1}
\]
费马小定理:若\(p\)为素数,\(\gcd(a,b) = 1\),则有\(a^{-1} = 1(\mod p)\)
\(ax=a^{p-1}(\mod p)\)
\(x = a^{p-2}(\mod p)\)
快读,边读入边模。然后利用快速幂求\(b^{-1}\),在将\(b^{-1} \times a\)输出即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p = 19260817;
inline int read(){
int res = 0;
char ch = getchar();
while(ch < '0' || ch > '9')
ch = getchar();
while('0' <= ch && ch <= '9'){
res = (res<<3)+(res<<1) + (ch^48);
res %= p;
ch = getchar();
}
return res;
}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res=(ll)res * a % p;
a = (ll)a * a % p;
b >>= 1;
}
return res;
}
int main()
{
int a,b;
a = read();
b = read();
b = power(b,p-2);
cout << (ll)a * b % p << endl;
}
“风雪越是呼啸,雪莲越是绽放”

浙公网安备 33010602011771号