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 1e-13
const int N=1e4+10,M=1e6+50000,inf=1e9+10,mod=1000000007;
int prime(int n)
{
    if(n<=1)
    return 0;
    if(n==2)
    return 1;
    if(n%2==0)
    return 0;
    int k, upperBound=n/2;
    for(k=3; k<=upperBound; k+=2)
    {
        upperBound=n/k;
        if(n%k==0)
            return 0;
    }
    return 1;
}
int f(int x)
{
    return x*x+41+x;
}
int flag[N];
int main()
{
    int x,y,i,z,t;
    for(i=0;i<=10000;i++)
    if(prime(f(i)))
    flag[i+1]=flag[i]+1;
    else
    flag[i+1]=flag[i];
    while(~scanf("%d%d",&x,&y))
    {
        double sum=flag[y+1]-flag[x];
        double di=(y-x+1);
        double ans=sum/di*100.0+1e-8;
        printf("%.2f\n",ans);
    }
    return 0;
}
时间: 2024-11-05 17:46:52

UVA 10200 Prime Time 水的相关文章

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 1644 Prime Gap (水题,暴力)

题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&

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 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

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. #

UVa 1586 Molar mass --- 水题

UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现一个从字符型的数到整型的数的转换函数,再将输入的串从头到尾扫描,遇到字母,则进一步扫描后面的数字的区间, 再调用函数进行转换,再乘以其的原子质量,最后累加到sum中即可. /* UVa 1586 Molar mass --- 水题 */ #include <cstdio> #include <

UVa 272 Tex Quotes --- 水题

题目大意:在TeX中,左引号是 ``,右引号是 ''.输入一篇包含双引号的文章,你的任务是把他转成TeX的格式 解题思路:水题,定义一个变量标记是左引号还是右引号即可 /* UVa 272 Tex Quotes --- 水题 */ #include <cstdio> #include <cstring> int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #endi

[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