URAL - 2102 Michael and Cryptography 分解质因数+优化
2017-01-19 16:26
369 查看
C - Michael and Cryptography
URAL- 2102
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 |
URAL - 2102
Source
题意:每一个数都可以表示为 ai^bi + ai+1^bi+1 + ...... an^bn,判断 sigma bi 是否等于 20分解质因数+优化
用O(sqrt(n))的分解质因数的方法,然后优化,sum记录次方的值,1、当sum >= 20 时直接break,不用管后面的;2、当if(sum == 19) 如果当前 i^2 > now 如果 i <= now 则接下来有且只有一个质因数,直接break; 如果 i > now 也直接f = false;break; 3、当前i的20 - sum次,已经大于now了,即接下来now也不可能使sum达到20,故直接break此外 n == 1时额外判断为 No复杂度 远小于O(sqrt(n))
#include <iostream> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; const LL maxn = 1e6 + 8; inline LL mod(const LL &a, const LL &b) { return a - a / b * b; } int main() { #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); LL T = 4; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); LL n; cin >> n; if(n == 1) cout << "No" << endl; else{ LL sum = 0; bool f = true; LL temp, now; temp = (LL)((double)sqrt(n) + 1); now = n; for(LL i = 2; i <= temp; i++){ if(sum >= 20) break; if(mod(now, i) == 0){ while(mod(now, i) == 0){ sum++; now /= i; } } if(sum >= 20) break; if(sum == 19){ if(pow(i + 1, 2) > now){ if(i + 1 <= now) break; else { f = false; break;} } } if((LL)pow(i + 1, 20 - sum) > now) {f = false; break;} } if(f && now != 1){ sum++; } if(!f) cout << "No" << endl; else if(sum == 20) cout << "Yes" << endl; else cout << "No" << endl; } #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- 【URAL 1223】Chernobyl’ Eagle on a Roof(传说中的DP神优化之鹰蛋)
- Ural2102:Michael and Cryptography(数论&素数)
- ACM k进制高精度优化 URAL 1803 The Czechs' Rifles 滚动数组
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
- URAL 1203 Scientific Conference (贪心 + 优化)
- URAL - 1097(线段树优化)
- 【bitset+string+关闭流同步优化】Oleg and Little Ponies URAL - 2108
- 【线性筛】【筛法求素数】【素数判定】URAL - 2102 - Michael and Cryptography
- HDOJ2102 深搜DFS解法+剪枝优化 入门题
- ural1223 鹰蛋(dp优化)
- 如何优化JavaScript脚本的性能
- 内存池性能优化 固定内存块使用 之 链表实现
- Silverlight体积优化
- PHP代码优化
- 【北京讲座】jQuery性能优化和HTML 5下的Web开发(2011.12.18)
- Sql Server查询性能优化之走出索引的误区
- 优化后的 求素数
- ural 1207. Median on the Plane(极角排序)
- mysql优化案例分析
- codechef April challenge (1)(学习去优化时间效率???)