CF1444A Division

题目链接:http://codeforces.com/problemset/problem/1444/A
题目概要:给出\(t\)组数据,每组数据包含两个整数\(p_i\)\(q_i\),对于第\(i\)组数据,找出满足以下条件的最大的\(x_i\)\(1.\) \(x_i\mid p_i\) \(2.\) \(q_i \nmid x_i\)

一开始看到这道题时,我的思路是二分\(1\)\(p_i\)的数,判断该数是否满足:为\(p_i\)的因数且\(q_i\)不为该数的因数。但仔细一想,便发现本题并不满足单调性:若\(x\)满足条件,\(x-1\)不一定满足条件。

此时,我们从整除的性质入手进行分析。

我们假设\(b\)的质因数分解为

\[{p_1}^{\alpha_1}{p_2}^{\alpha_2}...{p_n}^{\alpha_n} \]

这时,若\(b \mid a\),则\(a\)的质因数分解为

\[{p_1}^{\beta_1}{p_2}^{\beta_2}...{p_n}^{\beta_n}{q_1}^{\gamma_1}{q_2}^{\gamma_2}...{q_m}^{\gamma_m}(\forall i \in [1, n], \beta_i \ge \alpha_i) \]

我们发现,若让\(b \nmid a\),则\(\exist i \in [1, n], \beta_i < \alpha_i\)。从这一点出发,我们可以推导出本题做法。首先,我们对\(b\)进行质因数分解,然后我们考虑将这\(n\)个因子在\(a\)中对应的指数\(\beta_i\)求出来。假如我们当前考虑的是第\(i\)个因子,那么当前算出来的\(x\)值就是\(\dfrac {a}{{p_i}^{\beta_i - \alpha_i + 1}}\)(相当于把\(a\)\(p_i\)这一项的指数变为\(\alpha_i - 1\),这样\(a\)肯定满足\(b \nmid a\))。将这些\(x\)值取最大即是答案。

代码(空格警告):

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
#define ll long long
int T;
ll a, b;
ll ans;
int get(ll x, ll y)
{
    int cnt = 0;
    while (x % y == 0)
    {
        x /= y;
        cnt++;
    }
    return cnt;
}
ll Pow(int base, int t)
{
    ll cnt = 1;
    for (int i = 1; i <= t; i++) cnt *= base;
    return cnt;
}
void update(int f)
{
    ll e = a;
    int c = get(b, f);
    int d = get(a, f);
    e /= Pow(f, d-c+1);
    ans = max(ans, e);
}
int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%lld %lld", &a, &b);
        ans = 1;
        for (int i = 2; i <= sqrt(b); i++)
        {
            if (b % i == 0)
            {
                update(i);
                while (!(b % i))
                {
                    b /= i;
                }
            }
        }
        if (b > 1) update(b);
        printf("%lld\n", ans);
    }
    return 0;
}

欢迎关注我的公众号:智子笔记

posted @ 2020-12-17 23:09  David24  阅读(152)  评论(0编辑  收藏  举报