UVA 10200 Prime Time(简单素数判定预处理)

Euler is a well-known matematician, and, among many other things, he discovered that the formula n 2 + n + 41 produces a prime for 0 ≤ n < 40. For n = 40, the formula produces 1681, which is 41 ∗ 41. Even though this formula doesn’t always produce a prime, it still produces a lot of primes. It’s known that for n ≤ 10000000, there are 47,5% of primes produced by the formula! So, you’ll write a program that will output how many primes does the formulaoutput for a certain interval.

Input

Each line of input will be given two positive integer a and b such that 0 ≤ a ≤ b ≤ 10000. You must read until the end of the file.

Output

For each pair a, b read, you must output the percentage of prime numbers produced by the formula in this interval (a ≤ n ≤ b) rounded to two decimal digits. SampleInput

0 39

0 40

39 40

Sample Output

100.00

97.56

50.00

最大值是100010041.

用vector来记录1~sqrt(100010041+0.5)所有的素数。

然后1到10000进行判断素数进行pp数组的预处理

ans = pp[b] - pp[a-1]。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 1e7+10;

vector<LL> prime;
bool vis[N];
int pp[N];

void init(){
    memset(vis,false,sizeof vis);
    for(int i=2;i<=N;i++) if(!vis[i]){
        prime.push_back(i);
        for(int j=i;j<=N;j+=i) vis[j]=true;
    }
    memset(pp,0,sizeof pp);pp[0]=1;
    for(int i=1;i<=10000;i++){
        LL v = i*i + i +41;
        bool cp=false;
        for(int j=0;prime[j]*prime[j]<=v;j++)
        if(v%prime[j]==0) {cp=true;break;}
        if(cp) pp[i]=pp[i-1];
        else pp[i]=pp[i-1]+1;
    }
}

int main()
{
    init();int a,b;
    while(~scanf("%d%d",&a,&b)){
        double c ,d;
        if(a) c = pp[b]-pp[a-1],d=b-a+1;
        else c=pp[b],d=b+1;
        double ans = c/d*100;
        ans+=1e-5 ;//这里是4舍5入
        printf("%.2f\n",ans);
    }
    return 0;
}

  

时间: 2024-10-11 20:14:40

UVA 10200 Prime Time(简单素数判定预处理)的相关文章

UVA 10200 Prime Time 水

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意:判断区间[a,b]的f(i)是否为素数,f(i)=i*i+i+40: 思路:打个表,然后注意精度: #include<bits/stdc++.h> using namespace std; #define ll long long #define esp

UVA 10200 Prime Time 暴力水题

一个简单的暴力水题,只是输出方式让人无语... #include <stdio.h> #include <string.h> int prime(int n) { int i; for(i=2;i*i<=n;i++) { if((n%i)==0) return 0; } return 1; } int main() { int num[10010]; int i; int a,b; int sum; memset(num,0,sizeof(num)); for(i=0;i&l

UVA 10200 Prime Time

题意:给你两个数a,b,求[a,b]内有多少个数满足f(n)=n*n+n+41是素数. 题解:预处理除前缀和,直接求 #include<cstring> #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<stack> #include<vector> #define N 10010 using namespace

UVA - 1404 Prime k-tuple (素数筛选)

Description {p1,..., pk : p1 < p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk are consecutive prime numbers and pk - p1 = s . For example, with k = 4 , s = 8 , {11, 13, 17, 19} is a prime 4-tuple of distance 8. Given an i

UVA 10200 记忆打表,素数筛,浮点误差

UVA 10200 区间预处理,浮点误差 W - Prime Time Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10200 Description 题意:测试找素数函数f(n)=n^2+n+41在区间n<-[a,b]时,找到素数的成功率. 思路:区间的素数个数,打表预处理即可.输出答案时四舍五入,需注意浮点误差,四舍五入时加上EPS. #

UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <

[Miller-Rabin][CODEVS1702]素数判定2 解题报告

题面描述:判定一个数P∈[1,2^63-1]∩N是素数么. 按照朴素的判定素数方法,至少也需要O(P^0.5)的,但这道题就是霸气到连这样的时间复杂度都过不了的地步. 实在是不会做了,就学习了传说中的Miller-Rabin素数判定法. 两个引理: ①费马小定理: 设p为质数,且不满足p|a, 则a^(p-1)=a(mod p). 证: 又一个引理,若n与p互质,且a与p互质,则n*a与p互质. 这真的是一个看似很简单的引理,但它却意味着一些看似不那么简单的事情. 设A=(0,p)∩N,则 ①对

素数判定相关资料

素数(质数)的判定 (1)最基本素数判定方法大家熟悉,只用看看2到n(或n的平方根)之间有没有n的约数: #include<stdio.h> void main() { int i,n; scanf("%d",&n); for(i=2;i<n;i++) if(n%i==0)break; if(i<n||n==1)puts("No"); else puts("Yes"); } 此方法适用于判定较少数,数据量大时会超时

uva 1404 - Prime k-tuple(数论)

题目链接:uva 1404 1404 - Prime k-tuple 题目大意:如果k个相邻的素数p1,p2,-,pk,满足pk?p1=s,称这些素数组成一个距离为s的素数k元组,给定区间a,b,求有多少个距离s的k元组. 解题思路:筛选素数法,先预处理出[1, sqrt(inf)]的素数表,然后对给定区间[a,b]根据预处理出的素数表筛选出素数即可. #include <cstdio> #include <cstring> #include <cmath> #incl