首页 > 其他分享 >Codeforces Round #828 (Div. 3) E2 // 数论 + dfs

Codeforces Round #828 (Div. 3) E2 // 数论 + dfs

时间:2022-10-18 00:55:38浏览次数:69  
标签:le res LL Codeforces dfs large ab 828

题目来源:Codeforces Round #828 (Div. 3) E2 - Divisible Numbers

题目链接:Problem - E2 - Codeforces


题意

有 \(t\) 组案例(\(1\ \le t \le 10\)),对于每个案例:给定四个整数\(a,b,c,d\)(\(1 \le a < c \le 10^9,1 \le b < d \le 10^9\)),求一组整数\(x,y\),满足\(a < x \le c\),\(b < y \le d\),\(ab\ |\ xy\),若不存在,则输出\(-1\ -1\)。

思路:数论 + dfs

由\(ab\ |\ xy\)可知,存在整数\(res_1,res_2\)满足:\(res_1·res_2 = ab, res_1\ |\ x, res_2\ |\ y\),即\(res_1,res_2\)为\(ab\)的因数,\(x\)为\(res_1\)的倍数,\(y\)为\(res_2\)的倍数。

于是有如下做法:

  1. 先对\(ab\)分解质因数
  2. 再dfs枚举\(ab\)的第一个因数\(res_1\),则\(res_2 = \large \frac{ab}{res_1}\)
  3. 由于\(a < x \le c\),\(b < y \le d\),于是先找出大于 \(a\) 的最小的 \(res_1\) 的倍数,以及大于 \(b\) 的最小的 \(res_2\) 的倍数,即令\(x = {\large \lceil \frac{a + 1}{res_1} \rceil} ·res_1 = {\large \lfloor \frac{a + 1 + res_1 - 1}{res_1} \rfloor} ·res_1\) ,\(y = {\large \lceil \frac{b + 1}{res_2} \rceil} ·res_2 = {\large \lfloor \frac{b + 1 + res_2 - 1}{res_2} \rfloor} ·res_2\),若求出的值满足:\(x \le c\),\(y \le d\),则说明成功找出一组解。

记 \(ab\) 的因数个数为 \(\omega\),由于 \([1,10^{18}]\) 范围内的数的因数个数最多为\(103680\),因此有\(\omega \le 103680\)。
时间复杂度:\(O(t·(\sqrt{a}+\sqrt{b}+\omega))\).

代码

#include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define PII pair<int,int>
using namespace std;

int a, b, c, d;
map<int, int> mp;
vector<PII> v;
bool ok;

void getPrimes(int x)
{
    for(int i = 2; i <= x / i; ++ i) {
        while(x % i == 0) ++ mp[i], x /= i;
    }
    if(x > 1) ++ mp[x];
}

void dfs(int u, LL res)
{
    if(ok) return;
    if(u == v.size()) {
        LL res2 = (LL)a * b / res;
        LL x = (a + 1 + res - 1) / res * res, y = (b + 1 + res2 - 1) / res2 * res2;
        if(x <= c && y <= d) {
            cout << x << " " << y << endl;
            ok = true;
        }
        return;
    }
    LL mul = 1;
    for(int i = 0; i <= v[u].second; ++ i) {
        dfs(u + 1, res * mul);
        mul *= v[u].first;
    }
}

void solve()
{
    cin >> a >> b >> c >> d;

    mp.clear();
    getPrimes(a), getPrimes(b);

    v.clear();
    for(auto p : mp) v.push_back(p);

    ok = false;
    dfs(0, 1);
    if(!ok) cout << -1 << " " << -1 << endl;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int test;
    cin >> test;
    while(test--) solve();

    return 0;
}

标签:le,res,LL,Codeforces,dfs,large,ab,828
From: https://www.cnblogs.com/jakon/p/16801234.html

相关文章

  • Codeforces Round #733 D
    D.SecretSanta答案就是每一个数字是否出现很容易想到的就是我们只能满足一个人的要求(如果这一组人都选择同一个人所以我们直接就这样乱搞就可以了然后剩下的随便连一......
  • Codeforces Global Round 23 -D.Paths on the Tree
    题意给定一个树,树上每个节点i有一个权值s[i]。一共有k条从1(一定是根节点)开始的简单路径。设i点有c条路径通过,则其总权值为c*s.现在在限制:如果p[u]=i,p[v]=i,则abs(c[u]......
  • Codeforces Round #726 E1
    E1.EraseandExtend(EasyVersion)首先我们来证一个东西就是最优解一定由先删若干次然后一直copy而来而不会在中途再删一次因为在中途再删一次就证明这个后缀不如前......
  • 备赛蓝桥----DFS篇
    目录​​排列数字​​​​n-皇后问题​​排列数字importjava.util.Scanner;/***@authoryx*@date2022-01-2914:23*//*排列数字问题*/publicclassDFS_1{stat......
  • Codeforces Round #828 (Div. 3)
    CodeforcesRound#828(Div.3)https://codeforces.com/contest/1744罚时爆炸的a~e1A.NumberReplacement数字一样的对应字母一定要一样#include<bits/stdc++.h>......
  • Codeforces Round #827 (Div. 4) 复盘+题解
    原比赛链接复盘:ABC签到,手速太慢了。D捣鼓了好久才想起来从更小的值域出发去做。E简单二分答案。然后就timeout了。D题搞错方向浪费太久时间了。F思维题,拐两个弯再$r......
  • Codeforces Round #729 (Div. 2) C
    C.StrangeFunction考虑反想我们将x确定看看有多少个i对于f[i]=x我们显然i%lcm(1,2,3,...x-1)!=0这里就可以通过容斥直接求解i%lcm(1,2,3,...x-1)是含有1,2,3,...x-1......
  • HDFS文件上传下载
      一、HDFS文件上传(测试参数优先级) 1、编写源代码 @TestpublicvoidtestCopyFromLocalFile()throwsIOException,InterruptedException,URISyntaxExceptio......
  • Educational Codeforces Round 112 D
    D.SayNotoPalindromes很牛逼我们手动模拟一下可以知道只有3个字母不构成回文串只有可能是这样的abcabc....acbacb.......6种情况所以直接暴力预处理即可#inclu......
  • Codeforces Global Round 16 D
    D2.SeatingArrangements(hardversion)题意我们要先按照a来排序然后再来安排d的位置最开始都能想到的一点就是我们可以每一组内按照逆序排序我们就可以让组内是0贡......