• BZOJ 2440 [中山市选2011]完全平方数 (二分 + 莫比乌斯函数)


    2440: [中山市选2011]完全平方数

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 4805  Solved: 2325
    [Submit][Status][Discuss]

    Description

    小 X 自幼就很喜欢数。但奇怪的是,他十分讨厌完全平方数。他觉得这些
    数看起来很令人难受。由此,他也讨厌所有是完全平方数的正整数倍的数。然而
    这丝毫不影响他对其他数的热爱。 
    这天是小X的生日,小 W 想送一个数给他作为生日礼物。当然他不能送一
    个小X讨厌的数。他列出了所有小X不讨厌的数,然后选取了第 K个数送给了
    小X。小X很开心地收下了。 
    然而现在小 W 却记不起送给小X的是哪个数了。你能帮他一下吗?

    Input

    包含多组测试数据。文件第一行有一个整数 T,表示测试
    数据的组数。 
    第2 至第T+1 行每行有一个整数Ki,描述一组数据,含义如题目中所描述。 

    Output

    含T 行,分别对每组数据作出回答。第 i 行输出相应的
    第Ki 个不是完全平方数的正整数倍的数。

    Sample Input

    4
    1
    13
    100
    1234567

    Sample Output

    1
    19
    163
    2030745

    HINT

    对于 100%的数据有 1 ≤ Ki ≤ 10^9

    ,    T ≤ 50

    Source

    析:首先能够知道先进行二分答案x,这样我们就可以考虑1~x这个区间内有多少个数约数不包含完全平方数,这个我们可以考虑用容斥来解决,区间内不包含完全平方数的个数也就是总个数(也就是0个质数的倍数) -  一个质数的平方的数倍数(比如4,9,16) + 两个质数的的平方的数的倍数(比如36,64)- ....这个过程就是容斥的过程,很明显我这也正负号其实就是莫比乌斯函数,这样这个题目就可以解决了。

    可能方法不好,BZOJ跑了7s多,好多大佬的代码跑几百毫秒的。。。。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++")
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    //#define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 5e4 + 10;
    const int maxm = 3e5 + 10;
    const LL mod = 1e9 + 7LL;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    bool vis[maxn];
    int prime[maxn];
    int mu[maxn];
    
    void Moblus(){
      mu[1] = 1;
      int tot = 0;
      for(int i = 2; i < maxn; ++i){
        if(!vis[i])  prime[tot++] = i, mu[i] = -1;
        for(int j = 0; j < tot && i * prime[j] < maxn; ++j){
          int t = i * prime[j];
          vis[t] = 1;
          if(i % prime[j] == 0)  break;
          mu[t] = -mu[i];
        }
      }
    }
    
    
    bool judge(LL mid){
      int t = sqrt(mid + 1);
      int ans = 0;
      for(int i = 1; i <= t; ++i)
        ans += mu[i] * mid / i / i;
      return ans >= n;
    }
    
    int main(){
      Moblus();
      int T;  cin >> T;
      while(T--){
       scanf("%d", &n);
       LL l = 1, r = n * 2;
       while(l < r){
        LL mid = l + r >> 1;
        if(judge(mid))  r = mid;
        else l = mid + 1;
       }
       printf("%d
    ", l);
      }
      return 0;
    }
    

      

  • 相关阅读:
    CSS3很强大
    Notepad++关闭时自动保留,不弹出提示保存对话框
    数据库中更新或插入表记录
    手动指定网卡优先顺序方法
    在Excel中转换时间戳(timeStamp)
    format z: /p:3 & cipher /w z:abc
    修改eclipse中M2_REPO变量值
    Windows 10 主题的图片位置
    MySQL中 delete from 时提示 1064 错误。
    left join 中 on 与 where 理解
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8365841.html
Copyright © 2020-2023  润新知