Prime Time UVA - 10200
题意很简单求[a,b]中f(i)是素数的百分比
我当时一看ab都不大可以直接暴力,也没想打表直接用了拉宾米勒素数测试,结果还是超时了,然后发现可以直接用最笨的方法判断只要打表就可以了,结果他妈精度卡的蛋疼,wa了十多发,最后要加一个esp!
以后做题涉及精度的得注意了,不行就得加esp!
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <time.h>
#include <stdlib.h>
using namespace std;
int num[10010];
bool check(int n){
for(int i = 2; i * i <= n; i++){
if(n % i == 0)
return 0;
}
return 1;
}
void init(){
for(int i = 0; i <= 10000; i++){
int tmp = i * i + i + 41;
num[i] = num[i-1];
if(check(tmp)) num[i]++;
}
}
int main(){
int a,b;
init();
while(scanf("%d%d",&a,&b) != EOF){
int ans = b - a + 1;
int cnt = num[b] - num[a];
if(check(a*a+a+41)) cnt++;
printf("%.2f\n",((cnt*1.0)/(ans*1.0)+1e-8)*100.0);
}
return 0;
}