CF588B 题解
这是一篇 Python 题解,文末也会放上 C++ 代码。
首先,一个可爱的数中每个质因数的次数都小于
为什么?
假设一个数中质因数
设
代码如下:
Python
def check(x):
i=2
while i*i<=x:
if x%i==0:
return 0
i=i+1
return 1
a=int(input())
ans=1
while a!=1 and check(a)==0:
now=2
while a%now!=0:
now=now+1
ans=ans*now
while a%now==0:
a=a//now
ans=ans*a
print(ans)
C++
#include<iostream>
using namespace std;
long long a,ans=1;
long long check(long long x){
long long i=2;
while(i*i<=x){
if(x%i==0){
return 0;
}
i++;
}
return 1;
}
signed main(){
cin>>a;
while(a!=1&&check(a)==0){
long long now=2;
while(a%now!=0){
now++;
}
ans*=now;
while(a%now==0){
a/=now;
}
}
ans*=a;
cout<<ans;
}
代码中,ans为答案,now为当前a最小的质因数,a=a//now(Python)和a/=now;(C++)的作用是消去now这个质因数。
这里做了一点小优化:当a为质数时,一个一个找a最小的质因数的耗时会很大(Python 会直接超时)。check()函数的作用是判断一个数是否为质数,当a为质数时,a的最小的质因数即为a,且只有这一个质因数,所以可以直接将ans乘上a后输出。