zoukankan      html  css  js  c++  java
  • Ural2102:Michael and Cryptography(数论&素数)

    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

    题意:给定数字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个素数因子,然后判断剩下的数是不是素数即可。

    #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
    ");
            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
    "); return 0;}
                if(num>=20){ printf("No
    ");return 0;}
            }
        }
        if(num<19) printf("No
    ");
        else if(num==19&&N>1&&isprime(N)) printf("Yes
    ");
        else printf("No
    ");
        return 0;
    }
     
  • 相关阅读:
    ArcEngine实现对点、线、面的闪烁(转载)
    好久没写博客了.把这几个月的开发过程做一个总结
    利用暴力反编译的程序处理ArcXML数据遇到的问题小结(纯粹研究目的)
    ArcSde 9.2与Oracle 10g是最佳搭档
    当ArcEngine报事件同时存在于AxMapControl,MapControl时的解决方法(转载)
    写在苏州火炬接力的最后一站
    提问,如何才能触发鼠标事件
    地铁线路图高性能查找算法系统,最短路径查询地铁网络拓扑高效率算法原创附带demo
    二分查找
    .net面试题
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8858969.html
Copyright © 2011-2022 走看看