Divples .Gym - 102302B

这道题如果直接暴力一定会超时。这个时候可以利用sqrt缩减范围,然后求出另一半(数都是对称的),可以极大的简化代码运行时间。
在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

long long x[2000001];

int main()
{
    
    
    long long a,b;
    cin>>a>>b;
    int cnt=0;
    int t=sqrt(a);
    if(a%b==0)
    {
    
    
        for(int i=1; i<=t; i++)
        {
    
    
            if(a%i==0)
            {
    
    
                x[cnt++]=i;
            }
        }
        for(int i=cnt-1; i>=0; i--)
        {
    
    
            if(a/x[i]!=x[i])
                x[cnt++]=a/x[i];
        }
        for(int i=0; i<cnt; i++)
        {
    
    
            if(a%(x[i]*b)==0)
                cout<<x[i]*b<<" ";
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/JdiLfc/article/details/108818593