Codeforces Round #315 (Div. 2)569C Primes or Palindromes?(预处理)

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor20+工具畅享,效率翻倍! 阅读详情

C. Primes or Palindromes?
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!

Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.

Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.

One problem with prime numbers is that there are too many of them. Let's introduce the following notation: π(n) — the number of primes no larger than nrub(n) — the number of palindromic numbers no larger than n. Rikhail wants to prove that there are a lot more primes than palindromic ones.

He asked you to solve the following problem: for a given value of the coefficient A find the maximum n, such that π(n) ≤ A·rub(n).

Input

The input consists of two positive integers pq, the numerator and denominator of the fraction that is the value of A ().

Output

If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).

Sample test(s)
input
1 1
output
40
input
1 42
output
1
input
6 4
output
172



最大的A是42,n大于1200000则π(n) > A·rub(n)。回文数比较少,把所有回文数都保存到数组中,接着保存素数到数组中,

最后判断条件即可。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "cmath"
using namespace std;
const int MAXN = 1200005;
int a[MAXN], prim[MAXN], hw[MAXN], p, q;
int judge(int k)
{
	int len = 0;
	char c[10];
	while(k > 0) {
		c[len++] = char(k % 10 + '0');
		k /= 10;
	}
	for(int i = 0; i < len / 2; ++i)
		if(c[i] != c[len - i - 1]) return 0;
	return 1;

}
int main(int argc, char const *argv[])
{
	for(int i = 1; i <= 1200000; ++i)
		hw[i] = hw[i - 1] + judge(i);
	scanf("%d%d", &p, &q);
	for(int i = 2; i <= sqrt(1200000); ++i) {
		if(!a[i])
			for(int j = i * 2; j <= 1200000; j += i) {
				a[j] = 1;
				prim[i] = 1;
			}
		prim[i] += prim[i - 1];
	}
	for(int i = sqrt(120000) + 1; i <= 1200000; ++i)
		prim[i] = prim[i - 1] + (!a[i]);
	for(int i = 1200000; i >= 0; --i)
		if(prim[i] <= double(hw[i] * p / q)) {
			printf("%d\n", i);
			return 0;
		}
	printf("Palindromic tree is better than splay tree\n");
	return 0;
}


zoj2744 Palindromes 字符串的题 题意是求给一个字符串算出其中有多少个回文子串(每个回文子串必须是连续的),关于这题由于数据范围才5000,所以可以用O(N*N)的动态规划去做或者是用暴力的搜索,每次枚举回文串的对称中点复杂度不超过O(N*N),但是用dp做就很蛋疼了,因为内存限制存不了所有的状态,所以得用滚动数组去存,下面将给出几种不同实现的代码。如果暴力枚举中点方法确实很简单,但我还是建议,写一下dp,/可以锻炼一下代码能了, 阅读详情

相关推荐

COMSOL报错调试总结(不定期更新)

COMSOL报错调试总结1. 可定位错误1.1 除零错误1.2 未定义变量值1.3 边界条件/材料属性等缺失2. 难定位错误2.1. 装配期间内存不足 官方给的案例总是非常完美,运行流畅,结果完美。但是自己做模型时候就总是报错,而且不像自己coding的程序那样可以明确定位哪里出了bug。这里把我碰到的一些报错问题以及调试方法记录下来~ 部分来源于以下参考来源: http://wap.sciencenet.cn/blog-1467490-1118453.html?mobile=1 1. 可定位错误 以下错误

jessica0307的博客 8万+

Codeforces Round #315 (Div. 2) C. Primes or Palindromes? 素数表 回文数

C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that t

习惯与规则决定命运 1348

用LangFuse+LangChain打造可观测的AI应用:从埋点到成本监控实战

本文详细介绍了如何将开源可观测性平台LangFuse深度集成到基于LangChain构建的AI应用中,特别是电商客服机器人场景。通过从基础SDK集成、提示词管理到成本监控的完整实战,帮助开发者实现对LLM应用内部工作流的清晰追踪、问题排查与费用分析,从而打造稳定、可控的生产级AI应用。

dapp9builder的博客 688

CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)

题干: Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing...

xuanweiace的博客 575

Codeforces Round #315 (Div. 2)

A. Music time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little Lesha loves listening to music via his smartphone. ...

weixin_30516243的博客 257

CodeForces - 569C Primes or Palindromes?

CodeForces - 569C Primes or Palindromes? 这个题比赛的时候就压根没看,结果事后一翻题解发现居然就是一个暴力……算了算了,看了也不敢写。 Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and ...

mandiheyanyu的博客 293

H - Primes or Palindromes?

Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and

philophobia的博客 366

Codeforces Round #315 (Div. 2)C. Primes or Palindromes?(暴力)

题目链接:https://codeforces.com/problemset/problem/569/C 题目大意:求最大的n满足q*小于等于n的素食个数&lt;=p*小于等于n的回文数个数 题目思路:直接暴力,处理好素数表和暴力出回文数,然后1e7到1暴力即可。 以下是代码: #include&lt;bits/stdc++.h&gt; #include&lt;iostrea...

smilestruggler_Fly To Higher 212

Codeforces Round #315 (Div. 2) C. Primes or Palindromes?

C. Primes or Palindromes?time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard outputRikhail Mubinchik believes that the current definition of prime numb

2=10 548

Primes or Palindromes? CodeForces - 569C

http://codeforces.com/problemset/problem/569/C 神奇的暴力,注意大数相乘可能爆ll #include #include #include #include #include typedef long long ll; using namespace std; const int maxn=9900000; int prime[maxn+10],p

海绵的博客 292

Codeforces 568 A Primes or Palindromes?(求素数个数+判断是否是回文数)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that t

youngkl博客 909

A. Primes or Palindromes?

题目链接:http://codeforces.com/problemset/problem/568/A Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number...

wxl7777的博客 303

codeforces 568A A. Primes or Palindromes?(打表+暴力枚举)

题目链接:codeforces 568A题目大意:给出两个整数p,q,设A=pq,sum1[i]为≤i的素数的个数,sum2[i]为≤i的回文数的个数,问sum1[i]≤A⋅sum2[i]的最大的i是多少给出两个整数p,q,设A=\frac{p}{q} , sum_1[i]为\leq i的素数的个数,sum_2[i]为\leq i的回文数的个数,问sum_1[i] \leq A \cdot sum_

llin-黎辰 747

CodeForces #315 (div1) A.Primes or Palindromes?

题目大意: 求出最大的n,使得小于等于n的素数的个数 解题思路: 显而易见一定有答案。 因为A的值很小,而且回文数的个数很少,所以素数打表暴力统计,回文数暴力判断。 n的最大值大概是130w的样子,倒着枚举就可以了。 #include #define LL long long using namespace std; const int N=8000005, mx=8

test blog 775

C. Primes or Palindromes?

prime numbers non greater thannis about. We can also found the amount of palindrome numbers with fixed lengthk— it is aboutwhich is. #include <iostream> #include <cstdio> #...

weixin_30357231的博客 156

Palindromes _easy version(杭电2029)

#include  #include  main() {     int n;     char a[1000],*p,*q;     while(scanf("%d",&n)!=EOF)     {         getchar();         while(n--)         {             gets(a);             for(p=

u014292303的专栏 485

Codeforces Round #315 (Div. 2) C - Primes or Palindromes?(暴力打表)

题意:给一个p和q然后求π(n) ≤ p/q*rub(n),的最大的n值,其中π(n) 表示从1到n之间的素数的个数, rub(n)表示从1到n之间的回文数的个数(回文数不能有前导0,且从左到右和从右到左一样) 分析:其实这题没有题目没有确定n的范围让人不敢直接暴搜打表,但是你只要手动写个函数y=π(n) /rub(n) 手动模拟暴力一下就可以发现其实这个函数大概是先下降后上升的,由

sin_XF的专栏 819

CF 568A 数学 暴力

CF 568A 题目链接: http://codeforces.com/problemset/problem/568/A 题意: 设A(n),B(n)分别表示前n个数中素数个数、回文数个数。 问最大n满足A(n)  思路: 看到题解我一脸日了狗的表情+_+ 最大数1300000,然后从大到小枚举。 因为前n数种素数数量增长速度远大于前n个数中回文数的数量,而对于n=1时素数个数为

beihai2013 510

Palindromes

FJNU.1199DescriptionPalindromes are strings that read the same both forwards and backwards. `Eye is one such example (ignoring case). In this problem, you get to write a program to determine if a g

linyq@BambooFan ~ 1225

大于N的最小回文数

题目描述     Palindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;     For a given integer N, y

LiuXingLong 3893

acm hnu 10136 Palindromes

Palindromes Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users:

Manio 2069
上一篇: Codeforces Round #315 (Div. 2)569B Inventory(队列)
下一篇: 2015-08-11
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值