HOJ 1004 Prime Palindromes(模拟+数学)

传送门

Time limit : 15 sMemory limit : 32 mb

Problem Description

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 <= 1000,000,000); both a and b are considered to be within the range .

Input

Line 1: Two integers, a and b

Output

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

Sample Input

5 500

Sample Output

5
7
11
101
131
151
181
191
313
353
373
383
题目大意:找到a到b区间内所有的回文数和质数,回文数例如:11,131,2442,12321等

心路历程:时间只给15秒,判断两次会超时,可以打表过。上课yogurt学长介绍了另一种方法,先创造回文数,再判断这个回文数是不是素数。

ac代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int a,b;
//创造回文数,可以证明除11以外的偶数位回文数都为合数,所以只需特判11,并构造奇数位回文数
ll get(int val){
    if(val==8) return 11;
    int tmp=val;
    tmp/=10;
    while(tmp){
        val=val*10+tmp%10;
        tmp/=10;
    }
    return val;
}
//检查是不是素数
int check(int val)
{
    if(val%2==0) return 0;
    for(int i=3;i*i<=val;i+=2) if(val%i==0)return 0;
    return 1;
}

int main()
{
    while(~scanf("%d%d",&a,&b)){
        int i;
        for(i=1;i<=100000;i++){
            ll tmp=get(i);
            if(check(tmp)&&tmp>=a&&tmp<=b) printf("%d\n",tmp);
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值