分解质因数cf1445

题意:

给出两个数p和q。

求最大的x, 使得p%x==0 && q %x !=0

思路:

如果p%q!=0, x就是p。

如果p % q ==0。那么q是p的因子,q的因子都是p的因子。q可以表示为\sum c_i^ki, ci是质因数,p也一样。

x只需要有一个质因子和q不同就行了。

求最大的x,就先让x等于p, 然后一直去除以q的质因子,直到x不能整除q,取最大值。

#pragma warning(disable:4996)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 505;
const ll mod = 10000;
ll p, q;
int main()
{
	ll n, x, t;
	scanf("%lld", &n);
	while (n--)
	{
		scanf("%lld%lld", &p, &q);
		if (p %q != 0)printf("%lld\n", p);
		else
		{
			t = q;
			x = 0;
			for (ll i = 2; i*i <= t; i++)
			{
				if (t%i)continue;//不能整除
				ll now = p;
				while (now %q == 0)now /= i;
				while (t%i == 0)t /= i;
				x = max(x, now);
			}
			if (t > 1)//如果t是质数的情况
			{
				ll now = p;
				while (now %q == 0)now /= t;
				x = max(x, now);
			}
			printf("%lld\n", x);
		}
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值