119. Magic Pairs

time limit per test: 0.5 sec. 
memory limit per test: 4096 KB

“Prove that for any integer X and Y if 5X+4Y is divided by 23 than 3X+7Y is divided by 23 too.” The task is from city Olympiad in mathematics in Saratov, Russia for schoolchildren of 8-th form. 2001-2002 year. 

For given N and pair (A0, B0) find all pairs (A, B) such that for any integer X and Y if A0X+B0Y is divided by N then AX+BY is divided by N too (0<=A,B<N).

Input

Each input consists of positive integer numbers NA0 and B0 (N,A0,B0£ 10000) separated by whitespaces.

Output

Write number of pairs (A, B) to the first line of output. Write each pair on a single line in order of non-descreasing A (and B in case of equal A). Separate numbers by single space.

Sample Input

3
1 2

Sample Output

3
0 0
1 2
2 1
//started 22:24
//read error 1 23:15
//read answer 0:41 晕,既然a0x+b0y|n,ax+by|k*n,直接乘上k再modn即可....
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int extgcd(int a,int b,int &x,int &y){
int d=a;
if(b!=0){
d=extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else {
x=1;y=0;
}
return d;
}
typedef pair<int ,int> P;
P heap[10001];
int cnt;
int main(){
int n,a0,b0;
scanf("%d%d%d",&n,&a0,&b0);
int ty,tx,tmp1,tmp2;
int abgcd=extgcd(a0,b0,tx,ty);
int nabgcd=extgcd(abgcd,n,tmp1,tmp2);
n=n/nabgcd;
a0=a0/nabgcd;
b0=b0/nabgcd;
for(int i=0;i<n;i++){
int a=a0*i%n;
int b=b0*i%n;
heap[i]=P(a,b);
}
sort(heap,heap+n);
printf("%d\n",n);//必然有n个解
for(int i=0;i<n;i++){
printf("%d %d\n",heap[i].first*nabgcd,heap[i].second*nabgcd);
}
return 0;
}

  

快速切题 sgu119. Magic Pairs的更多相关文章

  1. 数论 - 119. Magic Pairs

    Magic Pairs Problem's Link Mean: 已知N.A0.B0,对于给定X.Y,若A0X+B0Y能被N整除,则AX+BY也能被N整除,求所有的A.B.(0<=A.B< ...

  2. Magic Pairs - SGU 119(同余)

    题目大意:如果A0*X + B0*Y能够整除 N,求出来多有少A*X+B*Y 也能够整除去N,求出所有的A,B(0<=A,B<N) 分析:有条件可以知道 A*X+B*Y = K *(A0* ...

  3. SGU 119.Magic pairs

    题意: 对于给出的一个整数N,和一对(A0,B0) 找到所有的整数对(A,B)满足 : 对于任意 X,Y 当 A0 * X + B0 * Y 能被 N 整除时 A * X + B * Y 也能被 N ...

  4. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...

  5. 快速切题sgu126. Boxes

    126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...

  6. 快速切题 sgu123. The sum

    123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...

  7. 快速切题 sgu120. Archipelago 计算几何

    120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...

  8. 快速切题 sgu118. Digital Root 秦九韶公式

    118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...

  9. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

随机推荐

  1. codevs 1423 骑士 - Tarjan - 动态规划

    题目描述 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵 ...

  2. BZOJ5188: [Usaco2018 Jan]MooTube 并查集+离线处理

    BZOJ又不给题面... Luogu的翻译看不下去... 题意简述 有一个$n$个节点的树,边有权值,定义两个节点之间的距离为两点之间的路径上的最小边权 给你$Q$个询问,问你与点$v$的距离超过$k ...

  3. hbase读写流程分析

    前言 最近被大佬问到一个问题,hbase查询数据在最坏的场景下需要进行几次rpc,当时就懵了..下面主要对client端代码进行分析.阅读文章和看源码更配~ 读数据 流程总览 1. 从zookeepe ...

  4. Ubuntu 14.04 安装libssh

    参考: libssh [CMake] include command Ubuntu 14.04 安装libssh $ git clone https://github.com/substack/lib ...

  5. 【TCP/IP详解 卷一:协议】第十章 动态选路协议

    更为详细的RIP博客解析: RIP理论 距离向量算法的简介: RIP协议V-D算法的介绍 10.1 引言 静态选路修改路由表的三种方法 (1)主机设置时,默认的路由表项 (2)ICMP重定向报文(默认 ...

  6. 正则表达式及R字符串处理之终结版

    http://yphuang.github.io/blog/2016/03/15/regular-expression-and-strings-processing-in-R/ 0.动机:为什么学习字 ...

  7. c++ 算法 栅格中两点之间连线

    屏幕划线,通过平面坐标系实现,基本组成是一个一个的点,起点为A,终点为B 本文的算法,可以实现平面栅格中,指定的A,B两点之间进行连线(代码中仅打印了两点间需要画出的坐标点) #include < ...

  8. C#学习 day1 c#基础

    C#是一门编程语言,为什么我今天开始学C#了,下学期有门C#的课,以及有一个经验丰富的老学长正在做C#项目,由于之前学过C++和C基础,所以,C#的基础部分我查看文档来尝试能否自学归纳,而不是一直依靠 ...

  9. Qt5.3.2(VS2010)_调试_遇到的问题

    1.(20171124)调试程序 的时候,一直卡住,看到 右下角的进度里面,一直是"Launching Debugger"的状态 ZC: 一直找不到 原因... 尝试了 32位/6 ...

  10. [源][osg][osgBullet]osgBullet例子介绍

    1.BasicDemo 基本样例 一个小玩具飞机坠落,一个立方体盒子在转圈 2.centerofmass 质心 3.collision 碰撞检测 这里有一个大神自己改的例子: http://blog. ...