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;
}