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*X + B0*Y)% N ====> (K * A0) % N = A, (K * B0) % N = B, (k=[0....N))。

ps.记得排序去重复......

代码如下:

==========================================================================================================

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN = 10007;
const int oo = 1e9+7;

struct Node
{
    int A, B;
}ans[MAXN];

bool cmp(Node a, Node b)
{
    if(a.A != b.A)
        return a.A < b.A;
    return a.B < b.B;
}

int main()
{
    int i, N, A0, B0, k=1;

    scanf("%d%d%d", &N, &A0, &B0);

    for(i=0; i<N; i++)
    {
        ans[i].A = (A0*i) % N;
        ans[i].B = (B0*i) % N;
    }

    sort(ans, ans+N, cmp);

    for(i=1; i<N; i++)
    {
        if(ans[i].A != ans[i-1].A || ans[i].B != ans[i-1].B)
            ans[k++] = ans[i];
    }

    printf("%d\n", k);

    for(i=0; i<k; i++)
        printf("%d %d\n", ans[i].A, ans[i].B);

    return 0;
}
时间: 2024-07-30 20:26:51

Magic Pairs - SGU 119(同余)的相关文章

SGU 119 Beautiful People

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxx 100050 struct node { int a,b,id; }g[maxx]; int root[maxx]; int id[maxx]; int len; void output(int x) { if(x==-1)return; output(root[x]); printf(&

SGU 119

欧几里得- -要分N种情况- - #include<cstdio> #include<algorithm> using namespace std; int gcd(int x,int y){if (y==0) return x;return gcd(y,x%y);} struct node { int x,y; friend bool operator < (const node &a, const node &b) { if(a.x != b.x) ret

SGU Magic Pairs

A0x + B0y = kn Ax + By = k'n 左差得 (A - A0)x + (B -B0)y = 0(mod n) 所以只要枚举A0, B0的倍数就行了.. 公式就是 ( (i*a)%n, (i*b)%n ), i =0, 1, ... , n-1 i*a, i*b如果大于n的话 不会影响结果, 因为对n取模 那一部分都约去了.. 1 /*Author :usedrose */ 2 /*Created Time :2015/7/24 14:55:16*/ 3 /*File Name

sgu100~199题解

老东西了..发上来吧.. Sgu题解系列  南开中学邹事成 100:A+B略 101:Domino 给n块多米诺骨牌,每张骨牌两端各有从1到6的一个数字,现在要把这些骨牌排成一列,使相邻的两块骨牌相对的面所写的数字一样. 可以把每一块多米诺骨牌想象成一条边,把面上写的数字抽象成点,比如一块骨牌正面写的1反面写的2就想象成连了一条从1到2的边,那么这就是求一条有重边的欧拉回路了,dfs一下即可. 102:Coprimes给定n求从1到n中与n互质的数的个数. 可以把n质因数分解后直接代入欧拉函数.

SGU 160.Magic Multiplying Machine

时间限制:0.5s 空间限制6M 题意:        给出n个(1<=n<=10000)1~m(2<m<1000)范围内的数,选择其中任意个数,使它们的 乘积 模m 最大,输出最大的分数,和选择的数的编号. Solution:               DP,               从第一个数开始,f[]记录当前有哪些数可以得到.如果k可以得到令f[k]=1;               再记录路径,和更新ans.               如果单纯使用二重循环将是N*

高精度特别策划 加减乘除余~~~

P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 A B 输出格式: 五个数 和 差 积 商 余 输入输出样例 输入样例#1: 1 1 输出样例#1: 2 0 1 1 0 说明 length(A),length(B)<=10^4 每个点3s. 代码 1 #include <iostream> 2 #include <cstdlib&

(最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

You're given a matrix A of size n?×?n. Let's call the matrix with nonnegative elements magic if it is symmetric (so aij?=?aji), aii?=?0 and aij?≤?max(aik,?ajk) for all triples i,?j,?k. Note that i,?j,?k do not need to be distinct. Determine if the ma

HDU 2274 Magic WisKey

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2274 Magic WisKey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 568    Accepted Submission(s): 323 Problem Description On New Year Festival, Liu

poj 2888 Magic Bracelet(Polya+矩阵快速幂)

Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 Description Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which