关闭

SGU 119 Magic Pairs

439人阅读 评论(0) 收藏 举报

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

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);


i64 gcd(i64 _a, i64 _b)
{
    if (!_a || !_b)
    {
        return max(_a, _b);
    }
    i64 _t;
    while (_t = _a % _b)
    {
        _a = _b;
        _b = _t;
    }
    return _b;
};

i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
    if (!_b)
    {
        _x = 1;
        _y = 0;
        return _a;
    }
    i64 _d = ext_gcd (_b, _a % _b, _x, _y);
    i64 _t = _x;
    _x = _y;
    _y = _t - _a / _b * _y;
    return _d;
}

i64 invmod (i64 _a, i64 _p)
{
    i64 _ans, _y;
    ext_gcd (_a, _p, _ans, _y);
    _ans < 0 ? _ans += _p : 0;
    return _ans;
}

struct zz
{
    int a;
    int b;
    bool operator < (const zz & cmp ) const
    {
        if(a!=cmp.a)
        {
            return a<cmp.a;
        }
        else
        {
            return b<cmp.b;
        }
    }
}zx;

int n,a,b,c,d;
vector<zz>v;

int main()
{
    while(cin>>n)
    {
        cin>>a>>b;
        c = gcd(a,b);
        d = gcd(c,n);
        a/=d;
        b/=d;
        n/=d;
        v.clear();
        for(int i=0;i<n;i++)
        {
            zx.a = a*i;
            zx.b = b*i;
            zx.a %= n;
            zx.b %= n;
            v.pb(zx);
        }
        sort(v.begin(),v.end());
        cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<v[i].a*d<<" "<<v[i].b*d<<endl;
        }
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:292267次
    • 积分:6432
    • 等级:
    • 排名:第4018名
    • 原创:300篇
    • 转载:19篇
    • 译文:0篇
    • 评论:126条
    最新评论