求逆元两种方法 hdu-1576-A/B
2013-06-11 18:14
274 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1576
题目意思:
求出A/B mod 9973的值,n=A mod 9973 ,gcd(B,9973)=1.
因为gcd(B,9973)=1.
可以用费马小定理和扩展欧几里得算法两种方法来求B的逆元,然后化除法为乘法。
代码:
http://acm.hdu.edu.cn/showproblem.php?pid=1576
题目意思:
求出A/B mod 9973的值,n=A mod 9973 ,gcd(B,9973)=1.
因为gcd(B,9973)=1.
可以用费马小定理和扩展欧几里得算法两种方法来求B的逆元,然后化除法为乘法。
代码:
#include<iostream> #include<cmath> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<stack> #include<list> #include<queue> #define eps 1e-6 #define INF (1<<30) #define PI acos(-1.0) using namespace std; #define ll __int64 ll quick(ll a,ll b,ll m) //用费马小定理解 { ll res=1; while(b) { if(b&1) res=(res*a)%m; a=(a*a)%m; b=b>>1; } return res; } void egcd(int a,int b,int &x,int &y) //用扩展的欧几里得算法解 { if(b==0) { x=1;y=0; return ; } egcd(b,a%b,x,y); int temp=x; x=y,y=temp-a/b*y; return ; } int main() { int t; int n,b; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&b); int x,y; /* egcd(b,9973,x,y); x=x%9973; if(x<0) x+=9973; printf("%d\n",(n*x)%9973);*/ printf("%d\n",(n*quick(b,9973-2,9973))%9973); } return 0; }
相关文章推荐
- hdu1102 prime()和kruskal()两种方法,感觉不错。 如果你要问我:我只能说,实力决定一切
- hdu 2072 1106学一波字符串分割,C语言与C++两种方法
- Hdu 1328 IBM Minus One 字符串处理的两种方法
- hdu 4585 Shaolin两种方法(暴力和STL map set)
- HDU 2089 数位dp/字符串处理 两种方法
- hdu 1026 Ignatius and the Princess I (bfs记录路径 两种记录路径方法模拟优先队列和优先队列两种做法)
- HDU 5596(更新,两种方法)
- hdu 1532 Drainage Ditches (网络流两种方法)
- HDU 1018 big number(两个公式,两种方法)
- HDU 4123(两种方法-RMQ,单调队列)
- HDU 1012 u Calculate e(两种方法)
- HDU 1250Hat's Fibonacci(两种方法处理大数)
- HDU-1251-统计难题(未完待续 还有两种方法还没整理)
- hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms)
- HDU 5418 Victor and World(状态压缩路径DP或+floyd 两种方法 求 玈行商问题)经典
- HDU 1198 Farm Irrigation 两种方法(dfs,并查集)
- hdu 1062Text Reverse 反转字符串【两种方法 数组法、堆栈】
- HDU 1013 Digital Roots(两种方法,求数字根)
- hdu 4533 威威猫系列故事——晒被子(两种方法)
- HDU 4565 So Easy! 构造矩阵(两种方法)