您的位置:首页 > 其它

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
inputoutput
2

No

1048576

Yes

10000000000

Yes

Source
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: