C. Division--唯一分解定理

Oleg’s favorite subjects are History and Math, and his favorite branch of mathematics is division.

To improve his division skills, Oleg came up with 𝑡 pairs of integers 𝑝𝑖 and 𝑞𝑖 and for each pair decided to find the greatest integer 𝑥𝑖, such that:

𝑝𝑖 is divisible by 𝑥𝑖;
𝑥𝑖 is not divisible by 𝑞𝑖

Oleg is really good at division and managed to find all the answers quickly, how about you?
Input
The first line contains an integer 𝑡(1≤𝑡≤50) — the number of pairs.
Each of the following 𝑡 lines contains two integers 𝑝𝑖 and 𝑞𝑖 (1≤𝑝𝑖≤1018; 2≤𝑞𝑖≤109) — the 𝑖-th pair of integers.
Output

Print
𝑡 integers: the 𝑖-th integer is the largest 𝑥𝑖 such that 𝑝𝑖 is divisible by 𝑥𝑖, but 𝑥𝑖 is not divisible by 𝑞𝑖.
One can show that there is always at least one value of 𝑥𝑖 satisfying the divisibility conditions for the given constraints.
Example
Input
3
10 4
12 6
179 822

Output
10
4
179
Note
For the first pair, where 𝑝1=10 and 𝑞1=4, the answer is 𝑥1=10, since it is the greatest divisor of 10 and 10 is not divisible by 4.

For the second pair, where 𝑝2=12 and 𝑞2=6, note that 12 is not a valid 𝑥2, since 12 is divisible by 𝑞2=6; 6 is not valid 𝑥2 as well: 6 is also divisible by 𝑞2=6 .

The next available divisor of 𝑝2=12 is 4, which is the answer, since 4 is not divisible by 6

题目大意:给出q,p,找出一个最大的x能被q整除不能被整除
官方题解:
在这里插入图片描述

大佬写的代码—

#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

int t;
long long p, q;
int a[55], cnt;
long long k;
int main()
{
    int i;
    long long x;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lld%lld", &p, &q);//输入p,q
        cnt = 0;
        for (i = 2, x = q; (long long)i * i <= q; i++)//筛出q的p1,p2,p3...(唯一分解定理里面的底数)
            if (!(x % i))
            {
                a[++cnt] = i;
                while (!(x % i))
                    x /= i;
            }
        //例如q=300000时,q=2^5*3^1*5^5,此时数组a中存储的元素是2,3,5,cnt=3;
        if (x > 1)  //如果最后x大于1,即最后产生了一个小的素数,直接保存即可
            a[++cnt] = x, x = 1;
        //执行完上面的代码后 x==1,本题如果没有的话就输出1;
        for (i = 1; i <= cnt; i++)
        {
            k = p;
            while (!(k % q))//找到不能变小的
                k /= a[i];
            if (k > x)//找出来最大的
                x = k;
        }
        printf("%lld\n", x);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JdiLfc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值