Hdu1576 A-A/B 扩展欧几里德

来源:互联网 发布:关于程序员的搞笑图片 编辑:程序博客网 时间:2024/05/19 12:16

要求(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

题解:
A=(n+x*9973)

A/B=y-> n+x*9973=B*y-> -x*9973+B*y=n首先计算:x1*9973+B*y1=1则y=y1*ny可能是负数,所以 y = (9973 + y % 9973) % 9973;

代码:

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#include <vector>#define MM(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 1005#define Lchild id<<1#define Rchild (id<<1)+1#define FILE  freopen("data.in","r",stdin)using namespace std;int extend_gcd(int a, int b, int &x, int &y) {    if (b == 0) {        x = 1, y = 0;        return a;    }    int t = extend_gcd(b, a % b, y, x);    y -= a / b * x;    return t;}int main() {    int T, n, b, x, y;    cin >> T;    while (T--) {        cin >> n >> b;        extend_gcd(9973, b, x, y);        y *= n;        y = (9973 + y % 9973) % 9973;        cout << y << endl;    }}
0 0
原创粉丝点击