Codeforces Round #732 (Div. 2) C. AquaMoon and Strange Sort

题目

  • 题意

给你两个数   p , q   ~p,q~  p,q  ,让你找到最大的一个数   x   ~ x~  x ,使得   x   ~x~  x  整除   p   ~p~  p ,但是不满足   q   ~q~  q  整除   x   ~x~  x 

  • 思路

显然当   p % q ! = 0   ~p\%q!=0~  p%q!=0 时,答案就是   p   ~p~  p ;但是当   p % = = 0 时   ~p\%==0时~  p%==0 ,如何处理,这里套路使然嘛,肯定要往 算术基本定理 上想:很显然   p   ~p~  p 包含的质因子,   p   ~p~  p 都包含,所以我们可以处理出   q   ~q~  q 的质因子,然后不断用   p   ~p~  p 除以   q   ~q~  q 的质因子,直到   p % q ! = 0   ~p\%q!=0~  p%q!=0 ,同时记录最大值。
时间复杂度   O ( 1 e 9 )   ~O(\sqrt{1e9})~  O(1e9 ) 
注:这里为什么要处理q的质因子,因为   1 e 9   ~1e9~  1e9 的开根号   1 e 4.5   ~1e4.5~  1e4.5 .。
处理   q   ~q~  q 的质因子也不能把素数筛出来,哎,还是对这样的题不熟吧,应该用下面的方法,自己看吧。

  • 代码
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=1e7+10,mod=1e9+7;
int a[N];
#define PII pair<int,long double>
#define x first
#define y second
#define push_back push_back

void solve()
{
	ll a,b;cin>>a>>b;
	if(a%b!=0){
		cout<<a<<endl;
	}
	else
	{
		ll ans=0;
		vector<int> v; v.clear();
        ll tmp=b;
        for(ll i=2;i*i<=b;i++){
            if(tmp%i) continue;
            while(tmp%i==0) tmp/=i;
            v.push_back(i);
        }
        if(tmp>1) v.push_back(tmp);
        for(int i=0;i<v.size();i++){
            tmp=a;
            while(tmp%v[i]==0 && tmp%b==0) tmp/=v[i];
            ans=max(ans,tmp);
        }
        cout<<ans<<endl;
	}
	
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

疯狂的码泰君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值