zoukankan      html  css  js  c++  java
  • 【BZOJ 2440】[中山市选2011]完全平方数

    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

    莫比乌斯反演

    感觉这种题的做题思路就是求出莫比乌斯函数,然后求出前缀和,再统计1~L-1和1~R中有多少符合条件的,减一减就好了

    或许是因为我做的少吧。。。

    这个题再做一下二分。。。注意一下二分边界!!!各种WA

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cmath>
     4 using namespace std;
     5 const int N=50010;
     6 int mu[N],pri[N],sum[N];
     7 int tot,T,a,b,c,d,k,ans;
     8 bool mark[N];
     9 void pre(){
    10     mu[1]=1;
    11     for (int i=2;i<=50000;i++){
    12         if (!mark[i]){
    13             pri[++tot]=i;
    14             mu[i]=-1;
    15         }
    16         for (int j=1;j<=tot&&pri[j]*i<=50000;j++){
    17             mark[pri[j]*i]=1;
    18             if(i%pri[j]==0) {
    19                 mu[pri[j]*i]=0;break;
    20             }else mu[pri[j]*i]=-mu[i];
    21         }
    22     }
    23 };
    24  
    25 bool calc(int x){
    26     int y=sqrt(x);long long sum=0;
    27     for (int i=1;i<=y;i++){
    28         sum+=mu[i]*(x/(i*i));
    29     }
    30     if (sum>=k) return 1;return 0;
    31 }
    32  
    33 int main(){
    34     pre();
    35     scanf("%d",&T);
    36     while(T--){
    37         scanf("%d",&k);
    38         long long l=k,r=1644934089;
    39         while(l<r){
    40             long long mid=(l+r)>>1;
    41             if (!calc(mid))l=mid+1;
    42             else r=mid,ans=mid;
    43         }
    44         printf("%d
    ",ans);
    45     }
    46 }
  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/wuminyan/p/5188322.html
Copyright © 2011-2022 走看看