Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)
题目链接
题目大意
给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件:
1. a<x<c,b<y<d
2. (x*y)%(a*b)==0
题目思路
因为(x*y)%(a*b)==0\(\rightarrow\)x*y\(~\)=\(~\)k*a*b\(~\)=\(~\)k*k1*k2
所以我们要找的就是a,b的因子来保证a%k1\(~\)||\(~\)a%k2\(~\)||\(~\)b%k1\(~\)||\(~\)b%k2为0
而每个数都能被拆成多个质因子的k次的乘积,也就是说:
a\(~\)=\(~\)P\(_{1}\)\(^{k1}\)\(~\)*\(~\)P\(_{2}\)\(^{k2}\)\(~\)*\(~\)P\(_{3}\)\(^{k3}\)\(~\)*....*\(~\)P\(_{n}\)\(^{kn}\)
b\(~\)=\(~\)S\(_{1}\)\(^{k1}\)\(~\)*\(~\)S\(_{2}\)\(^{k2}\)\(~\)*\(~\)S\(_{3}\)\(^{k3}\)\(~\)*....*\(~\)S\(_{n}\)\(^{kn}\)
\(~~~\)而且在质因子较少,便于搜索,所以我们直接拆出a,b的质因子去爆搜看能不能找到对应的x,y
代码详情
# include<iostream>
# include<bits/stdc++.h>
using namespace std;
# define int long long
# define endl "\n"
const int N = 5e5 + 10;
map<int, int> mp;
vector<pair<int, int>> fac;
int a, b, c, d;
bool ok = false;
void fj(int n) //分解质因数
{
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
mp[i] += cnt;//保存质因数的次数k
}
}
if (n > 1) mp[n]++;
}
int ans_x, ans_y;
void dfs(int id, int k1, int k2) {
if (id == fac.size()) {
int x = ((a / k1) + 1) * k1;
int y = ((b / k2) + 1) * k2;
if (x <= c && y <= d) {
ok = true;
ans_x = x;
ans_y = y;
}
return;
}
int p = fac[id].first, cnt = fac[id].second;
int pow = 1;
for (int i = 1; i <= cnt; ++i) pow *= p;
int res = 1;
for (int i = 0; i <= cnt; ++i) {
dfs(id + 1, k1 * res, k2 * (pow / res));
res *= p;
}
}
void solve() {
mp.clear(), fac.clear();
ok = false;
cin >> a >> b >> c >> d;
fj(a);
fj(b);
for (auto p : mp) fac.push_back(p);
dfs(0, 1, 1);
if (ok) cout << ans_x << " " << ans_y << endl;
else cout << -1 << " " << -1 << endl;
}
int tt;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
tt = 1;
cin >> tt;
while (tt--)solve();
return 0;
}
Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)的更多相关文章
- 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers
题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)
参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...
- Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)
https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- Codeforces Round #604 (Div. 2) B. Beautiful Numbers
链接: https://codeforces.com/contest/1265/problem/B 题意: You are given a permutation p=[p1,p2,-,pn] of ...
- Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )
上几次的一道cf题. 题目:http://codeforces.com/contest/376/problem/C 性质: (4)a与b的和除以c的余数(a.b两数除以c在没有余数的情况下除外),等于 ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
随机推荐
- 关于Redis在windows上运行及fork函数问题
Redis在将数据库进行持久化操作时,需要fork一个进程,但是windows并不支持fork,导致在持久化操作期间,Redis必须阻塞所有的客户端直至持久化操作完成.微软的一些工程师花费时间在解决在 ...
- C#基础_C#计算样本标准差和总体标准差
首先我们先了解样本标准差和总体标准差: 样本标准差=方差的算术平方根=s=sqrt(((x1-x)^2 +(x2-x)^2 +......(xn-x)^2)/(n-1)) 总体标准差=σ=sqrt(( ...
- C++一些新的特性的理解(二)
1 C++11多线程thread 重点: join和detach的使用场景 thread构造函数参数 绑定c函数 绑定类函数 线程封装基础类 互斥锁mutex condition notify.wai ...
- 并发与并行,同步和异步,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang并发编程之GoroutineEP13
如果说Go lang是静态语言中的皇冠,那么,Goroutine就是并发编程方式中的钻石.Goroutine是Go语言设计体系中最核心的精华,它非常轻量,一个 Goroutine 只占几 KB,并且这 ...
- RHEL7从安装的字符界面转为桌面GUI图形化步骤
1.光驱装载原版光盘镜像 2.挂载光盘镜像 mount /dev/sr0 /media 3.配置本地yum源 vi /etc/yum.repos.d/local.repo [rhel] name=rh ...
- vue中处理过内存泄露处理方法
1>意外的全局变量函数中意外的定义了全局变量,每次执行该函数都会生成该变量,且不会随着函数执行结束而释放. 2>未清除的定时器定时器没有清除,它内部引用的变量,不会被释放. 3>脱离 ...
- 原生JavaScript对【DOM元素】的操作——增、删、改、查
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Batch Norm 与 Layer Norm 比较
一.结论 Batch Norm一般用于CV领域,而Layer Norm一般用于NLP领域 Batch Norm需要计算全局平均,而Layer Norm不需要计算全局平均 二.Batch Norm Ba ...
- LFS(Linux From Scratch)构建过程全记录(六):交叉编译临时工具
写在前面 本章将展示如何使用刚刚构建的跨工具链来交叉编译基本实用程序. M4安装 和前文一样,先进行解压,然后cd进入 注意:不需要构建build文件夹,直接输入以下配置文件 ./configure ...
- 03-MyBatisPlus的CRUD 接口
一.insert 1.插入操作 @RunWith(SpringRunner.class) @SpringBootTest public class CRUDTests { @Autowired pri ...