UVa 10533 - Digit Primes

题目:输出给定区间中,本身是素数,并且这个数的各位之和也是素数的数(称为位素数)的个数。

分析:数论。首先利用筛法,求出1000000内的所有的素数;然后在利用生成的素数表,

判断每个数是不是各位之和也是素数;再后求出从0开始到任意区间中包含位素数数的个数;

最后输出两个区间之差就是区间中的位素数的个数。

说明:达标法计算,查询输出。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int prime[1000002];
int visit[1000002];

int bits( int k )
{
	int bit = 0;
	while ( k ) {
		bit += k%10;
		k /= 10;
	}
	return bit;
}

int main()
{
	memset( visit, 0, sizeof(visit) );
	memset( prime, 0, sizeof(prime) );
	prime[0] = 0; prime[1] = 0;
	for ( int i = 2 ; i < 1000000 ; ++ i )
		if ( !visit[i] ) {
			for ( int j = i*2 ; j < 1000000 ; j += i )
				visit[j] = 1;
			if ( !visit[bits(i)] )
				prime[i] = prime[i-1]+1;
			else prime[i] = prime[i-1];
		}else prime[i] = prime[i-1];

	int n,a,b;
	while ( ~scanf("%d",&n) ) {
		for ( int i = 0 ; i < n ; ++ i ) {
			scanf("%d%d",&a,&b);
			printf("%d\n",prime[b]-prime[a-1]);
		}
	}
	return 0;
}

UVa 10533 - Digit Primes

时间: 2025-01-13 08:11:37

UVa 10533 - Digit Primes的相关文章

UVa 1583 Digit Generator(数)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 

UVa 1225 Digit Counting --- 水题

UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现的次数,先将cnt初始化为0,接着让i从1枚举到n, 对每个i,处理以活的i的每一个位置上的数,并在相应的cnt下标上+1 最后输出cnt数组即可 /* UVa 1225 Digit Counting --- 水题 */ #include <cstdio> #include <cstring

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

UVA 1225 Digit Counting(统计数位出现的次数)

Digit Counting Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu SubmitStatus Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting

2 UVA 10394 Twin Primes

直接筛素数预处理. #include<cstdio> #include<cstring> #include<vector> using namespace std; const int maxn=20000000; bool pri[20000005]; vector<int> v; void init() { int i,j; memset(pri,1,sizeof pri); pri[0]=pri[1]=0; for(i=2;i<=(maxn>

UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告

1.题目大意 把前n$n\le 10000$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更好的办法了,写完看了一下其它人的思路好像也差不多是打表的思路. 3.应注意的问题 (1)首先是格式问题,我第一次提交的时候PE了,因为没有意识到空格也会有影响.最开始我的最后一段代码是: for(i=0;i<10;i++) printf("%d ",s[n][i]); printf(&q

UVa 1583 Digit Generator(数学)

 题意 如果a加上a所有数位上的数等于b时 a称为b的generator  求给定数的最小generator 给的数n是小于100,000的  考虑到所有数位和最大的数99,999的数位和也才45  因此我们只需要从n-45到n枚举就行了 #include<cstdio> #include<cstring> using namespace std; int t, n, a, b, ans, l; int main() { scanf ("%d", &

UVa 1587 - Digit Generator

A+A的每一位的数字的和=B 问你每一个B对应 的最小的A 是多少 不然输出0: 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 using namespace std; 5 const int N=100005; 6 int ans[2*N],tmp,cnt,n,t; 7 void fuc(){ 8 memset(ans,0,sizeof(ans)); 9 for(int i=1;i&l

UVa 1225 - Digit Counting

题目:输出1~N所有数字中,0~9出现的总次数. 分析:简单题.打表计算,查询输出即可. 说明:最近事情好多啊╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int f[10000][10]; in