A/B

来源:互联网 发布:nginx 限制域名访问 编辑:程序博客网 时间:2024/05/21 22:37
A/B
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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

思路:可以用扩展欧几里得算法进行求解。这是在看到的别人的思路,找不到在哪看的了。感觉比较简单,过程如下:

设 (A / B) % 9973 = m,令 A = 9973 * x + n

 则 ((9973 * x + n) / B) = 9973 * y + m

化简得:9973(x - y * B) = m * B - n

所以:(m * B - n) % 9973 = 0;


#include<algorithm>#include<iostream>#include<cstdio>#include<string>using namespace std;int main(){    int t;    cin >> t;    while (t--)    {        long long n, b;        cin >> n >> b;        for (int i = 0; i < 9973; ++i)        {            if((b * i - n) % 9973 == 0)            {                cout << i << endl;                break;            }        }    }    return 0;}


0 0