【UVa】10200 - Prime Time(打表)

来源:互联网 发布:手机淘宝店铺装修教程 编辑:程序博客网 时间:2024/04/28 13:52

点击打开题目

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 formula output 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.


Sample Input
0 39
0 40
39 40


Sample Output
100.00
97.56
50.00



题意就是说用 n^2 + n +41 这个公式出来的数是素数的概率。

对0~10000打一个表就行了。

输出的时候注意四舍五入(这一点特别坑)

代码如下:

#include <stdio.h>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3f#define LL long long#define f(x) (x*x+x+41)int sum[10000+11];bool isPrime(int x){int endd = sqrt(x);for (int i = 2 ; i <= endd ; i++){if (x % i == 0)return false;}return true;}void getPrime(){sum[0] = 1;for (int i = 1 ; i <= 10000 ; i++){if (isPrime(f(i)))sum[i] = sum[i-1] + 1;elsesum[i] = sum[i-1];}}int main(){getPrime();int n;int l,r;double ans;while (scanf ("%d %d",&l,&r) != EOF){if (l == 0)ans = (double)(sum[r]) / (r+1);elseans = (double)(sum[r] - sum[l-1]) / (r - l + 1);ans = (int)(ans*10000+0.5) / 100.0;printf ("%.2lf\n",ans);}return 0;}



0 0