HDU 1576 A/B(扩展欧几里得算法)
2015-11-04 21:41
337 查看
题目地址:点击打开链接
思路:1.(A/B)%9973=K
2.A/B=K+9973X(X为任意数)
3.A=KB+9973XB
4.A%9973=(KB+9973XB)%9973
5.N=KB%9973
6.KB=9973Y+N(Y为任意数)
7.K/N*B-9973/N*Y=GCD(B,9973)=1
实际上就是用扩展欧几里得算法求X,具体求模的地方不太了解,参考大神A的
AC代码:
思路:1.(A/B)%9973=K
2.A/B=K+9973X(X为任意数)
3.A=KB+9973XB
4.A%9973=(KB+9973XB)%9973
5.N=KB%9973
6.KB=9973Y+N(Y为任意数)
7.K/N*B-9973/N*Y=GCD(B,9973)=1
实际上就是用扩展欧几里得算法求X,具体求模的地方不太了解,参考大神A的
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; const int m = 9973; int exgcd(int m,int n,int &x,int &y) { int x1,y1,x0,y0; x0=1; y0=0; x1=0; y1=1; x=0; y=1; int r=m%n; int q=(m-r)/n; while(r) { x=x0-q*x1; y=y0-q*y1; x0=x1; y0=y1; x1=x; y1=y; m=n; n=r; r=m%n; q=(m-r)/n; } return n; } int main() { int t; int n,b; int x,y; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&b); exgcd(b,m,x,y); x = (x % m + m) % m; printf("%d\n",(x * n) % m); } return 0; }
相关文章推荐
- 《leetCode》:4Sum
- Android网络编程Demo
- Android网络编程Demo
- [leetcode 119] Pascal's Triangle II
- 计算几何与图形学有关的几种常用算法(二)
- 小白学习做项目(2)_使用EntityFramework实现Code First设计数据库(2)
- BroadCast详解
- HTML中动态生成内容的事件绑定问题
- 深入理解Java的接口和抽象类
- OC初期-07NSDate,类目,延展,协议
- 父类指针指向子类对象的理解
- css选择器权值
- VS2008中opengl配置
- mongodb 性能监控
- Linux下Socket 多客户端通信
- 10月联赛集训考试总结
- Parallel dev: .NET SynchronizationContext 二三事
- zoj 1649 Rescue
- <HTML5秘籍>——第五章(音频与视频)
- iOS第三方框架---SDWebImage详解