程序员必知:【UVA10533】DigitPrimes

简介: 程序员必知:【UVA10533】DigitPrimes

  本题数字本身是素数,并且各数位和也是素数,这样的数字在某区间内的个数。

  先素数筛,再筛出各数位和是奇数的数,接着统计1-a的时候所有符合条件的数字的个数,求特定区间符合条件的数字做差即可。

  代码如下:

1 #include

2 #include

3 #include

4 #include

5

6 using namespace std;

7

8 const int maxn = 1000010;

9 bool prime【maxn】;

10 int satis【maxn】;

11 int a, b;

12

13 void printlist() {

14 memset(prime, true, sizeof(prime));

15 prime【0】 = prime【1】 = false;

16 int pedge = int(sqrt(maxn));

17 for(int i = 2; i <= pedge; i++) {

18 if(//代码效果参考:http://www.zidongmutanji.com/zsjx/346165.html

prime【i】) {

19 int o = maxn / i;

20 for(int j = 2; j <= o; j++) {

21 prime【i*j】 = false;

22 }

23 }

24 }

25 }

26

27 bool judge(int x) {

28 int ans = 0;

29 while(x) {

30 ans += x % 10;

31 x /= 10;

32 }

33 return prime【ans】;

34 }

35

36 void init() {

37 printlist();

38 memset(satis, 0, sizeof(satis));

39 for(int i = 2; i <= maxn; i++) {

40 if(judge(i) prime【i】) {

41 satis【i】 = 1;

42 }

43 }

44 for(int i = 2; i <= maxn; i++) {

45 satis【i】 += satis【i-1】;

46 }

47 }

48

49 int main() {

50 init();

51 int T;

52 scanf("%d", T);

53 while(T--) {

54 scanf("%d %d", a, b);

55 cout [ satis【b】 [ " " [ satis【a-1】 [ endl;

56 printf("%d\n",satis【b】-satis【a-1】);

57 }

58 return 0;

59 }

相关文章
|
2天前
|
程序员
程序员必知:【UVA10533】DigitPrimes
程序员必知:【UVA10533】DigitPrimes
|
1天前
|
机器学习/深度学习 算法 C++
技术笔记:UVA322ships(POJ1138)
技术笔记:UVA322ships(POJ1138)
|
2天前
|
人工智能 Java 程序员
程序员必知:uva10808
程序员必知:uva10808
|
1天前
|
BI
技术笔记:UVA11174StandinaLine
技术笔记:UVA11174StandinaLine
|
2天前
技术好文:UVa414
技术好文:UVa414
|
30天前
|
Java
HDU-1286-找新朋友
HDU-1286-找新朋友
19 0
|
6月前
|
存储 人工智能 算法
C语言 每日一题 PTA 10.25 day4
C语言 每日一题 PTA 10.25 day4
31 0
|
6月前
|
C语言
C语言 每日一题 PTA 10.30 day8
C语言 每日一题 PTA 10.30 day8
35 0
|
6月前
|
C语言
C语言 每日一题 PTA 10.27 day5
C语言 每日一题 PTA 10.27 day5
56 0
|
7月前
|
Java
hdu 1286 找新朋友
hdu 1286 找新朋友
22 0