HDU 4548 美素数 素数题解

本题就是能够直接打表的,推断能否够打表也须要技巧的:

1 推断最大的数值为1000000。百万下面的数打表都是能够的

2 能够线性预处理好。使用素数筛子法是能够接近线性预处理的。

故此能够打表了。

须要熟悉的基本知识点:

1 素数筛子法 - 一两分钟之内写出代码

2 一般素数推断法,由于位数相加之后的数值很小,故此一般素数推断就能够了,假设写个primality test 算法会大材小用了。

3 然后是带点动态规划法的思想把前面的美素数叠加起来,方便查找。

算是基础题目了,也是有人说的水题,我还是喜欢叫基础题吧。

难在于推断好,并运用好这些基础知识。简单优雅地解决,写出代码。

#include <stdio.h>
#include <string.h>

const int MAX_N = 1000001;
bool isPrime(int n)
{
	if (n == 2) return true;
	for (int r = 2; r * r <= n; r++)
	{
		if (n % r == 0) return false;
	}
	return true;
}

bool isMeiPrime(int n)
{
	int d = 0;
	while (n)
	{
		d += n % 10;
		n /= 10;
	}
	return isPrime(d);
}

int primeNums[MAX_N];
bool primes[MAX_N];

void seive()
{
	memset(primes, 0, MAX_N * sizeof(bool));
	for (int i = 2; i < MAX_N; i++)
	{
		if (!primes[i])
		{
			for (int j = i << 1; j < MAX_N; j += i)
			{
				primes[j] = true;
			}
		}
	}

	primeNums[0] = 0, primeNums[1] = 0;
	for (int i = 2; i < MAX_N; i++)
	{
		if (!primes[i] && isMeiPrime(i)) primeNums[i] = primeNums[i-1] + 1;
		else primeNums[i] = primeNums[i-1];
	}
}

int main()
{
	seive();
	int T, a, b;
	scanf("%d", &T);
	for (int t = 1; t <= T; t++)
	{
		scanf("%d %d", &a, &b);
		printf("Case #%d: %d\n", t, primeNums[b] - primeNums[a-1]);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/zhchoutai/p/8932996.html

时间: 2024-08-01 12:14:17

HDU 4548 美素数 素数题解的相关文章

HDU 4548——美素数

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4548 题解 #include<cstring> #include<iostream> using namespace std; const int n=1000005; int prime[1000005]; bool vis[1000005]; int cnt[1000005]; //记录到数字 j为止的美素数个数 void oula(){ //通过欧拉筛打表 int cnt=0; me

HDU 4548.美素数 解题心得

原题: Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000). 接下来共T行,每行输入两个整数L,R(1<= L <= R <

hdu 4548 筛法求素数 打表

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数. 给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据

HDU 1251 统计难题 Trie题解

基本上是标准的寻找前缀的问题,只需要insert和search函数就可以了. 我这里主要是修改一下n的记录方法,这里的n代表的不是叶子节点的标志,而是有多少单词经过了这条路径的标志. 然后是查找需要查找的前缀单词,如果没有找到,就返回0,表示没有单词以这个前缀单词为前缀,如果找到,直接返回n就是答案了.因为有n个单词经过了这条路径. 查找效率是常数. 使用静态分配空间的办法. #include <stdio.h> #include <string.h> const int MAX_

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

HDU 3791 二叉搜索树 题解

Problem Description 判断两序列是否为同一二叉搜索树序列 Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树. 接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树. Output 如果序列相同则输出YES,否则输出NO Sample Input 2 567432 543267

HDU 3049 Data Processing 数论题解

Problem Description Chinachen is a football fanatic, and his favorite football club is Juventus fc. In order to buy a ticket of Juv, he finds a part-time job in Professor Qu's lab. And now, Chinachen have received an arduous task--Data Processing. Th

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU 2824 The Euler function 题解

求区间的euler数值,自然使用筛子法了. Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful character