uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers

题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数).

解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数。枚举素数,判断即可。

#include <cstdio>
#include <cstring>

typedef long long ll;

const int maxn = 1e6;
ll lower, up, prime[maxn];
int cp, v[maxn];

void primeTable (int n) {

    cp = 0;
    memset(v, 0, sizeof(v));

    for (int i = 2; i <= n; i++) {
        if (v[i])
            continue;

        prime[cp++] = i;
        for (int j = 2 * i; j <= n; j += i)
            v[j] = 1;
    }
}

ll solve (ll n) {
    ll ans = 0;

    for (int i = 0; i < cp; i++) {
        ll u = prime[i] * prime[i];

        if (u > n)
            break;

        while (u <= n) {
            u *= prime[i];
            ans++;
        }
    }
    return ans;
}

int main () {
    primeTable(maxn);
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%lld%lld", &lower, &up);
        printf("%lld\n", solve(up) - solve(lower-1));
    }
    return 0;
}

uva 10539 - Almost Prime Numbers(数论),布布扣,bubuko.com

时间: 2024-12-25 10:44:08

uva 10539 - Almost Prime Numbers(数论)的相关文章

UVA 10539 Almost Prime Numbers( 素数因子)

Problem AAlmost Prime NumbersTime Limit: 1 second Almost prime numbers are the non-prime numbers which are divisible by only a single prime number. In this problem your job is to write a program which finds out the number of almost prime numbers with

UVA - 10539 Almost Prime Numbers (几乎是素数)

题意:输入两个正整数L.U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子. 分析: 1.满足条件的数是素数的倍数. 2.枚举所有的素数,以及其倍数,将满足条件且小于等于n的个数计算出来,solve(u) - solve(l - 1)即可. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cst

uva 1530 - Floating Point Numbers(数论)

题目链接:uva 1530 - Floating Point Numbers 题目大意:给出一个16位的二进制数,用来表示一个浮点数,第一位为符号,1~7位表示一个十进制的数s,e=63-s;剩下的8位为小数部分,默认整数部分为1,得到f,然后最后a=f*2^e,要求用科学计数法输出a. 解题思路:模拟就好了,注意0的情况特殊处理,以及科学计数法的整数部分不能为0. #include <cstdio> #include <cstring> #include <cmath>

UVA 11582 Colossal Fibonacci Numbers!(数论)

 题意:输 入两个非负整数a.b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,其中f(0) = 0,f(1) =  1:且对任意非负整数i,f(i+2)= f(i+1)+f(i). 思路:因为斐波那契序列要对n取模,余数只有n种,所以最多n^2项序列就开始重复,所以问题转化成了求周期然后大整数取模. #include<cstdio> #include<cstring> #include<cmath&

UVA 1210 Sum of Consecutive Prime Numbers(数论)

UVA - 1210 Sum of Consecutive Prime Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such

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】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的. 14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11 打表的话效率可能不是很高. AC代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&

uva 1404 - Prime k-tuple(数论)

题目链接:uva 1404 1404 - Prime k-tuple 题目大意:如果k个相邻的素数p1,p2,-,pk,满足pk?p1=s,称这些素数组成一个距离为s的素数k元组,给定区间a,b,求有多少个距离s的k元组. 解题思路:筛选素数法,先预处理出[1, sqrt(inf)]的素数表,然后对给定区间[a,b]根据预处理出的素数表筛选出素数即可. #include <cstdio> #include <cstring> #include <cmath> #incl

UVA 1415 - Gauss Prime(数论,高斯素数拓展)

UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或b为0.推断还有一个数为4 * n + 3形式的素数(用到费马平方和定理) 2.假设a.b都不为0,推断a ^ 2 + b ^ 2 是否为素数 那么这题,提取出sqrt(2)来,就和基本情况一样了. 对于2,变成: 假设a.b都不为0,推断a ^ 2 + 2 b ^ 2是否为素数 对于1.事实上仅仅