UVA 10539 Almost Prime Numbers( 素数因子)

Problem A
Almost Prime Numbers
Time 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 within a certain range.

Input
First line of the input file contains an integer N (N <= 600) which indicates how many sets of inputs are there. Each of the next N lines make a single set of input. Each set contains two integer numbers low and high (0 < low <= high < 1012).

Output
For each line of input except the first line you should produce one line of output. This line contains a single integer, which indicates how many almost prime numbers are within the range (inclusive) low…high.

Sample Input Sample Output
3
1 10
1 20
1 5
3
4
1


Problemsetter: Shahriar Manzoor, special thanks to Derek Kisman

题目大意:

  这道题说的是,给你一个[l,r],然后让你在这个[l,r]中,找到仅仅含有一个素数因子并且自己不是素数的数的个数。

解题思路:

  一看题,就知道是数学题,那么我们上来后先打表就是了。

  首先,我们先看看第一组样例[1,10]有3个,那么答案就是4,8,9这三个了?为什么是这三个数字呢?首先4=2*2 = 2^2.仅含有一个素数因子,这个因子是2.

  然后是8 = 2*4 = 2^3也含有一个素数因子,这个因子是2,然后是 9 = 3*3 = 3^2,含有素数因子3。

  不难发现,要想找到自己不是素数,并且仅仅含有一个素因子的数的等价于在这个区间中去寻找是否存在这样的数字val使得对于任意的一个素数p有val = p^i;(i>=2)

  当然,val的值肯定是介于l和r之间的。

  在最初的素数打表的过程中,我们只需要计算出p^2<=10^12   -> p<=10^6.

  所以,只需要计算出[1,10^6]的素数就好了,然后我们从pime[i]*prime[i]开始枚举,直到其数值超过了r为止。。。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<cstring>
 4
 5 using namespace std;
 6
 7 typedef long long LL;
 8
 9 # define MAX 1000004
10
11 LL prime[MAX];
12 LL book[MAX];
13 LL len;
14
15 void init()
16 {
17     for ( int i = 2;i <= MAX;i++ )
18     {
19         book[i] = 1;
20     }
21     for ( int i = 2;i <= MAX;i++ )
22     {
23         if ( book[i]==1 )
24         {
25             for ( int j = 2*i;j <= MAX;j+=i )
26             {
27                 book[j] = 0;
28             }
29         }
30     }
31     for ( int i = 2;i <= MAX;i++ )
32     {
33         if ( book[i]==1 )
34         {
35             prime[len++] = i;
36         }
37     }
38
39 }
40
41
42 int main(void)
43 {
44     init();
45     int t;cin>>t;
46     while ( t-- )
47     {
48         LL tot = 0;
49         LL l,r;
50         cin>>l>>r;
51         for ( int i = 0;i < len;i++ )
52         {
53             for ( LL j = prime[i]*prime[i];j <= r;j*=prime[i] )
54             {
55                 if ( j >= l )
56                     {
57                        // cout<<j<<endl;
58                         tot++;
59                     }
60             }
61
62         }
63         cout<<tot<<endl;
64
65     }
66
67     return 0;
68 }
时间: 2024-10-25 07:06:02

UVA 10539 Almost Prime Numbers( 素数因子)的相关文章

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

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 1415 - Gauss Prime(高斯素数)

题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=?2 ̄ ̄ ̄√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != 0时,推断a2+2b2是否为素数就可以. #include <cstdio> #include <cstring> #include <cmath> bool is_prime (int n) { int m = sqrt(n+0.5); for (int i = 2;

CodeForces 385C Bear and Prime Numbers 素数打表

第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

Codeforces 385C Bear and Prime Numbers(素数预处理)

Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出现个数时使用了map,以至于后面做前缀和的累加时,每次都要对map进行查询,以至于TLE.而自己一直没有发现,以为是欧拉筛对于这道题还不够优,于是上网搜题解,发现别人的做法几乎一样,但是却能跑过,挣扎了许久才想起是map的原因.map的内部实现是一颗红黑树,每次查询的复杂度为O(logN),在本来时

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p