您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: