UVA-10539 打表+二分

题意非常简单,就是给你一个区间(闭区间),然后让你统计区间内有多少数满足本身不是素数,但只有一个素因子

首先注意题目中区间左右端点最大可以取到1e12,这早就超越了int的表示范围

我们首先打表计算出1e6内的素数表,然后计算所有满足要求的数,存进数组,最后排序

然后对于给定的区间[L,R],我们用二分找到上界和下届

二分一直是我心中的痛,总是用不好,以后碰一道二分记录一道

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<assert.h>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
#pragma warning(disable:4996)
#define me(s)  memset(s,0,sizeof(s))
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
typedef unsigned long long llu;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
const int maxn = 1000000;
ll p[maxn + 5], vis[maxn + 5], pcnt;
vector<ll> ans;
void init()
{
	me(vis);
	pcnt = 0;
	for (int i = 2; i<maxn; i++) {
		if (!vis[i]) {
			p[pcnt++] = i;
			for (int j = 2; i*j < maxn; j++) vis[i*j] = true;
		}
	}
	for (int i = 0; i<pcnt; i++) {
		ll tmp = p[i] * p[i];//刚开始没用p数组没用ll坑死
		while (tmp < 1000000000000LL) {
			ans.push_back(tmp);
			tmp *= p[i];
		}
	}
	sort(ans.begin(), ans.end());
}
ll L, R;
int main()
{
	init();
	int T; scanf("%d", &T);
	while (T--) {
		scanf("%lld%lld", &L, &R);
		cout << lower_bound(ans.begin(), ans.end(), R+1) - lower_bound(ans.begin(), ans.end(), L) << endl;
	}
}

  

原文地址:https://www.cnblogs.com/033000-/p/9912388.html

时间: 2024-08-04 02:40:15

UVA-10539 打表+二分的相关文章

uva 1356 - Bridge(积分+二分)

题目链接:uva 1356 - Bridge 题目大意:在一座长度为B的桥上建若干个塔,塔的间距不能超过D,塔的高度为H,塔之间的绳索形成全等的抛物线.绳索的总长度为L.问在建最少塔的情况下,绳索的最下段离地面的高度. 解题思路:贪心的思想求出最少情况下建立的塔数. 二分高度,然后用积分求出两塔之间绳索的长度. C++ 积分 #include <cstdio> #include <cstring> #include <cmath> #include <algori

[HDOJ6154] CaoHaha&#39;s staff(规律, 打表, 二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 有些题一辈子只会做一次,比如这个题.. 题意:炒鸡难懂,学弟读明白的.懒得描述,反正这题以后不会再做. f(i)表示i个线段能围成的最大面积,画画图就会发现一个规律. 然后查询的时候二分最小的大于等于s的即可. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛

HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛

题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #include <math.h> #include <stdio.h> #include<algorithm> using namespace std; long long data[1000000],tot=0; int main() { long long maxn = 10

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

HDU2098分拆素数和【打素数表+二分】

大素数表+二分 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 const int maxn = 10005; 8 int prm[maxn / 3 + 1], is[maxn / 32 + 1]; 9 int n; 10 int k; 11 void get() { 12 int N =

UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 typedef long long LL; 6 7 const int maxn = 10

[LeetCode] #1# Two Sum : 数组/哈希表/二分查找

一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solutio

2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 406 Problem Description I will show you the most popular board game in the Shanghai Ingress Resis

XTU 1185 暴力打表+二分

题目连接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1185 Bob's Problem Accepted : 105   Submit : 550 Time Limit : 1000 MS   Memory Limit : 65536 KB 题目描述 Bob今天碰到一个问题,他想知道x3+y3 = c 是否存在正整数解? 输入 第一行是一个整数K(K≤20000),表示样例的个数. 以后每行一个整数c(2≤c≤10

poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】

Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It i