codechef Prime Palindromes 题解

给定一个数,求一个新数要大于等于这个数,而这个新数既要是palindromes回文又要是prime素数。

题目很简单,有人都使用取巧的方法保存好结果直接查表。

或者暴力法求解。

这里不使用保存表的方法,也不要用暴力法。- 这些方法都不好。

使用的技巧有:

1 而是使用next palindrome的技巧,只需要O(n),n是数位,可以认为是常数了。

2 判断素数的方法,时间效率是O(sqrt(n)), n是数值大小,如果是重复判断很多数是否是素数是有办法优化的,但是如果是单个素数判断的话,我还想不到,也找不到优化的方法。

不过怎么都好,下面程序假设它是需要重复判断数是素数的,那么就可以保持一个数组的方法,然后只需要判断这个数是否能被这个数组中小于这个数n的sqrt(n)除尽,就可以快速判断出这个数是否是素数了。

当然本题好像直接判断是否是素数就可以了。

原题:http://www.codechef.com/problems/PRPALIN/

全部实现上面的优化,那么程序还是非常繁琐的,也是出于学习的目的吧。

#include <assert.h>
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

namespace{
	static const int MAX_NUM = 1100;
}

bool allNines(string a)
{
	bool nines = true;
	for (int i = 0; i < (int)a.size() && nines; i++)
	{
		if (a[i] != ‘9‘) nines = false;
	}
	return nines;
}

string nextPalindrome(string a)
{
	if (allNines(a))
	{
		a[0] = ‘1‘;
		for (int i = 1; i < (int)a.size(); i++)
		{
			a[i] = ‘0‘;
		}
		a.push_back(‘1‘);
		return a;
	}

	int mid = (a.size() >> 1);
	int L = mid - 1, R = mid + 1;
	if (a.size() % 2 == 0) R--;//确定中心

	int i = L, j = R;//查找不同点下标
	for ( ; i >= 0 && j < (int)a.size() && a[i] == a[j]; i--, j++);

	bool leftsmall = false;
	if (i < 0 || a[i] <= a[j]) leftsmall = true;//i<0排除了等于的情况了

	for ( ; i >= 0; i--, j++)
	{
		a[j] = a[i];
	}
	if (leftsmall)
	{
		int carry = 1;
		if (a.size() % 2)
		{
			a[mid]++;
			if (a[mid] > ‘9‘) a[mid] = ‘0‘;
			else carry = 0;
		}
		i = L, j = R;
		while (carry)
		{
			a[i]++;
			if (a[i] > ‘9‘) a[i] = ‘0‘;
			else carry = 0;
			a[j] = a[i];
			i--, j++;
		}
	}
	return a;
}

bool isPalindrome(string s)
{
	bool palin = true;
	for (int i = 0, j = int(s.size()) - 1; i < j; i++, j--)
	{
		if (s[i] != s[j]) palin = false;
	}
	return palin;
}

int PrimePalindromes()
{
	bool primes[MAX_NUM+1] = {0};
	for (int i = 2; i <= MAX_NUM; i++)
	{
		if (!primes[i])
		{
			for (int j = (i<<1); j <= MAX_NUM; j += i)
			{
				primes[j] = true;
			}
		}
	}
	int primeNums[MAX_NUM] = {0};
	int id = 0;
	for (int i = 2; i <= MAX_NUM; i++)
	{
		if (!primes[i]) primeNums[id++] = i;
	}

	string s;
	cin>>s;
	int sn = stoi(s);
	assert(1 <= sn && sn <= 1000000);
	if (!isPalindrome(s)) s = nextPalindrome(s);

	while (true)
	{
		sn = stoi(s);
		int sq = (int)sqrt((double)sn);
		bool isprimeNum = true;
		int i = 0;
		for ( ; primeNums[i] <= sq && i < id; i++)
		{
			if (sn % primeNums[i] == 0)
			{
				isprimeNum = false;
				break;
			}
		}
		if (isprimeNum) break;
		s = nextPalindrome(s);
	}
	cout<<s;
	return 0;
}

codechef Prime Palindromes 题解

时间: 2024-10-28 10:53:47

codechef Prime Palindromes 题解的相关文章

USACO 1.5 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 a

Codechef Nuclear Reactors 题解

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will cause 1 particl

codechef Cleaning Up 题解

After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have already left and onl

codechef Permutation Cycles 题解

We consider permutations of the numbers 1,..., N for some N. By permutation we mean a rearrangment of the number 1,...,N. For example 2  4  5  1  7  6  3  8 is a permutation of 1,2,...,8. Of course, 1  2  3  4  5  6  7  8 is also a permutation of 1,2

codechef Turbo Sort 题解

Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7 大数据的排序,输入和输出. 一开始使用了cou

LeetCode: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

codechef Top Batsmen题解

A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the

codechef Birthday Candles 题解

Birthday Candles The chef is preparing a birthday cake for one of his guests, and his decided to write the age of the guest in candles on the cake. There are 10 types of candles, one for each of the digits '0' through '9'. The chef has forgotten the

codechef Cutting Recipes题解

Cutting Recipes The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredie