Uva10533 - Digit Primes
2014-07-26 22:25
288 查看
A prime number is a positive number, which is divisible by exactly two different integers. A digit prime is a prime number whose sum of digits is also prime. For example the prime number 41 is
a digit prime because 4+1=5 and 5 is a prime number. 17 is not a digit prime because 1+7 = 8, and 8 is not a prime number. In this problem your job is to find out the number
of digit primes within a certain range less than 1000000.
Input
First line of the input file contains a single integer N (0<N<=500000) that indicates the total number of inputs. Each of the next N lines contains two integers t1 and t2 (0<t1<=t2<1000000).
Output
10 20
10 100
100 10000
10
576
a digit prime because 4+1=5 and 5 is a prime number. 17 is not a digit prime because 1+7 = 8, and 8 is not a prime number. In this problem your job is to find out the number
of digit primes within a certain range less than 1000000.
Input
First line of the input file contains a single integer N (0<N<=500000) that indicates the total number of inputs. Each of the next N lines contains two integers t1 and t2 (0<t1<=t2<1000000).
Output
For each line of input except the first line produce one line of output containing a single integer that indicates the number of digit primes betweent1 and t2 (inclusive).
Sample Input
310 20
10 100
100 10000
Sample Output
110
576
#include <cstdio> #include <cstring> bool u[1000010]; int su[500010]; int uu[1000010]; bool ok(int x){ int t = 0; while(x){ t += x % 10; x /= 10; } return u[t]; } int main(){ int i, j, k, n, num = 0; memset(u, true, sizeof(u)); for(i = 2; i < 1000000; i++){ if(u[i]) su[num++] = i; for(j = 0; j < num; j++){ if( i * su[j] > 1000000) break; u[ i*su[j] ] = false; if(i % su[j] == 0) break; } } memset(uu, 0, sizeof(uu)); for(i = 2; i < 1000000; i++) if(u[i] && ok(i)) uu[i] = 1; for(i = 2; i < 1000000; i++) uu[i] += uu[i-1]; scanf("%d", &k); while(k--){ scanf("%d %d", &i, &j); printf("%d\n", uu[j] - uu[i-1]); } return 0; }
相关文章推荐
- UVA10533 Digit Primes【筛选法+前缀和】
- UVa 10533 - Digit Primes
- (数论2.1.3)UVA 10533 Digit Primes(埃拉托斯特尼筛法)
- Uva 10533 - Digit Primes
- UVa 10533 - Digit Primes
- UVA-1583 - Digit Generator
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,费马小定理和欧拉函数判素数。
- UVa 1225 Digit Counting
- 生成元(Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
- UVa 1583-Digit Generator-AC
- UVA_Digit Puzzle UVA 12107
- UVa 1225 / UVALive 3996 Digit Counting 数数字(字符统计)
- uva 1225 Digit Counting
- 【紫书】例题3-5 生成元(Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
- UVA 1213 Sum of Different Primes
- UVa1225 DIgit Counting
- UVa 10168 - Summation of Four Primes
- [Digit Generator, ACM/ICPC Seoul 2005, UVA1583]
- UVa 1225 Digit Counting
- UVa 1225(Digit Counting)