zoukankan      html  css  js  c++  java
  • 【线性筛】【筛法求素数】【素数判定】URAL

    暴力搞肯定不行,因此我们从小到大枚举素数,用n去试除,每次除尽,如果已经超过20,肯定是no。如果当前枚举到的素数的(20-已经找到的质因子个数)次方>剩下的n,肯定也是no。再加一个关键的优化,如果剩下的次数是1了,就直接判定剩下的n是否是素数。这样可以保证次方>=2,将我们需要枚举的素数限制在200w以内,就可做了。线性筛在这题虽然不必要,但是可以当个板子存下来。

    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
    #include<cstdio>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    #define MAXP 2000000
    #define EPS 0.00000001
    ll n;
    bool isNotPrime[MAXP+10];
    int num_prime,prime[MAXP+10];
    void shai()
    {
    	for(long i = 2 ; i <  MAXP ; i ++)    
        {    
            if(! isNotPrime[i])    
                prime[num_prime ++]=i;    
            for(long j = 0 ; j < num_prime && i * prime[j] <  MAXP ; j ++)    
            {    
                isNotPrime[i * prime[j]] = 1;    
                if( !(i % prime[j]))    
                    break;    
            }    
        }    
    }
    bool is_prime(ll x)
    {
    	if(x==1ll)
    	  return 0;
    	for(ll i=2;i*i<=x;++i)
    	  if(x%i==0)
    	    return 0;
    	return 1;
    }
    int m=20;
    int main()
    {
    	scanf("%I64d",&n);
    	shai();
    	for(int i=0;i<num_prime;++i)
    	  {
    	  	if((double)m*log((double)prime[i])-log((double)n)>EPS)
    	  	  {
    	  	  	puts("No");
    	  	  	return 0;
    	  	  }
    	  	while(n%(ll)prime[i]==0)
    	  	  {
    	  	  	n/=(ll)prime[i];
    	  	  	--m;
    	  	  }
    	  	if(m==0 && n==1)
    	  	  {
    	  	  	puts("Yes");
    	  	  	return 0;
    	  	  }
    	  	if(m<0 || (m==0 && n>1))
    	  	  {
    	  	  	puts("No");
    	  	  	return 0;
    	  	  }
    	  	if(n>1 && m==1)
    	  	  {
    	  	  	if(is_prime(n))
    	  	  	  {
    	  	  	  	puts("Yes");
    	  	  	  	return 0;
    	  	  	  }
    	  	  	else
    	  	  	  {
    	  	  	  	puts("No");
    	  	  	  	return 0;
    	  	  	  }
    	  	  }
    	  }
    	return 0;
    }
  • 相关阅读:
    思科 ASA 系列防火墙 官方文档下载指南
    Batch批量替换hosts
    OPCDA通信--工作在透明模式下的CISCO ASA 5506-X防火墙配置
    OPC DA通讯 KEP6.4 DCOM 配置脚本
    拖放获取文件信息的bat代码
    禁用UpdateOrchestrator重新启动任务
    SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字
    提问的智慧 (提问前必读)
    [AHK]输入法状态提示,中文状态提示“中”,英文状态提示“EN”[转]
    Wincc V7.3SE安装截图
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/6304319.html
Copyright © 2011-2022 走看看