codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)

题意:求使pi(n)*q<=rub(n)*p成立的最大的n。

先收集所有的质数和回文数。质数好搜集。回文数奇回文就0-9的数字,然后在头尾添加一个数。在x前后加a,就是x*10+a+a*pow(10,2)。偶回文同理。然后不能二分,因为比值不是单调的。

乱码:

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
const int SZ=1e7+10,INF=0x7FFFFFFF;
typedef long long lon;

vector<lon> pri,pld;
bool isp[SZ];

void init()
{
    memset(isp,1,sizeof(isp));
    isp[0]=isp[1]=0;
    for(lon i=2;i*i<SZ;++i)
    {
        for(lon j=i*i;j<SZ;j+=i)
        {
            isp[j]=0;
        }
    }
    for(lon i=2;i<SZ;++i)
    {
        if(isp[i])
        {
            pri.push_back(i);
        }
    }
}

lon pow(lon x,lon p,int rbs1,int rbs2)
{
    lon res=1,ele=x;
    for(;p;p>>=1)
    {
        if(p&1)res*=ele;
        ele=ele*ele;
    }
    return res;
}

void getp(lon x,lon bit)
{
    if(bit>8)
    {
        return;
    }
    lon res;
    for(lon i=0;i<=9;++i)
    {
        res=0;
        res=x*10;
        res+=(bit&1)?i*pow((lon)10,bit+1,0,0):i*pow((lon)10,bit+1,0,0);
        res+=i;
        if(i)pld.push_back(res);
        getp(res,bit+2);
    }
}

int main()
{
    std::ios::sync_with_stdio(0);
    init();
    for(lon i=0;i<=9;++i)
    {
        if(i)pld.push_back(i*11);
        getp(i*11,2);
    }
    for(lon i=0;i<=9;++i)
    {
        if(i)pld.push_back(i);
        getp(i,1);
    }
    sort(pld.begin(),pld.end());
//    for(int i=0;i<pld.size();++i)
//    {
//        cout<<pld[i]<<endl;
//    }
    //cout<<pri.size()<<" "<<pld.size()<<endl;
    lon p,q;
    cin>>p>>q;
    lon res=0;
    for(lon i=1;i<2e6+10;++i)
    {
        lon num1=upper_bound(pri.begin(),pri.end(),i)-pri.begin();
        lon num2=upper_bound(pld.begin(),pld.end(),i)-pld.begin();
        //if(i<2600&&i>2500)cout<<i<<" "<<num1<<" "<<num2<<endl;
        if(num1*q<=num2*p)
        {
            res=max(res,i);
        }
    }
    cout<<res<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/gaudar/p/9646002.html

时间: 2024-08-12 13:54:23

codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)的相关文章

Codeforces Round #315 (Div. 1)

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 the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #315 (Div. 1) A. Primes or Palindromes? 暴力

A. Primes or Palindromes?Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 Description Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindro

Codeforces Round #315 (Div. 2) (ABCD题)

A. Music 题意: 一首歌长度为S秒,已经下载了T秒,下载速度为每q秒的现实时间能下载下来(q-1)秒 的歌曲.现在开始听歌,如果听到还没下载的地方就从0秒的地方开始replay,求一首歌听完需要从0秒听几次(包括一开始那次) 思路: 我们可以用路程-时间的思路来考虑这道题. 假设两位选手"播放"与"下载","播放"的起点是0m处,"下载"的起点是Tm处,终点在Sm处,"播放"的速度是1m/s,&qu

2017-4-29-Train:Codeforces Round #315 (Div. 2)

A. Music(数学题) Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of

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

题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以可以先暴力求下最大的n满足上式,可以想象下随着n的增大A也在增大(总体正相关,并不是严格递增的),所以二分查找时不行的,所以对给定的A,n是一定存在的.这个题的关键就是快速得到素数表最好在O(n)的时间以内.(杭电15多校的一个题也用到了这个算法点这里查看) 1 #include <bits/std

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

这道题竟然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,其中A=p/q: 然后要你找到对于给定的A,找到使得π(n)?≤?A·rub(n) 最大的n. (A<=42) 思路: 首先我们可以暴力算出当n为大概150万左右的时候,π(n)大概是 rub(n) 的42倍. 所以我们只需要for到150万左右就好,因为对于后面的式子,肯定能在150万的范围内找到一个n使得这个式子成立的. 而且,我们可以得出因为素

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) 手动模拟暴力一下就可以发现其实这个函数大概是先下降后上升的,由于1/42<=p/q<=42,也就是说当y=42的时候就 是它的边界了,那么n的范围大概是1200000左右,取

Codeforces Round #315 (Div. 2)

Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of E

Codeforces Round #315 (Div. 2) A. Music 解题心得

原题: Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city