zoukankan      html  css  js  c++  java
  • UVA 10200 记忆打表,素数筛,浮点误差

    UVA 10200 区间预处理,浮点误差

    W - Prime Time
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Description

    Download as PDF
     
    题意:测试找素数函数f(n)=n^2+n+41在区间n<-[a,b]时,找到素数的成功率。
    思路:区间的素数个数,打表预处理即可。输出答案时四舍五入,需注意浮点误差,四舍五入时加上EPS。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<set>
    #include<map>
    #include<string>
    #include<math.h>
    #include<cctype>
    #define ll long long
    #define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
    #define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t))
    #define PII pair<int,int>
    #define MP make_pair
    #define PB push_back
    #define RI(x) scanf("%d",&(x))
    #define RLL(x) scanf("%lld",&(x))
    #define RI64(x) scanf("%I64d",&(x))
    #define DRI(x) int x;scanf("%d",&(x))
    #define DRLL(x) ll x;scanf("%lld",&(x))
    #define DRI64(x) llx;scanf("%I64d",&(x))
    #define MS0(a) memset((a),0,sizeof((a)))
    #define MS1(a) memset((a),0,sizeof((a)))
    #define MS(a,b) memset((a),(b),sizeof((a)))
    
    using namespace std;
    
    const int maxn=10000100;
    const int INF=(1<<29);
    const double EPS=0.0000000001;
    const double Pi=acos(-1.0);
    
    ll a,b;
    bool isprime[maxn*10];
    vector<int> prime;
    ll cnt[maxn];
    
    void getPrime()
    {
        MS(isprime,1);
        isprime[1]=0;
        REP(i,2,maxn-1){
            if(!isprime[i]) continue;
            REPP(j,i*2,maxn-1,i) isprime[j]=0;
        }
        REP(i,1,maxn-1) if(isprime[i]) prime.PB(i);
    }
    
    ll f(ll n)
    {
        return n*n+n+41;
    }
    
    bool isP(ll n)
    {
        REP(i,0,prime.size()){
            if(prime[i]*1LL*prime[i]>n||prime[i]*1LL*prime[i]<=0) break;
            if(n%prime[i]==0) return 0;
        }
        return 1;
    }
    
    void getCnt()
    {
        MS0(cnt);
        cnt[0]=1;
        REP(i,1,10010){
            ll x=f(i);
            if(x<maxn){
                if(isprime[x]) cnt[i]=cnt[i-1]+1;
                else cnt[i]=cnt[i-1];
            }
            else{
                if(isP(x)) cnt[i]=cnt[i-1]+1;
                else cnt[i]=cnt[i-1];
            }
        }
    }
    
    int main()
    {
        getPrime();
        getCnt();
        //cout<<f(10000)<<endl;
        while(cin>>a>>b){
            printf("%.2f
    ",(cnt[b]-(a?cnt[a-1]:0))*100.0/(b-a+1)+EPS);
        }
        return 0;
    }
    View Code
     
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    eclipse下c/cpp " undefined reference to " or "launch failed binary not found"问题
    blockdev 设置文件预读大小
    宝宝语录
    CentOS修改主机名(hostname)
    subprocess报No such file or directory
    用ldap方式访问AD域的的错误解释
    英特尔的VTd技术是什么?
    This virtual machine requires the VMware keyboard support driver which is not installed
    Linux内核的文件预读详细详解
    UNP总结 Chapter 26~29 线程、IP选项、原始套接字、数据链路访问
  • 原文地址:https://www.cnblogs.com/--560/p/4570480.html
Copyright © 2011-2022 走看看