UVa 10539 - Almost Prime Numbers

求low~high之间有多少个质数分解后由一个素数组成,将10^6内的素数全部打出来,然后枚举每个素数的n(n > 2 )次幂在low和high之间个数的和。

/*************************************************************************
    > File Name: 10539.cpp
    > Author: Toy
    > Mail: ycsgldy@163.com
    > Created Time: 2013年06月09日 星期日 20时27分49秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
const int INF = 0x7fffffff;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<i64,i64> II;
typedef vector<i64> IV;
typedef vector<II> IIV;
typedef vector<bool> BV;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
const int MAXP = 1000000;
const int SQRP = 1000; //sqrt(MAX)
int _c[(MAXP>>6)+1];
IV primes;
int Case;
i64 low, high;

void prime_sieve() {
	for ( int i = 3; i <= SQRP; i += 2 )
		if ( !IsComp(i) ) for ( int j = i*i; j <= MAXP; j+=i+i ) SetComp(j);
	primes.push_back ( 2 );
	for ( int i = 3; i <= MAXP; i += 2 ) if ( !IsComp(i) ) primes.push_back ( i );
}

int main ( ) {
	prime_sieve();
	scanf ( "%d", &Case );
	while ( Case-- ) {
        i64 ans = 0;
        scanf ( "%lld%lld", &low, &high );
        For ( IV, it, primes ) {
            i64 tmp = *it;
            tmp *= *it;
            while ( tmp < low ) tmp *= *it;
            while ( tmp >= low && tmp <= high ) {
                ans++;
                tmp *= *it;
            }
            if ( *it > high ) break;
        }
        printf ( "%lld\n", ans );
	}
    return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值