hdu 1576 求逆元

题意:给出n=A mod 9973和B,求(A/B) mod 9973

昨天用扩展欧几里得做过这题,其实用逆元也可以做。

逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元。

求逆元方法也很简单,用扩展欧几里得解这个方程即可。

逆元性质:若a是b的逆元,则(x/a)mod p=(x*b)mod p

对于本题呢?设B的逆元为x,

那么有(A/B) mod 9973=((A mod 9973)*(x mod 9973))mod 9973

Reference:  http://blog.csdn.net/leonharetd/article/details/13095191

 1 #include <iostream>
 2 using namespace std;
 3 __int64 a,b,b1,x,k,tm,r,T,n,ans;
 4
 5 __int64 extend_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
 6 {
 7     if (b==0)
 8     {
 9         x=1;
10         y=0;
11         return a;
12     }
13     else
14     {
15         __int64 r=extend_gcd(b,a%b,y,x);
16         y=y-x*(a/b);
17         return r;
18     }
19 }
20
21 int gcd(int a,int b)
22 {
23     if (b==0) return a;
24     return gcd(b,a%b);
25 }
26
27 int main()
28 {
29     cin>>T;
30     while (T--)
31     {
32         cin>>n>>b;
33         //bx==1 (mod m)
34         tm=extend_gcd(b,9973,x,k);
35         b1=x*(1/tm);
36         r=9973/tm;
37         b1=(b1%r+r)%r;    //求出最小非负整数解
38         ans=(n*(b1%9973))%9973;
39         cout<<ans<<endl;
40
41     }
42     return 0;
43 }
时间: 2024-08-25 19:55:58

hdu 1576 求逆元的相关文章

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 (总结乘法逆元的几种求法)

题目链接: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

HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 754    Accepted Submission(s): 279 Problem Description When ?? was born, seven crows flew

hdu 3524 Perfect Squares 推公式求逆元

Perfect Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 501    Accepted Submission(s): 272 Problem Description A number x is called a perfect square if there exists an integer b satisfy

HDU 5407 CRB and Candies(LCM +最大素因子求逆元)

[题目链接]click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的,说实话.本人数学归纳大法没有推出来,幸得一个大神给定愿文具体证明,点击这里:click here~~ 代码: #include <bits/stdc++.h> using namespace std; const int N=1e6+10; const int MOD=1e9+7; typedef long long LL; LL p[N

HDU 3240 Counting Binary Trees(组合数学-斯特林数,数论-整数快速幂,数论-求逆元)

Counting Binary Trees Problem Description There are 5 distinct binary trees of 3 nodes: Let T(n) be the number of distinct non-empty binary trees of no more than n nodes, your task is to calculate T(n) mod m. Input The input contains at most 10 test

hdu_1576A/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): 4020    Accepted Submission(s): 3091 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%99

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 扩展欧几里德算法

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 数据的第一行是一个