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 :2.cpp*/
 4 #include <cstdio>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <sstream>
 8 #include <cstdlib>
 9 #include <cstring>
10 #include <climits>
11 #include <vector>
12 #include <string>
13 #include <ctime>
14 #include <cmath>
15 #include <deque>
16 #include <queue>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #define INF 0x3f3f3f3f
21 #define eps 1e-8
22 #define pi acos(-1.0)
23 #define MAXN 1110
24 #define OK cout << "ok" << endl;
25 #define o(a) cout << #a << " = " << a << endl
26 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
27 using namespace std;
28 typedef long long LL;
29
30 set<pair<int, int > > s;
31 int main()
32 {
33     //freopen("data.in","r",stdin);
34     //freopen("data.out","w",stdout);
35     cin.tie(0);
36     ios::sync_with_stdio(false);
37     int n, a, b;
38     cin >> n;
39     cin >> a >> b;
40     for (int i = 1;i <= n; ++ i)
41         s.insert(make_pair((a*i)%n, (b*i)%n));
42     cout << s.size() << endl;
43     set<pair<int, int> > ::iterator it = s.begin();
44     while (it != s.end()) {
45         cout << it->first << " " << it->second << endl;
46        it++;
47     }
48
49     return 0;
50 }

大牛的详细分析:

http://d.ream.at/sgu-119/

时间: 2024-10-31 04:19:25

SGU Magic Pairs的相关文章

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.记得排序去重复...... 代码如下: ============================================================

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*

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

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

POJ2888 Magic Bracelet

Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 5476   Accepted: 1775 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 consists of n 

SGU题目总结

SGU还是个不错的题库...但是貌似水题也挺多的..有些题想出解法但是不想写代码, 就写在这里吧...不排除是我想简单想错了, 假如哪位神犇哪天发现请告诉我.. 231.Prime Sum. Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N.1<=N<=10^6看起来挺难的样子..a prime应该是奇数(除了2)

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 223    Accepted Submission(s): 110 Problem Description Harry got a string T, he wanted to know the number of T's disjoint

Leetcode-24 Swap Nodes in Pairs

#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify t