题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141
Sample Input 0 39 0 40 39 40 Sample Output 100.00 97.56 50.00
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <vector> 9 using namespace std; 10 11 #define N 12000 12 #define INF 0x3f3f3f3f 13 14 int f[N]; 15 16 int q(long long m) 17 { 18 int k=(int)sqrt(m); 19 for(int i=2;i<=k;i++) 20 if(m%i==0) 21 return 0; 22 return 1; 23 } 24 25 int main() 26 { 27 int a,b; 28 f[0]=1; 29 30 for(int i=1;i<N;i++) 31 f[i]=f[i-1]+q(i*i+i+41); 32 33 while(scanf("%d%d", &a,&b) != EOF) 34 { 35 double s; 36 if(a==0) 37 s=1.0*f[b]/(b-a+1); 38 else 39 s=1.0*(f[b]-f[a-1])/(b-a+1); 40 printf("%.2f\n", s*100+1e-8); 41 } 42 43 return 0; 44 }