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, ...
随机推荐
- CF1511G Chips on a Board (倍增)
题面 原题题面 转化方便版题意: 有 n n n 堆石子,第 i i i 堆有 c i ∈ [ 1 , m ] c_i\in [1,m] ci∈[1,m] 个石子,有 q q q 次询问,每次询问给 ...
- LOJ2312 LUOGU-P3733「HAOI2017」八纵八横 (异或线性基、生成树、线段树分治)
八纵八横 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路的两端都是城市(可能两端是同一个 ...
- Java中字节流的总结及代码练习
Java中的字节流 在描述字节流时,先知道什么是流 流可以分为:输入流和输出流 输入流和输出流 示意图: 字节流读取内容:二进制,音频,视频 优缺点:可以保证视频音频无损,效率低,没有缓冲区 字节流可 ...
- 【java】学习路线5-public和private、构造方法、this关键字、封装对象、static关键字、main方法结构解析
//一个教务管理系统//知识点清单/*public & private 的区别一个是公开的,一个是私有的,作用域不一样,访问的权限不一样咯如果是用private修饰,则调用者只可以是在当前的作 ...
- 简单创建一个SpringCloud2021.0.3项目(二)
目录 1. 项目说明 1. 版本 2. 用到组件 3. 功能 2. 上一篇教程 3. 创建公共模块Common 4. 网关Gateway 1. 创建Security 2. Security登陆配置 3 ...
- x64dbg 插件开发环境配置
x64dbg 是一款开源的应用层反汇编调试器,旨在对没有源代码的可执行文件进行恶意软件分析和逆向工程,同时 x64dbg 还允许用户开发插件来扩展功能,插件开发环境的配置非常简单,如下将简单介绍x64 ...
- ESP8266 RTOS SDK开发
ESP8266 RTOS SDK开发 目录 ESP8266 RTOS SDK开发 一.源码RTOS SDK包的下载和编译 二.固件烧录 1.管脚定义 三.程序例程 ## 1.PWM设置 连接MQTT ...
- Linux_etc-passwd文件总结
文件内容 ## # User Database # # Note that this file is consulted directly only when the system is runnin ...
- 【读书笔记】C#高级编程 第六章 数组
(一)同一类型和不同类型的多个对象 如果需要使用同一类型的多个对象,就可以使用数组或集合(后面章讲). 如果需要使用不同类型的多个对象,可以使用Tuple(元组)类型. (二)简单数组 如果需要使用同 ...
- [apue] 标准 I/O 库那些事儿
前言 标准 IO 库自 1975 年诞生以来,至今接近 50 年了,令人惊讶的是,这期间只对它做了非常小的修改.除了耳熟能详的 printf/scanf,回过头来对它做个全方位的审视,看看到底优秀在哪 ...