E1. Divisible Numbers (easy version)
E1. Divisible Numbers (easy version)
This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on and .
You are given positive integers with and . Find any pair of numbers and that satisfies the following conditions:
- , ,
- is divisible by .
Note that required and may not exist.
Input
The first line of the input contains a single integer , the number of test cases.
The descriptions of the test cases follow.
The only line of each test case contains four integers and .
Output
For each test case print a pair of numbers and such that is divisible by . If there are multiple answers, print any of them. If there is no such pair of numbers, then print .
Example
input
5 1 1 2 2 3 4 5 7 8 9 15 18 12 21 14 24 36 60 48 66
output
2 2 4 6 12 12 -1 -1 -1 -1
解题思路
容易想到的暴力做法是双重循环枚举和,这样做肯定会超时,因此可以想一下能否只枚举,则根据这个条件直接求出来。
因为被整除,因此必然被整除。令,因此当我们枚举到某个,看一下在区间内是否存在一个的倍数,设这个数为,如果存在则表示就是一组解。
如何判断在内是否存在一个的倍数呢?只要看一下严格大于的最小的的倍数是否不超过就可以了。
- 如果,那么很明显严格大于的最小的的倍数是。
- 如果,那么严格大于的最小的的倍数是。
因此可以统一表示成。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 int gcd(LL a, LL b) { 7 return b ? gcd(b, a % b) : a; 8 } 9 10 void solve() { 11 int a, b, c, d; 12 cin >> a >> b >> c >> d; 13 LL k = 1ll * a * b; 14 for (int i = a + 1; i <= c; i++) { // 枚举x 15 LL t = k / gcd(k, i); // t|y,t是y的因子 16 if ((b / t + 1) * t <= d) { // 大于b的最小的t的倍数不超过d 17 printf("%d %d\n", i, d / t * t); 18 return; 19 } 20 } 21 printf("-1 -1\n"); 22 } 23 24 int main() { 25 int t; 26 scanf("%d", &t); 27 while (t--) { 28 solve(); 29 } 30 31 return 0; 32 }
参考资料
Codeforces Round #828 (Div. 3) Editorial:https://codeforces.com/blog/entry/108101
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16807496.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!