题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576
求解思路:从A一定是B的倍数入手,令A=k*9973+C,那么k*9973+C=tB,所以tB-k*9973=C,用扩展欧几里得求出最小的正t即是A/B,那么再去一次模就求得了解
AC代码:
//HDU-1576 A/B //AC 2016-4-19 19:54:46 //extent Euclid #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cctype> #include <cstdlib> #include <cstring> #include <vector> #include <set> #include <string> #include <map> #include <queue> #include <deque> #include <list> #include <sstream> #include <stack> using namespace std; #define cls(x) memset(x,0,sizeof x) #define inf(x) memset(x,0x3f,sizeof x) #define neg(x) memset(x,-1,sizeof x) #define ninf(x) memset(x,0xc0,sizeof x) #define st0(x) memset(x,false,sizeof x) #define st1(x) memset(x,true,sizeof x) #define INF 0x3f3f3f3f #define lowbit(x) x&(-x) #define abs(x) (x>0?x:-x) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define bug cout<<"here"<<endl; //#define debug long long X,Y; long long extgcd(int a,int b)//return gcd, X&Y are global { if(b==0) { X=1; Y=0; return a; } long long ans=extgcd(b,a%b); long long temp=Y; Y=X-(a/b)*Y; X=temp; return ans; } int main() { #ifdef debug freopen("E:\Documents\code\input.txt","r",stdin); freopen("E:\Documents\code\output.txt","w",stdout); #endif long long A,B; int T; scanf("%d",&T); while(T--) { scanf("%lld %lld",&A,&B); long long gcd=extgcd(B,-9973); long long t=A/gcd; long long s=abs(9973/gcd); long long res=((X%s*t%s)%s+s)%s; printf("%lld ",res); } return 0; }