题目传送门
这模板就挺离谱的。
求 bamod19260817,a,b≤1010001。
下记 p=19260817。
p 是质数,所以不难发现这玩意就是一个费马小定理求逆元的板子题。瓶颈在于 a,b 过大。
由于 abmodp=(amodp)bmodp,那么 bamodp=a×bp−2modp=(amodp)×(bmodp)p−2modp。
然后写快读的时候边输入边取模,用快速幂求一下 bp−2 就完事了。
时间复杂度 O(logp+∣a∣),其中 ∣a∣ 为 a 的数字位数。
(不判无解也能 AC)
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include<bits/stdc++.h> #define ll long long #define ri register int #define back return #define ull unsigned ll using namespace std; const int mod=19260817; ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x*10+ch-'0')%mod; ch=getchar(); } back x*f%mod; } ll ksm(ll a,ll b,ll p) { ll ans=1,base=a; if(p==1) back 0; while(b>0) { if(b&1) ans=ans*base%p; base=base*base%p; b/=2; } back ans%p; } ll a,b; int main() { a=read(),b=read(); cout<<a*ksm(b,mod-2,mod)%mod<<"\n"; back 0; }
|