暴力搞肯定不行,因此我们从小到大枚举素数,用n去试除,每次除尽,如果已经超过20,肯定是no。如果当前枚举到的素数的(20-已经找到的质因子个数)次方>剩下的n,肯定也是no。再加一个关键的优化,如果剩下的次数是1了,就直接判定剩下的n是否是素数。这样可以保证次方>=2,将我们需要枚举的素数限制在200w以内,就可做了。线性筛在这题虽然不必要,但是可以当个板子存下来。
The hacker Michael develops breakthrough password manager, which is called KEK (Keeper of Encrypted Keys). A distinctive feature of KEK is excellent security. To achieve this, Michael had to develop innovative encryption scheme. For example, in the well-known RSA scheme the sum of prime powers in the factorization is equal to 2, whereas in Michael’s scheme this sum is equal to 20!
However, the current version of the KEK runs very slow. Michael has found out that the problem is in the function of checking a modulus for correctness. This function should take the number
n and answer, whether the sum of prime powers included in the factorization of
n is equal to 20. Can you do this quickly?
Remember that the factorization of an integer is the representation of it in the form like
p
1
α1 ·
p
2
α2 · ... ·
p
k
αk, where
p
i are prime numbers, and
α
i> 0. It is known that such representation is unique. Then the sum of powers looks like
α
1 +
α
2 + ... +
α
k.
Input
The only line contains an integer
n (1 ≤
n ≤ 10
18).
Output
If the sum of prime powers, included in the factorization of
n, is equal to 20, then output “Yes”, otherwise output “No”.
Example
input | output |
---|---|
2 | No |
1048576 | Yes |
10000000000 | Yes |
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
#define MAXP 2000000
#define EPS 0.00000001
ll n;
bool isNotPrime[MAXP+10];
int num_prime,prime[MAXP+10];
void shai()
{
for(long i = 2 ; i < MAXP ; i ++)
{
if(! isNotPrime[i])
prime[num_prime ++]=i;
for(long j = 0 ; j < num_prime && i * prime[j] < MAXP ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if( !(i % prime[j]))
break;
}
}
}
bool is_prime(ll x)
{
if(x==1ll)
return 0;
for(ll i=2;i*i<=x;++i)
if(x%i==0)
return 0;
return 1;
}
int m=20;
int main()
{
scanf("%I64d",&n);
shai();
for(int i=0;i<num_prime;++i)
{
if((double)m*log((double)prime[i])-log((double)n)>EPS)
{
puts("No");
return 0;
}
while(n%(ll)prime[i]==0)
{
n/=(ll)prime[i];
--m;
}
if(m==0 && n==1)
{
puts("Yes");
return 0;
}
if(m<0 || (m==0 && n>1))
{
puts("No");
return 0;
}
if(n>1 && m==1)
{
if(is_prime(n))
{
puts("Yes");
return 0;
}
else
{
puts("No");
return 0;
}
}
}
return 0;
}