HDU——T 1576 A/B

http://acm.hdu.edu.cn/showproblem.php?pid=1576

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6418    Accepted Submission(s): 5075

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

Author

xhd

Source

HDU 2007-1 Programming Contest

膜的性质+逆元

(a/b)mod p=?
定义 c为b在mod p意义下的逆元
=a*c mod p = (a mod p * c mod p)mod p

① exgcd求逆元

 1 #include <algorithm>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 const int mod(9973);
 7 int T,n,b,x,y;
 8
 9 int exgcd(int a,int b,int &x,int &y)
10 {
11     if(!b)
12     {
13         x=1; y=0;
14         return a;
15     }
16     int ret=exgcd(b,a%b,x,y);
17     int tmp=x; x=y;
18     y=tmp-a/b*y;
19     return ret;
20 }
21
22 int main()
23 {
24     scanf("%d",&T);
25     for(;T--;)
26     {
27         scanf("%d%d",&n,&b);
28         int gcd=exgcd(b,mod,x,y);
29         x*=gcd;
30         x=(x%mod+mod)%mod;
31         printf("%d\n",(x*n)%mod);
32     }
33     return 0;
34 }

② 欧拉定理求逆元

 1 #include <algorithm>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 #define LL long long
 7 const int mod(9973);
 8 int T,n,b;
 9
10 LL phi(LL x)
11 {
12     LL ret=1;
13     for(LL i=2;i*i<=x;i++)
14         if(x%i==0)
15         {
16             x/=i;
17             ret*=i-1;
18             for(;x%i==0;)
19                 ret*=i,x/=i;
20         }
21     if(x>1) ret*=x-1;
22     return ret;
23 }
24 LL Q_pow(LL a,LL b)
25 {
26     LL ret=1,base=a;
27     for(;b;b>>=1)
28     {
29         if(1&b) ret=(ret*base)%mod;
30         base=(base*base)%mod;
31     }
32     return ret;
33 }
34
35 int main()
36 {
37     scanf("%d",&T);
38     for(;T--;)
39     {
40         scanf("%d%d",&n,&b);
41         LL x=Q_pow((LL)b,(LL)phi((LL)mod)-1);
42         x=(x%mod+mod)%mod;
43         printf("%d\n",(x*n)%mod);
44     }
45     return 0;
46 }
时间: 2024-12-15 16:11:22

HDU——T 1576 A/B的相关文章

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 题意:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). 普通版欧几里得算法(辗转相除): 1 typedef long long LL; 2 LL gcd(LL a,LL b){ 3 return (b==0) ? a : gcd(b,a%b); 4 } 扩展欧几里得算法(理论):对于不完全为0的非负整数,

HDU 1576 (乘法逆元)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思路: 两种思路,一种从乘法逆元角度,另一种从扩展GCD推公式角度. ①乘法逆元: 先来看下逆元和乘法逆元的关系,对于A*X=B,有X=A-1*B,A-1就是普通的逆元了,在这里就是倒数. 如果A*X=B mod n,变成同余式了,那么A-1依然是存在的,只不过不是倒数了,一般把同余之后的逆元称为乘法

HDU 1576 A/B 扩展欧几里德算法

A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2017    Accepted Submission(s): 1469 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个

HDU 1576 A/B(扩展欧几里德变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 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. Sampl

HDU 1576 A/B (扩展欧几里得应用)

题目链接:HDU 1576 A/B 中文题, 思路:设X=(A/B)%9973.A/B=k_1*9973+X.A=B*k_1*9973+x*B.n=A%9973,A=k_2*9973+n.k_2*9973+n=B*k_1*9973+x*B B*X ≡ n mod 9973 就是转化为 求B关于n模9973 的逆元.gcd(B,9973) = 1 得知一定有解. AC代码: #include<stdio.h> #define ll __int64 ll exgcd(ll a,ll b,ll &a

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 60

HDU 1576 A/B(扩展欧几里德变形)

一道扩展欧几里德的变形题目 题中给出 n = A%9973 → n = A - A/9973*9973(若x = A%B 则 x = A - A/B*B) 因为A能整除B 所以设x = A/B → A = B*x 所以原式 = B*x - A/9973*9973 = n 设y = A/9973 B*x - 9973y = n 然后利用扩展欧几里德求出x即可. #include <iostream> #include <cstdio> #include <algorithm&g

hdu 1576 A/B (扩展欧几里德简单运用)

http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3351 Accepted Submission(s): 2545 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必

HDU 1576 -- A/B (总结乘法逆元的几种求法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7264    Accepted Submission(s): 5774 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%99