UVA10200 Prime Time

 1 /*
 2  UVA10200 Prime Time
 3  https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141
 4  素数 概率论 暴力
 5
 6  输出的时候加1e-5到1e-14都能过,不加就wa,不是很懂
 7  *
 8  *
 9  *
10  */
11 #include <cstdio>
12 #include <algorithm>
13 #include <cmath>
14 using namespace std;
15 const int Nmax=10001;
16 #define dabiao
17 #ifdef dabiao
18 int f[Nmax];
19 int num[Nmax];
20 int is_prime(long long n)
21 {
22     long long sqr=sqrt(n);
23     for(int i=2;i<=sqr+1;i++)
24         if(n%i==0)
25             return 0;
26     return 1;
27 }
28
29 inline long long get(long long a)
30 {
31     return a*a+a+41;
32 }
33
34 int main()
35 {
36     for(int i=0;i<Nmax;i++)
37     {
38         f[i]=is_prime(get(i));
39         if(i==0)
40             num[i]=f[i];
41         else
42             num[i]=f[i]+num[i-1];
43     }
44     int a,b;
45     while(scanf("%d%d",&a,&b)==2)
46     {
47         if(a==0)
48             printf("%.2lf\n",100.0*num[b]*1.0/((b+1)*1.0)+1e-5);
49         else
50             printf("%.2lf\n",100.0*(num[b]*1.0-num[a-1]*1.0)/(b*1.0-a*1.0+1)+1e-5);
51     }
52     return 0;
53 }
54
55
56 #endif
时间: 2024-10-20 04:33:20

UVA10200 Prime Time的相关文章

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i

HDU 1389 继续畅通工程【最小生成树,Prime算法+Kruskal算法】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21871    Accepted Submission(s): 9356 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

HDU 1016 Prime Ring Problem(DFS)

题目链接 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

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

POJ 2689 Prime Distance(素数区间筛法--经典题)

大致题意:给定[L,R]区间,找出区间内的每个素数 数据范围 : 1<=L< R<=2,147,483,647) R-L <=1,000,000. R的数值太大,所以不能直接筛[0,R]的,要空间和时间优化,用到区间筛法,另外注意不能用int,因为R和L都是满int的,中间有很多细节处理会爆int的,还要注意1不是素数,所以在区间筛中要特判一下,是个易错的地方 //1160K 16MS C++ 1539B #include<cstdio> #include<ios

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

07:清泉-改(prime+堆)

时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  512000kB 描述 华北电力大学可以抽象为一张有n个点m条边的无向图. 现在所有的边都断了. 修复每条边都有个不同的代价w_i. 求让所有点都能互相到达的最小代价和. 输入 第一行两个正整数 n, m 表示顶点数和边数 接下来m行每行三个正整数 u v w 表示一条边 (u和v是边的端点, w是边权) 输出 输出一行一个正整数表示答案 样例输入 2 2 1 2 2 2 1 3 样例输出 2 提示 n ≤ 10^

Sum of Consecutive Prime Numbers POJ - 2739

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege