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;
    }
     
  • 相关阅读:
    getchar,putchar函数
    强制类型转换和整数常量的数据类型及转换
    c语言整型的隐式数据 类型转换
    c语言整型数据输出格式声明
    c语言整型变量的储存空间,以及所表示的整数范围
    c语言常量
    c语言求回文数
    Android4.0源码目录结构详解
    MTK Android源代码目录
    Comparator 和 Comparable
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8858969.html
Copyright © 2011-2022 走看看