【线性筛】【筛法求素数】【素数判定】URAL - 2102 - Michael and Cryptography
2017-01-19 01:59
531 查看
暴力搞肯定不行,因此我们从小到大枚举素数,用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
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; }
相关文章推荐
- Ural2102:Michael and Cryptography(数论&素数)
- [kuangbin带你飞]专题十四 数论基础 C - Aladdin and the Flying Carpet (线性素数筛,分解质因数)
- codeforces 385C C. Bear and Prime Numbers(线性筛素数)
- CF385C-(Bear and Prime Numbers)线性筛素数
- cf D. Dima and Lisa (三素数定理_素数打表+判定)
- 线性筛素数and欧拉函数
- 【线性筛】【筛法求素数】【约数个数定理】URAL - 2070 - Interesting Numbers
- HDU 2012 --- 素数判定
- codevs 1702素数判定2
- wikioi 素数判定
- 2012 素数判定
- ECPP——利用有限域上的椭圆曲线,精确判定素数的算法
- 素数判定
- 素数判定算法
- 【POJ】-3641-Pseudoprime numbers(快速幂,大数素数判定)
- wikioi 2849 素数判定 3 (筛法求质)
- 线性求区间欧拉函数(顺便线性求区间内所有素数)(类似欧拉线性素数筛)
- URAL 1553. Caves and Tunnels 树链剖分
- 【hdu】 Pseudoprime numbers 伪素数(快速幂+判定素数)
- 3813: 奇数国 线段树+线性筛素数+欧拉函数+乘法逆元+压位