B - Fadi and LCM CodeForces - 1285C

题目大意很简单,给你一个整数X,让你求a和b,使得max(a,b)尽可能的小,然后打印a,b

题解:想到了质因子分解,也考虑到了暴力,但是觉得暴力的话会TLE,所以打算用贪心做,然后就一直Wa......。看人家的题解,,就是暴力..将求出的质因子分为两部分即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll M=1E12+7;
const ll N=1e6+7;
ll arr[N];
ll pos=0;
bool mp[N];
ll ans=M;
void dfs(ll a,ll b,ll p){ 
    ans=min(ans,max(a,b));
    if(p>=pos) return ;
    dfs(a/arr[p],b,p+1);
    dfs(a,b/arr[p],p+1);
}
int main(){
    ll x;
    cin>>x;
    ll x1=x;
    ll q=sqrt(x);
    for(ll i=2;i<=q;i++){
        if(x%i==0)arr[pos++]=1;
        while(x%i==0){
            x/=i;
            arr[pos-1]*=i;
        }
    }
    if(x!=1) arr[pos++]=x;
    ll a=x1,b=x1;
    dfs(x1,x1,0);
    cout<<ans<<" "<<x1/ans<<endl; 
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Accepting/p/12209537.html