sgu 119 Magic Pairs

本文解决了一个数学竞赛中关于整数对的问题,通过数学原理找到所有满足条件的整数对,并提供了算法实现。适用于数学竞赛准备和算法学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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

Author: Michael R. Mirzayanov
Resource: PhTL #1 Training Contests
Date: Fall 2001


思路:其实这道题是参考大牛博客才搞出来的,果然自己数学还是不行啊。

结论就是  所有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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值