题目描述:
119. Magic Pairs time limit per test: 0.5 sec. “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.
Input Each input consists of positive integer numbers N, A0 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
| ||||||
|
思路:其实这道题是参考大牛博客才搞出来的,果然自己数学还是不行啊。
结论就是 所有a*i%n,b*i%n 都是解。
知道结论去证明就不难了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
int const nMax = 1000;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
set<pij> ans;
int a,b,n;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int main(){
cin>>n>>a>>b;
ans.clear();
int d=n/gcd(gcd(a,b),n);
for(int i=0;i<d;i++){
ans.insert(pij((a*i)%n,(b*i)%n));
}
set<pij>::iterator it;
printf("%d\n",ans.size());
for(it=ans.begin();it!=ans.end();it++){
printf("%d %d\n",(*it).first,(*it).second);
}
return 0;
}