当前位置:网站首页>Palindromic Primes
Palindromic Primes
2022-04-23 06:46:00 【Round moon】
Palindromic Primes Category in Jeopardy!
Problem description
Prime numbers are defined as follows: a number is prime if it is greater than 1 and is evenly divisible only by itself and 1. Note that by definition neither zero nor one is a prime number.
A palindromic number is one whose string representation is a palindrome, that is, a string that reads the same backwards and forwards.
You are on the clue crew preparing questions for the category “Palindromic Primes” and are to write a program to generate the answer and responding question in Jeopardy! style.
Input
The input file contains a series of number pairs (with white space separating them) specifying individual problems, ending with a pair of zeroes. The first number gives the number of digits for the numbers to be considered, the second number gives the base in which the numbers are to be generated. The numbers are separated by a single space. You are assured that all palindromic primes for this problem can be represented in the range of a standard 32-bit signed integer. The bases allowed are integer bases between 2 and 36 — with bases above base ten handled as extensions of hexadecimal. This means that the valid numeric digits are in the range [‘0’…‘9’] and [‘a’…‘z’].
Output
For each number, generate one line giving the number of digits and the base as the answer and then on the next line the number of palindromic primes found as the question as shown in the sample output. Each output pair should be separated by a blank line.
Sample Input
1 10
2 10
3 10
4 24
5 4
0 0
Sample Output
The number of 1-digit palindromic primes < 2^31 in base 10.
What is 4?
The number of 2-digit palindromic primes < 2^31 in base 10.
What is 1?
The number of 3-digit palindromic primes < 2^31 in base 10.
What is 15?
The number of 4-digit palindromic primes < 2^31 in base 24.
What is 0?
The number of 5-digit palindromic primes < 2^31 in base 4.
What is 10?
The main idea of the topic
Defines a special number called palindrome prime .
such as 11 Is a palindrome prime . But this question is not so easy to give up and toss you .
He also defined a kind of palindrome prime in different base systems . This base number can be up to 36 Base number .
such as ZXZ It's a palindrome number . This number is actually only Three place , Never lean on the decimal system .
He also specifies the length of the palindrome prime !
Thinking analysis
Then let's talk about the idea of solving problems .
We can construct half of the palindrome number , The other half naturally knows .
such as 12321 We can construct 123 The other half is 21
Because this number is increasing . Neither less than nor greater than is the range to be . Only one interval is valid .
Let's just run out of his possession with violence , Of course, if more than 1e31 Quit naturally .
The second step , We need to convert the constructed palindrome number into decimal system and use prime detection method , In fact, that is Miller Robin Algorithm
Miller Robin's general operation is , Use Fermat theorem to verify whether it is a prime number , The probability of each error is about 0.25 about , Repeated detection can minimize the error probability .
Accepted Code
//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a > 9)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define read read()
ll qmul(ll x, ll y, ll mod) {
return (x * y - (long long)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
bool Miller_Rabin(ll p) {
ll prime[] = {
2, 3, 5, 233, 331 };
if (p < 2 || p != 2 && p % 2 == 0) return false;
ll s = p - 1;
while (!(s & 1)) s >>= 1;
for (int i = 0; i < 5; i++) {
if (p == prime[i]) return true;
ll t = s, m = qpow(prime[i], s, p);
while (t != p - 1 && m != 1 && m != p - 1)
m = qmul(m, m, p), t <<= 1;
if (m != p - 1 && !(t & 1)) return false;
}
return true;
}
char mark[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char now[100];
int n, m;
bool sw;
bool can;
bool sw2;
void change(int num)
{
int base = m;
char temp[100];
int cnt = 0;
while (num)
{
temp[cnt++] = mark[num % base];
num /= base;
}
if ((n & 1))
{
if (cnt * 2 - 1 != n)
{
if (can)sw2 = false;
sw = false;
return;
}
}
else
{
if (cnt * 2 != n)
{
if (can)sw2 = false;
sw = false;
return;
}
}
can = true;
for (int i = 0; i < cnt; i++)
now[i] = temp[cnt - 1 - i];
if (cnt * 2 == n)
{
int j = cnt;
for (int i = 0; i < cnt; i++, j++)
now[j] = temp[i];
now[j] = 0;
}
else
{
int j = cnt;
for (int i = 1; i < cnt; i++, j++)
now[j] = temp[i];
now[j] = 0;
}
}
int main()
{
int CNT = 0;
while (scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0)break;
if (CNT++)puts("");
int res = 0;
can = false;
sw2 = true;
for (int i = 1; sw2 && i <= 100000000; i++)
{
sw = true;
change(i);
if (sw)
{
ll val = 0;
for (int j = 0; j < n; j++)
{
char t = now[j];
if (t <= '9')t -= '0';
else t = t - 'A' + 10;
val += ll(t) * qpow(m, j, ll_inf);
}
if (val < (1ll << 31) && Miller_Rabin(val))res++;
}
}
printf("The number of %d-digit palindromic primes < 2^31 in base %d.\n", n, m);
printf("What is %d?\n", res);
}
}
By-Round Moon
版权声明
本文为[Round moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230549499209.html
边栏推荐
- [ThreadX] ThreadX source code reading plan (I)
- 带默认模板实参的类模板与模板模板形参的匹配
- Notes on advanced points of C language 4
- Matching between class template with default template argument and template parameter
- 【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
- 进程管理命令
- 【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (2)
- [stepping on the pit] MELD in win11 wsl2 cannot be used normally. Problem repair
- Eigen 学习总结
- C语言中volatile的使用
猜你喜欢
HDU-Tunnel Warfare
[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
FOC 单电阻采样 位置环控制伺服电机
深蓝学院激光slam理论与实践 -第二章(里程计标定)作业
Qt 添加QSerialPort类 实现串口操作
[UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer
cv_bridge 与opencv 版本不匹配的解决
C语言的浪漫
PHP junior programmers, take orders and earn extra money
Qt 给应用程序加图标
随机推荐
CUDA environment installation
PN结、二极管原理详解与应用
【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
Figure guessing game
Incremental update of client software
在MFC中使用printf
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
Camera calibration: key point method vs direct method
软件工程中的十三种文档
赛氪-zeal
Static member
TP download folder, compress folder and download
基于VGG对五种类别图片的迁移学习
猜數字遊戲
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
Qt 添加QSerialPort类 实现串口操作
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
2022LDU寒假训练-程序补丁
HDU-Memory Control
文件查看命令和用户管理命令