SGU 119 Magic Pairs(一元线性同余方程)
来源:互联网 发布:爆笑网络剧排行榜前十 编辑:程序博客网 时间:2024/05/11 12:43
Description
找所有满足条件的(A,B)使得,对任意X,Y,如果A0*X+B0*Y能整除N则A*X+B*Y就能整除N
Input
三个整数N,A0,B0
Output
第一行输出满足条件的(A,B)对数,之后按字典序输出这些(A,B)
Sample Input
3
1 2
Sample Output
3
0 0
1 2
2 1
Solution
Code
#include<cstdio>#include<iostream>#include<vector>using namespace std;typedef pair<int,int>P;vector<P>ans;int A0,B0,N;int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b);} int extend_gcd(int a,int b,int &x,int &y){ int d=a; if(b!=0) { d=extend_gcd(b,a%b,y,x); y-=(a/b)*x; } else { x=1; y=0; } return d;}void linear(int A,int a,int b,int c)//求解一元线性同余方程ax=b(mod c){ int x,y; int g=extend_gcd(a,c,x,y); if(b%g)return ; x=x*(b/g); int mod=c/g; x=(x%mod+mod)%mod; for(int i=0;i<g;i++) { if(x<N)ans.push_back(make_pair(A,x)); x+=mod; }}int main(){ while(~scanf("%d%d%d",&N,&A0,&B0)) { ans.clear(); int g=gcd((gcd(A0,B0)),N); A0/=g,B0/=g,N/=g; for(int i=0;i<N;i++) linear(i,A0,i*B0%N,N); printf("%d\n",ans.size()); for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].first*g,ans[i].second*g); } return 0;}
0 0
- SGU 119 Magic Pairs(一元线性同余方程)
- SGU 119. Magic Pairs 同余+枚举
- 一元线性同余方程&&一元线性同余方程组
- 一元线性同余方程&&一元线性m同余方程组(模板)
- SGU 261 Discrete Roots(原根+高次同余方程+线性同余方程)
- POJ 2115 C Looooops(一元线性同余方程)
- POJ 2657 Comfort(一元线性同余方程)
- HDU 5114 Collision(一元线性同余方程)
- HDU 5114 Collision(一元线性同余方程)
- GYM 100883 H.tourists(一元线性同余方程)
- CodeForces 7 C.Line(一元线性同余方程)
- HDU3579 一元线性同余方程
- 一元线性同余方程的概述
- 一元线性同余方程简介
- hdu5114 Collision(一元同余方程)
- SGU 140 Integer Sequences (多元线性同余方程)
- Poj2142解不定方程——一元线性同余
- POJ 2115-C Looooops(基础一元线性同余方程)
- 正则表达式的先行断言(lookahead)和后行断言(lookbehind)
- 不解,排名靠前那么多的人为什么抄袭我的activit博文??
- 类的继承
- 运算
- linux如何使用umount命令强制卸载文件系统
- SGU 119 Magic Pairs(一元线性同余方程)
- MySQL学习笔记---基础概念和一些基础SQL语句(一)
- 判断单链表是否带环?若带环,求环的长度?求环的入口点?(C语言)
- 码农小汪-设计模式之-观察者模式 依赖倒置原则
- Leetcode-关于二叉树的对称性
- epoll介绍
- R语言求解线性方程组
- Libgdx之Animation 动画
- Python学习