【HDU 1576】 A/B
生活随笔
收集整理的這篇文章主要介紹了
【HDU 1576】 A/B
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Problem Description 要求(A/B)%9973,但由于A很大,我們只給出n(n=A%9973)(我們給定的A必能被B整除,且gcd(B,9973) = 1)。 Input 數據的第一行是一個T,表示有T組數據。
每組數據有兩個數n(0 <= n < 9973)和B(1 <= B <= 10^9)。 Output 對應每組數據輸出(A/B)%9973。 Sample Input 2 1000 53 87 123456789 Sample Output 7922 6060 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 題解: 很容易想到 就是解線性方程 a*B+b*9973=n 很明顯最小的正數a就是答案 秒上exgcd 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 typedef long long ll; 8 ll exgcd(ll a,ll b,ll &x,ll &y) 9 { 10 if(!b) 11 { 12 x=1;y=0; 13 return a; 14 } 15 ll r=exgcd(b,a%b,x,y); 16 ll t=x; 17 x=y; 18 y=t-a/b*y; 19 return r; 20 } 21 void work(ll a,ll b,ll c) 22 { 23 ll x,y; 24 ll r=exgcd(a,b,x,y); x*=c/r; 25 ll t=b/r; 26 if(t<0)t=-t; 27 x=(x%t+t)%t; 28 printf("%lld\n",x); 29 } 30 int main() 31 { 32 ll a,b;int T; 33 scanf("%d",&T); 34 while(T--) 35 { 36 scanf("%lld%lld",&a,&b); 37 work(b,9973,a); 38 } 39 return 0; 40 }
每組數據有兩個數n(0 <= n < 9973)和B(1 <= B <= 10^9)。 Output 對應每組數據輸出(A/B)%9973。 Sample Input 2 1000 53 87 123456789 Sample Output 7922 6060 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 題解: 很容易想到 就是解線性方程 a*B+b*9973=n 很明顯最小的正數a就是答案 秒上exgcd 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 typedef long long ll; 8 ll exgcd(ll a,ll b,ll &x,ll &y) 9 { 10 if(!b) 11 { 12 x=1;y=0; 13 return a; 14 } 15 ll r=exgcd(b,a%b,x,y); 16 ll t=x; 17 x=y; 18 y=t-a/b*y; 19 return r; 20 } 21 void work(ll a,ll b,ll c) 22 { 23 ll x,y; 24 ll r=exgcd(a,b,x,y); x*=c/r; 25 ll t=b/r; 26 if(t<0)t=-t; 27 x=(x%t+t)%t; 28 printf("%lld\n",x); 29 } 30 int main() 31 { 32 ll a,b;int T; 33 scanf("%d",&T); 34 while(T--) 35 { 36 scanf("%lld%lld",&a,&b); 37 work(b,9973,a); 38 } 39 return 0; 40 }
?
轉載于:https://www.cnblogs.com/Yuzao/p/7029280.html
總結
以上是生活随笔為你收集整理的【HDU 1576】 A/B的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玖富过不了小鱼福卡能过吗
- 下一篇: Publish/Subscribe