2017年暑假组队训练赛-No.7 K - 简单的数论 CSU - 1871
2017-08-27 18:24
351 查看
擅长数学的小A君不满足于简单的求解最大公约数和最小公倍数的问题了,他研究出了一个新的题型准备去考考小B,小A给小B 4个正整数a,b,c,d,他问小B能否找到一个数x,使得a和x的最大公约数为b,c和x的最小公倍数为d。如果有多个这样的x,将所有的x按由小到大的顺序列出来,如果找不到任何一个这样的x,那么就输出-1。
Input
第一行给定一个正整数T,表示有T组数据 (T<=100) 接下来T行每行输入4个正整数a,b,c,d (这四个数都小于2^31)
Output
对于每组数据按照从小到大的顺序输出所有的x的值,如果一个x都找不到则输出-1。
Sample Input
1 6 3 5 15
Sample Output
3 15
Hint
gcd(3,6)=3 lcm(3,5)=15 gcd(15,6)=3 lcm(15,5)=15
做这题的时候有点蠢,由于第一题的影响一直在推x和a,b,c,d之间的关系,然而实际上直接枚举c的约数就可以了,gcd(x,y)为log(n),枚举约数时间复杂度为O(sqrt(n) *log(n))。以后一定要观察时间复杂度,再决定枚举是否可行,实际上推出来的式子也是超时了毫无优化。
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <set> #include <map> #include <list> #include <queue> #include <stack> #include <string> #include <vector> #include <algorithm> #include <map> using namespace std; #define MOD 100000007 long long gcd(long long x, 4000 long long y){ if(y == 0) return x; else return gcd(y,(x % y)); } set<long long> f; int main() { int T; long long a,b,c,d; scanf("%d",&T); while(T--){ scanf("%lld%lld%lld%lld",&a,&b,&c,&d); if(a % b != 0 || d % c != 0){ printf("-1\n"); continue; } for(int i = 1;i * i <= c;++i){ if(c % i == 0){ long long x = d / c * i; if(gcd(a,x) == b && gcd(c,x) == i){ f.insert(x); } x = d / c * (c / i); if(gcd(a,x) == b && gcd(c,x) == c / i){ f.insert(x); } } } set<long long>::iterator it = f.begin(); if(it == f.end()){ printf("-1\n"); continue; } for(;it != f.end();++it) printf("%lld ",*it); putchar('\n'); f.clear(); } return 0; }
相关文章推荐
- 2017年暑假组队训练赛-No.2 F - 牧场物语 FZU - 2234
- CSU 1871 中南大学网络赛E题 简单的数论
- CSU-ACM暑假集训基础组训练赛(5-1) C - Problem C
- CSU-ACM暑假集训基础组训练赛(5-1) A - Problem A
- CSU-ACM暑假集训基础组训练赛(2) 解题报告
- CSU-ACM暑假集训基础组训练赛(2) D - Problem D
- 哈理工2015暑假训练赛BNU16488 Easy Task(简单题)
- CSU-ACM暑假集训基础组训练赛(5-1) B - Problem B
- CSU-ACM暑假集训基础组训练赛(1) A - Problem A
- CSU-ACM暑假集训基础组训练赛(4)解题报告
- 2017年浙江理工大学程序设计竞赛校赛 题解&源码(A.水, D. 简单贪心 ,E.数论,I 暴力)
- CSU-ACM暑假集训基础组训练赛(2) B - Problem B
- CSU-ACM暑假集训基础组训练赛(1) B - Problem B
- CSU-ACM2014暑假集训基础组训练赛(1) 解题报告
- csu 1416 Practical Number(数论,结论题)
- CSU-ACM2017暑假集训比赛1 A - I Can Guess the Data Structure! -uva111995
- csu 1246 十二指肠钩虫 简单计算
- 2007暑假简单题目二
- CSU-ACM2016暑假集训训练2-DFS
- CSU-ACM暑假集训基础组七夕专场