zoukankan      html  css  js  c++  java
  • codeforces 569 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 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

    先预处理出来比小于等于n的素数的个数和回文数的个数...

    素数不筛竟然也可以过 

    然后暴力就好.

    需要注意的是,比值并不单调,找最大的n,可能之前某个数开始不满足条件,之后又有满足条件的了.

    我们可以倒着扫来找,第一个满足条件的就是最大的.

    /*************************************************************************
        > File Name: code/cf/#315/C.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年08月11日 星期二 00时54分13秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    const int inf = 0x7fffffff;
    const int N=2E6+5;
    int f[N],g[N];
    bool prime( int x)
    {
        if (x==1) return false;
        if (x<=3) return true;
        for ( int i = 2 ; i*i<=x ; i ++)
        {
        if (x%i==0)
        return false;
        }
        return true;
    }
    bool pal( int x)
    {
        int a[100];
        int len=0;
        while(x)
        {
        len++;
        a[len]=x%10;
        x = x/ 10;
        }
    
        for ( int i = 1 ;i  <= len/2 ; i++)
        {
        if (a[i]!=a[len+1-i])
            return false;
        }
        return true;
    }
    int main()
    {
        int cnt = 0;
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        for (int i = 1 ; i <= N-5 ; i ++)
        {
        if (prime(i))
        {
            cnt++;
    
        //    pri[cnt]  = i;
        }
        f[i] = cnt;
        }
        int cnt2 = 0 ;
        for ( int i =1 ; i <=N-5 ; i++)
        {
        if (pal(i))
        {
            cnt2++;
    
        }
        g[i] = cnt2;
        }
     //   for ( int i =1; i <= N-5; i ++)
     //   {
    //    cout<<"i:"<<i<<"f[i]:"<<f[i]<<" g[i]:"<<g[i]<<"f[i]/g[i]:"<<f[i]*1.0/g[i]*1.0<<" g[i]/f[i]"<<g[i]*1.0/f[i]<<endl;
      //  }
        int p,q;
        int i = 0;
        scanf("%d %d",&q,&p);
        double r = p*1.0/q;
        for ( int i = N-5 ; i >= 1 ; i--)
        {
        if (r*f[i]<=g[i])
        {
            cout<<i<<endl;
            break;
        }
        }
        
    
        return 0;
    }
  • 相关阅读:
    git 常用操作命令行
    Mysql 命令行...
    bootstrap
    10.11 android输入系统_补充知识_activity_window_decor_view关系
    10.10 android输入系统_APP获得并处理输入事件流程
    10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
    10.8 android输入系统_实战_使用GlobalKey一键启动程序
    10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理
    10.6 android输入系统_Dispatcher线程_总体框架
    10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
  • 原文地址:https://www.cnblogs.com/111qqz/p/4731640.html
Copyright © 2011-2022 走看看