USACO1.5.2--Prime Palindromes

Prime Palindromes

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .

PROGRAM NAME: pprime

INPUT FORMAT

Line 1: Two integers, a and b

SAMPLE INPUT (file pprime.in)

5 500

OUTPUT FORMAT

The list of palindromic primes in numerical order, one per line.

SAMPLE OUTPUT (file pprime.out)

5

7

11

101

131

151

181

191

313

353

373

383

HINTS (use them carefully!)

Prime Palindromes: Hint 1

Generate the palindromes and see if they are prime.

Prime Palindromes: Hint 2

Generate palindromes by combining digits properly. You might need more than one of the loops like below.

/* generate five digit palindrome: */

for (d1 = 1; d1 <= 9; d1+=2) {	/* only odd; evens aren't so prime */

    for (d2 = 0; d2 <= 9; d2++) {

        for (d3 = 0; d3 <= 9; d3++) {

	    palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;

	    ... deal with palindrome ...

	}

    }

}
题解:枚举。可以先生成回文数然后再判断是否是质数。我第一次是用最朴素的方法,一亿果断超时了。生成回文数的时候有个小技巧,也就是Hint 2提到的,因为回文数是对称的,所以我么只要枚举回文数的一半就行,然后再倒转一下就OK了。并且偶数长度的回文数全部能整除11,所以我么只需枚举奇数长度的回文数就可以了,唯一特殊的偶数长度的回文数就是11。这样的话最大的数据枚举量也就是5*9*9*9=3645。比直接枚举一亿,少了好多!!!
View Code
 1 /*

 2 ID:spcjv51

 3 PROG:pprime

 4 LANG:C

 5 */

 6 #include<stdio.h>

 7 #include<math.h>

 8 long f[15];

 9 long a,b;

10 int is_prime(long n)

11 {

12     long i;

13     if(n<2) return 0;

14     for(i=2; i<=sqrt(n); i++)

15         if(n%i==0) return 0;

16     return 1;

17 }

18 int main(void)

19 {

20     freopen("pprime.in","r",stdin);

21     freopen("pprime.out","w",stdout);

22     long i,d1,d2,d3,d4,palindrome;

23     scanf("%ld%ld",&a,&b);

24     for(i=a; i<10; i++)

25         if(is_prime(i)) printf("%ld\n",i);

26     if(a<11&&b>11) printf("%d\n",11);

27     for (d1 = 1; d1 <= 9; d1+=2)

28         for (d2 = 0; d2 <= 9; d2++)

29         {

30             palindrome = 100*d1 + 10*d2 + d1;

31             if(palindrome>=a&&palindrome<=b&&is_prime(palindrome))

32                 printf("%ld\n",palindrome);

33 

34 

35         }

36     for (d1 = 1; d1 <= 9; d1+=2)

37         for (d2 = 0; d2 <= 9; d2++)

38             for(d3=0; d3<=9; d3++)

39             {

40                 palindrome =10000*d1+1000*d2+100*d3 + 10*d2 + d1;

41                 if(palindrome>=a&&palindrome<=b&&is_prime(palindrome))

42                     printf("%ld\n",palindrome);

43 

44             }

45     for (d1 = 1; d1 <= 9; d1+=2)

46         for (d2 = 0; d2 <= 9; d2++)

47             for(d3=0; d3<=9; d3++)

48                 for(d4=0; d4<=9; d4++)

49                 {

50                     palindrome =1000000*d1+100000*d2+10000*d3+1000*d4+100*d3 + 10*d2 + d1;

51                     if(palindrome>=a&&palindrome<=b&&is_prime(palindrome))

52                         printf("%ld\n",palindrome);

53 

54                 }

55     return 0;

56 }

 

你可能感兴趣的:(USACO)