zoukankan      html  css  js  c++  java
  • 湖南大学ACM程序设计新生杯大赛(同步赛)D

    题目描述

    We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), and prime fourth power(四次方).
    The first four Shuaishuai numbers are:
    How many Shuaishuai numbers in [1,n]? (1<=n<=50 000 000)

    输入描述:

    The input will consist of a integer n.

    输出描述:

    You should output how many Shuaishuai numbers in [1...n]
    示例1

    输入

    28

    输出

    1

    说明

    There is only one Shuaishuai number

    题解

    暴力打标。

    把所有满足要求的数组都存进数组,排序后去重,每次询问二分即可。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
     
    const int maxn = 50000000;
    int a[1200000 + 10];
    int sz = 0, cnt = 0;
    int b[1200000 + 10];
    bool noprime[maxn + 10];
    int n;
     
    void init() {
      noprime[1] = 1;
      for(int i = 2; i <= maxn; i ++) {
        if(noprime[i]) continue;
        for(int j = i + i; j <= maxn; j = j + i) {
          noprime[j] = 1;
        }
      }
      for(int i = 1; i * i <= maxn; i ++) {
        if(noprime[i]) continue;
        for(int j = 1; i * i + j * j * j <= maxn; j ++) {
          if(noprime[j]) continue;
          for(int k = 1; i * i + j * j * j + k * k * k * k <= maxn; k ++) {
            if(noprime[k]) continue;
            a[sz ++] = i * i + j * j * j + k * k * k * k;
          }
        }
      }
      sort(a, a + sz);
      b[cnt ++] = a[0];
      for(int i = 1; i < sz; i ++) {
        if(a[i] == a[i - 1]) continue;
        b[cnt ++] = a[i];
      }
    }
     
    int main() {
      init();
      while(~scanf("%d", &n)) {
        int L = 0, R = cnt - 1, pos = -1;
        while(L <= R) {
          int mid = (L + R) / 2;
          if(b[mid] <= n) pos = mid, L = mid + 1;
          else R = mid - 1;
        }
        printf("%d
    ", pos + 1);
      }
      return 0;
    }
    

      

  • 相关阅读:
    用Fiddler模拟低速网络环境
    定制 Fiddler 之将请求发往另一服务器
    软件测试流程进阶----两年软件测试总结
    Fiddler 4 抓包(APP HTTPS )
    JMeter报错 ERROR o.a.j.t.JMeterThread: Test failed!
    Python显示百分比
    python 2 到 3 的新手坑
    Python运行的17个时新手常见错误小结
    Python去除字符串的空格
    js控制媒体查询样式/判断是PC端还是移动端
  • 原文地址:https://www.cnblogs.com/zufezzt/p/8099028.html
Copyright © 2011-2022 走看看