UVA

来源:互联网 发布:qt matlab混合编程 编辑:程序博客网 时间:2024/06/03 22:05

题解:

精度精度!不懂为什么,难道是因为四舍五入的?

Euler is a well-known matematician, and, among many other things, he discovered that the formulan2+n+41 produces a prime for 0n <40. For n= 40, the formula produces 1681, which is 4141.Even though this formula doesn’t always produce a prime, it still produces a lot of primes. It’s knownthat 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 formula output for a certaininterval.

Input

Each line of input will be given two positive integera andb such that 0ab10000. You mustread 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 inthis interval (anb) rounded to two decimal digits. 

Sample Input

0 390 4039 40

Sample Output

100.0097.5650.00
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<sstream>#define inf 0x3f3f3fusing namespace std;#define maxn 10005double eps = 1e-6;typedef long long ll;int a[maxn];int is_prime(int n){    for(int i=2;i<=sqrt(n);i++)    {        if(n%i==0)            return 0;    }    return 1;}void get(){    for(int i=0;i<maxn;i++)    {        int ans=i*i+i+41;        a[i]=a[i-1]+is_prime(ans);    }}int main(){    int a1,b1;    get();    while(~scanf("%d%d",&a1,&b1))    {        int d=a[b1]-a[a1-1];        printf("%.2lf\n",d*100.0/(b1-a1+1)+eps);    }    return 0;}


原创粉丝点击