暴力搞肯定不行,因此我们从小到大枚举素数,用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;
}

【线性筛】【筛法求素数】【素数判定】URAL - 2102 - Michael and Cryptography的更多相关文章

  1. 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Sta ...

  2. 2018.08.29 NOIP模拟 pmatrix(线性筛)

    [问题描述] 根据哥德巴赫猜想(每个不小于 6 的偶数都可以表示为两个奇素数之和),定义 哥德巴赫矩阵 A 如下:对于正整数对(i,j),若 i+j 为偶数且 i,j 均为奇素数,则 Ai,j = 1 ...

  3. 莫比乌斯反演/线性筛/积性函数/杜教筛/min25筛 学习笔记

    最近重新系统地学了下这几个知识点,以前没发现他们的联系,这次总结一下. 莫比乌斯反演入门:https://blog.csdn.net/litble/article/details/72804050 线 ...

  4. <转载>一般筛法和快速线性筛法求素数

    素数总是一个比较常涉及到的内容,掌握求素数的方法是一项基本功. 基本原则就是题目如果只需要判断少量数字是否为素数,直接枚举因子2 ..N^(0.5) ,看看能否整除N. 如果需要判断的次数较多,则先用 ...

  5. 欧拉筛法模板and 洛谷 P3383 【模板】线性筛素数(包括清北的一些方法)

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入格式 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行包含一个不小于1 ...

  6. 欧拉筛法模板&&P3383 【模板】线性筛素数

    我们先来看欧拉筛法 •为什么叫欧拉筛呢?这可能是跟欧拉有关 •但是为什么叫线性筛呢?因为它的复杂度是线性的,也就是O(n),我们直接来看代码   #include<cstdio> #inc ...

  7. Algorithm --> 筛法求素数

    一般的线性筛法 genPrime和genPrime2是筛法求素数的两种实现,一个思路,表示方法不同而已. #include<iostream> #include<math.h> ...

  8. [Luogu]A%BProblem——线性筛素数与前缀和

    题目描述 题目背景 题目名称是吸引你点进来的[你怎么知道的] 实际上该题还是很水的[有种不祥的预感..] 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m接下来n行, ...

  9. 全网一定不是最好懂的C++线性筛素数

    Part 0:概念 先给几个概念(很重要): 合数:如果\(xy=z\text{且}x,y\text{为正整数}\),我们就说\(x,y\text{是}z\text{的合数}\) 素数:如果数\(a\ ...

随机推荐

  1. Spring AOP execution表达式

    Spring中事务控制相关配置: <bean id="txManager" class="org.springframework.jdbc.datasource.D ...

  2. 洛谷P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...

  3. [hdu 1398]简单dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1398 看到网上的题解都是说母函数……为什么我觉得就是一个dp就好了,dp[i][j]表示只用前i种硬币 ...

  4. Maven环境搭建、调试、打包

    1.配置Maven环境 将下载文件解压,然后设置maven环境 新建环境变量M2_HOME 变量名:M2_HOME 变量值:F:\maven\apache-maven-3.0.3 追加path环境变量 ...

  5. BigDecimal与Long、int之间的互换

    在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigDecimal 数据类型 //int 转换成 big ...

  6. abstract 与interface区别

    1.abstract用于修饰类,interface用于修饰接口 2.抽象类中可以有抽象和非抽象方法,接口中只能定义抽象方法,不能有实现 3.抽象类必须被继承,interface被实现 4.抽象类有构造 ...

  7. 【CodeForces】841D. Leha and another game about graph(Codeforces Round #429 (Div. 2))

    [题意]给定n个点和m条无向边(有重边无自环),每个点有权值di=-1,0,1,要求仅保留一些边使得所有点i满足:di=-1或degree%2=di,输出任意方案. [算法]数学+搜索 [题解] 最关 ...

  8. 【洛谷 P4211】[LNOI2014]LCA(树链剖分,差分)

    题目链接 看到题目肯定首先想到要求LCA(其实是我菜),可乍一看,n与q的规模为5W, 求LCA的复杂度为\(O(logN)\),那么总时间复杂度为\(O(nq\ log\ n)\). 怎么搞呢? 会 ...

  9. requests上传文件

    """ requests上传文件时,如果文件名是中文,会导致上传失败,参考:https://www.cnblogs.com/liaofeifight/p/5807901. ...

  10. /usr/bin/expect介绍

    /usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...