HDU1576 A/B 费马小定理
生活随笔
收集整理的這篇文章主要介紹了
HDU1576 A/B 费马小定理
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
A/B
Time Limit: 1000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1369????Accepted Submission(s): 1045
?
Problem Description
要求(A/B)%9973,但由于A很大,我們只給出n(n=A%9973)(我們給定的A必能被B整除,且gcd(B,9973) = 1)。
?
?
Input
數(shù)據(jù)的第一行是一個(gè)T,表示有T組數(shù)據(jù)。
每組數(shù)據(jù)有兩個(gè)數(shù)n(0 <= n < 9973)和B(1 <= B <= 10^9)。
?
?
Output
對(duì)應(yīng)每組數(shù)據(jù)輸出(A/B)%9973。
?
?
Sample Input
?2 1000 53 87 123456789
?
Sample Output
?7922 6060
思路:由gcd(B,9973)=1可知,B與9973互質(zhì),且為質(zhì)數(shù),因此可以用費(fèi)馬小定理來(lái)求出B對(duì)9973的乘法逆元;
(A/B)%mod==(A*B的逆元)%mod.
代碼:
#include <iostream> using namespace std; typedef long long ll; int mod=9973; ll quickpow(ll a,ll b) {ll res = 1;a = a % mod;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res; } int main() {int t,n,b,ans;cin>>t;while(t--){cin>>n>>b;ans=quickpow(b,mod-2);cout<<ans*n%mod<<endl;}return 0; } ??
??
總結(jié)
以上是生活随笔為你收集整理的HDU1576 A/B 费马小定理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU1862 EXCEL排序
- 下一篇: 51nod 1256 乘法逆元(扩展欧几