• [USACO1.5] 回文质数


     P1217  Prime Palindromes 

    题目描述

    因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。

    写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数;

     

    输入格式:

    第 1 行: 二个整数 a 和 b .

    输出格式: 

    输出一个回文质数的列表,一行一个。

    思路:枚举一半长度的整数即可。

    代码:

     1 #include"bits/stdc++.h"
     2 #define ll long long
     3 #define ci(x) scanf("%d",&x)
     4 #define cd(x) scanf("%lf",&x)
     5 #define cl(x) scanf("%lld",&x)
     6 #define pi(x) printf("%d
    ",x)
     7 #define pd(x) printf("%f
    ",x)
     8 #define pl(x) printf("%lld
    ",x)
     9 using namespace std;
    10 const int N   = 1e6 + 5;
    11 const int mod = 1e9 + 7;
    12 ll a[N],b[N];
    13 
    14 ll mod_mul(ll a, ll b, ll mod) {
    15     ll ret = 0;
    16     while(b) {
    17         if(b & 1)  ret = (ret + a) % mod;
    18         a = (a + a) % mod;
    19         b >>= 1;
    20     }
    21     return ret;
    22 }
    23 
    24 ll mod_exp(ll a, ll b, ll mod) {
    25     ll ret = 1;
    26     while(b) {
    27         if(b & 1) ret = mod_mul(ret, a, mod);
    28         a = mod_mul(a, a, mod);
    29         b >>= 1;
    30     }
    31     return ret;
    32 }
    33 
    34 bool check(ll a, ll n) {
    35     ll x = n - 1, y;
    36     int t = 0;
    37     while((x & 1) == 0) {
    38         x >>= 1;
    39         t++;
    40     }
    41     x = mod_exp(a, x, n);
    42 
    43     for(int i = 1; i <= t; i++) {
    44         y = mod_exp(x, 2, n);
    45         if(y == 1 && x != 1 && x != n - 1) return true;
    46         x = y;
    47     }
    48     if(y != 1) return true;
    49     return false;
    50 }
    51 
    52 bool mill(ll n, int times = 10) {
    53     if(n == 2) return true;
    54     if(n == 1 || !(n & 1)) return false;
    55 
    56     for(int i = 1; i <= times; i++) {
    57         ll a = rand() % (n - 1) + 1;
    58         if(check(a, n)) return false;
    59     }
    60     return true;
    61 }
    62 int p=1,q=0;
    63 int n;
    64 int l,r;
    65 void init()//预处理出所有的回文质数
    66 {
    67     string f,e;
    68     a[0]=11;
    69     for(ll i=5;i<=10000;i++){
    70         e=to_string(i);
    71         f=e;
    72         reverse(f.begin(),f.end());
    73         e=e+f;
    74         ll x=(ll)atoi(e.data());
    75         if(mill(x)==1) b[q++]=x;
    76 
    77         e=to_string(i);
    78         f=e.substr(0,e.size()-1);
    79         reverse(f.begin(),f.end());
    80         e=e+f;
    81         ll y=(ll)atoi(e.data());
    82         if(mill(y)==1) a[p++]=y;
    83     }
    84     int k=0;
    85     for(int i=p;i<p+q;i++) a[i]=b[k++];
    86     sort(a,a+p+q);
    87     n=p+q;
    88 }
    89 int main ()
    90 {
    91     init();
    92     ios::sync_with_stdio(0);
    93     cin>>l>>r;
    94     for(int i=0;i<n;i++) if(l<=a[i]&&a[i]<=r) pl(a[i]);
    95     return 0;
    96 }
  • 相关阅读:
    变形与动画
    验证状态、禁用
    选择列表和可多选的选择列表
    浏览——选择文件
    引用
    <abbr> 元素的样式为显示在文本底部的一条虚线边框,当鼠标悬停在上面时会显示完整的文本(只要您为 <abbr> title 属性添加了文本)
    bootstrap强调类名
    bootstrap列排序
    jsp变量的使用规则
    随机生成30个四则运算算式
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9038844.html
Copyright © 2020-2023  润新知