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

http://codeforces.com/contest/569/problem/C

π(n) — the number of primes no larger than n
rub(n) — the number of palindromic numbers no larger than n.  
for a given value of the coefficient A find the maximum n, such that π(n) ≤ A·rub(n).
A ().

没想到先观察一下数据,,,发现最大的A是42,大概n到1200000后   n再大,就会有π(n) >A·rub(n).


因此只要暴力做到120W就好了


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

const int maxn=1200005;
int ok(int x)<span style="white-space:pre">			</span>//判断回文
{
	int ret=x;
	int j=0;
	while(x)
	{
	j=j*10+x%10;
	x/=10;
	}
	if (j==ret)
		return 1;
	return 0;

}


int p[maxn+5]; <span style="white-space:pre">		</span>//n之前素数个数
int q[maxn+5];<span style="white-space:pre">			</span>//n之前回文数个数
bool f[maxn+5];
int main() 
{
//	cout<<ok(212)<<endl;
//	return 0;
	__int64 i,j;
	f[1]=true;
	for (i=2;i<=maxn;i++)
	{
		if (f[i]==false)
		{
			for (j=i*i;j<=maxn;j=j+i)
			{
				 
				f[j]=true;
			}
		}
	}

	for (i=1;i<=maxn;i++)
	{
		if (f[i]==false)
			p[i]=p[i-1]+1;
		else
			p[i]=p[i-1];
	}


	for (i=1;i<=maxn;i++)
	{
		if (ok(i))
			q[i]=q[i-1]+1;
		else
			q[i]=q[i-1];
	}

	int a,b;
	cin>>a>>b;
	for (i=maxn;i>=0;i--)
	{
		if (p[i]*b<=q[i]*a)
		{
			printf("%d\n",i);
			return 0;
		}

	}



	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值