UVA 10200 Prime Time

题意:给你两个数a,b,求[a,b]内有多少个数满足f(n)=n*n+n+41是素数。

题解:预处理除前缀和,直接求

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define N 10010

using namespace std;
int n;
int s[N];

bool ok(int x) {
    if(x==1||x==0)return false;
    for(int i=2; i*i<=x; i++) {
        if(x%i==0)return false;
    }
    return true;
}

void init() {
    for(int i=0; i<N; i++) {
        int k=i*i+i+41;
        if(ok(k))s[i]=1;
        else  s[i]=0;
    }
    for(int i=1; i<N; i++)s[i]+=s[i-1];
}
int main() {
    //freopen("test.in","r",stdin);
    init();
    int a,b;
    while(~scanf("%d%d",&a,&b)) {
        int x=b-a+1;
        int y=s[b];
        if(a!=0)
            y-=s[a-1];
        double ans=y*100.0/(x*1.0)+1e-5;//精度
        printf("%.2f\n",ans);
    }
    return 0;
}
时间: 2024-11-14 16:31:49

UVA 10200 Prime Time的相关文章

UVA 10200 Prime Time 水

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意:判断区间[a,b]的f(i)是否为素数,f(i)=i*i+i+40: 思路:打个表,然后注意精度: #include<bits/stdc++.h> using namespace std; #define ll long long #define esp

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

UVA 10200 Prime Time(简单素数判定预处理)

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

UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <

UVA 10200 记忆打表,素数筛,浮点误差

UVA 10200 区间预处理,浮点误差 W - Prime Time Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10200 Description 题意:测试找素数函数f(n)=n^2+n+41在区间n<-[a,b]时,找到素数的成功率. 思路:区间的素数个数,打表预处理即可.输出答案时四舍五入,需注意浮点误差,四舍五入时加上EPS. #

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

UVa 524 - Prime Ring Problem

题目:把1-n,连续的放到一个环里,使相邻的数字和为素数,输出所有结果. 分析:搜索+剪枝.如果裸搜,用dancing-links那种拆装的链表,应该差不多满足16的数据量. 这里利用一个性质进行剪枝:相邻的数字一定是奇偶性不同的数字. (如果上述假设不成立,则存在相邻的奇数或偶数,那么他们的和一定是大于2的偶数,不是素数) 根据上面的假设,还有一条推论:只有n为偶数时才有解. (n为奇数,一直至少有一对奇数相邻,同上,矛盾(鸽巢原理)) 因此,每次搜索的数据其实是n/2,时间复杂度为O((n/

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number