Ural2102:Michael and Cryptography(数论&素数)
2018-04-16 22:28
633 查看
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
题意:给定数字N(1e18级),问将其唯一分解后(N=a1^p1*a2^p2...),幂的和(p1+p2+p3...)是否为20。
思路:根号N等于1e9级别,显然不能普通地分解因子来做。但是注意到20比较小,可以从20出发考虑:
将N分解后不可能有两个大于1e6的因子。因为1e6*1e6*2^18>2e18。认识到这一点,说明最多只有一个大于1e6的因子。所以只要在[1,1e6]找19个素数因子,然后判断剩下的数是不是素数即可。
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 |
思路:根号N等于1e9级别,显然不能普通地分解因子来做。但是注意到20比较小,可以从20出发考虑:
将N分解后不可能有两个大于1e6的因子。因为1e6*1e6*2^18>2e18。认识到这一点,说明最多只有一个大于1e6的因子。所以只要在[1,1e6]找19个素数因子,然后判断剩下的数是不是素数即可。
#include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ll long long const int maxn=2000000; int p[maxn+10],vis[maxn+10],cnt; void getprime() { for(int i=2;i<=maxn;i++){ if(!vis[i]) p[++cnt]=i; for(int j=1;p[j]*i<=maxn;j++){ vis[i*p[j]]=1; if(i%p[j]==0) break; } } } bool isprime(ll x) { for(int i=2;i*i<=x;i++) if(x%i==0) return false; return true; } int main() { getprime(); ll N; int num=0; scanf("%lld",&N); if(N<1024*1024){ printf("No\n"); return 0; } for(int i=1;i<=cnt;i++){ while(N%p[i]==0){ N/=p[i]; num++; if(num==20&&N==1){ printf("Yes\n"); return 0;} if(num>=20){ printf("No\n");return 0;} } } if(num<19) printf("No\n"); else if(num==19&&N>1&&isprime(N)) printf("Yes\n"); else printf("No\n"); return 0; }
相关文章推荐
- 【线性筛】【筛法求素数】【素数判定】URAL - 2102 - Michael and Cryptography
- ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
- 2016_S&P_Hawk:The Blockchain Model of Cryptography and Privacy-Preserving Smart Contracts
- Goldbach's Conjecture(数论--素数判断)
- URAL 1430. Crime and Punishment(数论)
- UVA 10061 How many zero's and how many digits ?(数论)
- 【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)
- hdu4497——GCD and LCM(数论&容斥原理or排列组合)
- hihoCoder 九十二周 数论一·Miller-Rabin质数测试 (数论 筛素数)
- CF#319-C. Vasya and Petya's Game-猜数-数论
- 【数论】埃氏筛法&&CODE[VS] 3223 素数密度 = =
- ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
- Ural 2003: Simple Magic(数论&思维)
- 筛选素数空间压缩(数论)&& Double Happiness
- HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
- 【二进制 && bitset】URAL - 2108 Oleg and Little Ponies
- HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
- [kuangbin带你飞]专题十四 数论基础 C - Aladdin and the Flying Carpet (线性素数筛,分解质因数)
- HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
- [数论] Codeforces 819D R #421 D.Mister B and Astronomers & 516E R #292 E. Drazil and His Happy Friends