G - Factorial and Multiple 解説
by
TOMWT
Another way
#include<stdio.h>
#define int long long
inline int gcd(const int&x,const int&y){if(y)return gcd(y,x%y);return x;}
inline void min(int&x,const int&y){if(x>y)x=y;}
main()
{
int n,m,ans=0,nxt;scanf("%lld",&n);
for(;n>1;)//repeat the algorithm below again and again
{
m=n;nxt=1ll<<60;
for(int i=2;i*i<=m;++i)if(!(m%i))
for(min(nxt,(ans/i+1)*i);!(m%i);m/=i);
if(m>1)min(nxt,(ans/m+1)*m);
//find the smallest number named nxt that is greater than the current answer and makes gcd(n,nxt)>1
ans=nxt;n/=gcd(n,nxt);
}
printf("%lld",ans);
}
投稿日時:
最終更新: