题目链接


题目大意

 给定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)的更多相关文章

  1. 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

    题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...

  2. 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 ...

  3. 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 题意 ...

  4. Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)

    https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...

  5. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  6. Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum

    E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...

  7. 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 ...

  8. Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )

    上几次的一道cf题. 题目:http://codeforces.com/contest/376/problem/C 性质: (4)a与b的和除以c的余数(a.b两数除以c在没有余数的情况下除外),等于 ...

  9. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

随机推荐

  1. 微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

    写在前面 在日常开发中,我这边比较熟悉的配置中心有,携程Apollo,阿里Nacos(配置中心,服务治理一体) 之前文章: Asp.Net Core与携程阿波罗(Apollo)的第一次亲密接触 总体来 ...

  2. Dreamweaver8 网站制作软件使用教程

    Dreamweaver是我喜欢做网站的软件.之所以喜欢Dreamweaver 8 是因为这个版本有折叠功能. 下面说说它的使用方法. 1.创建站点文件.注意看截图的顺序,如下图 2.选择你喜欢的编辑模 ...

  3. C#基础_枚举

    一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...

  4. 关于DOS命令窗口的一点基本知识

    1.DOS命令窗口又可称为CMD窗口.shell.终端. 2.常用的DOS命令: -硬盘分区名+: :进入到该硬盘分区(注意是英文冒号哦) -cd+目录名:进入到指定(该)目录 -dir:列出当前目录 ...

  5. Springboot连接数据库

    好家伙, 这里使用的软件是IDEA 2021 1.新建项目 2.更改配置项目文件目录 更改前: 更改后: 2.1.更改pom.xml文件 在该文件中添加: <dependency> < ...

  6. 餐饮行业SRM案例:方正璞华"采云链"携手和府捞面

    ​ 方正璞华在与和府捞面的项目中,帮助整合采购系统,系统化管理供应商,优化采购流程和数据分析,建立储备知识库. 项目背景 企业介绍 ◇ 和府捞面成立于2012年,是国内中式面食头部品牌.自2013年首 ...

  7. Elasticsearch:ICU分词器介绍

    ICU Analysis插件是一组将Lucene ICU模块集成到Elasticsearch中的库. 本质上,ICU的目的是增加对Unicode和全球化的支持,以提供对亚洲语言更好的文本分割分析. 从 ...

  8. logstash中output{}的另类写法

    日志传输路径如下: filebeat->redis->logstash->es 在filebeat配置文件中,收集日志的时候配置的有如下参数: fields: log_source: ...

  9. centos7使用yum方式安装MySQL5.7

    yum -y localinstall http://mirrors.ustc.edu.cn/mysql-repo/mysql57-community-release-el7.rpm yum inst ...

  10. 用prometheus监控Nginx

    GitHub上官方地址:https://github.com/knyar/nginx-lua-prometheus 告警规则地址:https://awesome-prometheus-alerts.g ...