您的位置:首页 > 其它

HDU 1576 A/B(扩展欧几里得算法)

2015-11-04 21:41 337 查看
题目地址:点击打开链接

思路:1.(A/B)%9973=K

2.A/B=K+9973X(X为任意数)

3.A=KB+9973XB

4.A%9973=(KB+9973XB)%9973

5.N=KB%9973

6.KB=9973Y+N(Y为任意数)

7.K/N*B-9973/N*Y=GCD(B,9973)=1

实际上就是用扩展欧几里得算法求X,具体求模的地方不太了解,参考大神A的

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;
const int m = 9973;

int exgcd(int m,int n,int &x,int &y)
{
int x1,y1,x0,y0;
x0=1; y0=0;
x1=0; y1=1;
x=0; y=1;
int r=m%n;
int q=(m-r)/n;
while(r)
{
x=x0-q*x1; y=y0-q*y1;
x0=x1; y0=y1;
x1=x; y1=y;
m=n; n=r; r=m%n;
q=(m-r)/n;
}
return n;
}

int main()
{
int t;
int n,b;
int x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&b);
exgcd(b,m,x,y);
x = (x % m + m) % m;
printf("%d\n",(x * n) % m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: