Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar calls the sequence that formed by the rest integers“Y sequence”.When r=3,The first few items of it are:
2,3,5,6,7,10......
Given positive integers n and r,you should output Y(n)(the n-th number of Y sequence.It is obvious that Y(1)=2 whatever r is).

InputThe first line of the input contains a single number T:the number of test cases.
Then T cases follow, each contains two positive integer n and r described above.
n<=2*10^18,2<=r<=62,T<=30000.
OutputFor each case,output Y(n).Sample Input

2
10 2
10 3

Sample Output

13
14

题意:F(x)表示不大于x的而且满足不少a^b形式的个数,求ans,使得F(ans)=N。

思路:我们可以荣容斥的方法求F(x)。 所以可以用二分来得到答案,但是我们可以用二分超时。 所以我们需要用高效的方法。

先假设当前答案是ans,然后求F(ans),如果F(ans)<N,表示还至少缺N-F(ans)个,所以ans=ans+N-F(ans),继续下一次验证,直到F(ans)==N。

这样优于二分的原因是a^b的形式比较离散,所以迭代的次数比较少。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int prime[]={-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-};
vector<int>p;
void getR(int R)
{
p.clear();
for(int i=;abs(prime[i])<=R;i++){
int sz=p.size();
for(int j=;j<sz;j++){
if(abs(prime[i]*p[j])<=)p.push_back(prime[i]*p[j]);
}
p.push_back(prime[i]);
}
}
ll cal(ll x)
{
if(x==) return ;
ll res=x;
for(int i=;i<p.size();i++){
ll tmp=(ll)pow(x+0.5,1.0/abs(p[i]))-;
if(p[i]<) res-=tmp;
else res+=tmp;
}
return res-;
}
void solve(ll N,int R)
{
getR(R); ll ans=N;
while(){
ll tmp=cal(ans);
if(tmp==N) break;
ans+=N-tmp;
}
printf("%I64d\n",ans);
}
int main()
{
int T,R; ll N;
scanf("%d",&T);
while(T--){
scanf("%I64d%d",&N,&R);
solve(N,R);
}
return ;
}

超时的二分代码:

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int prime[]={-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-};
vector<int>p;
void getR(int R)
{
p.clear();
for(int i=;abs(prime[i])<=R;i++){
int sz=p.size();
for(int j=;j<sz;j++){
if(abs(prime[i]*p[j])<=)p.push_back(prime[i]*p[j]);
}
p.push_back(prime[i]);
}
}
ll cal(ll x)
{
if(x==) return ;
ll res=x;
for(int i=;i<p.size();i++){
ll tmp=(ll)pow(x+0.5,1.0/abs(p[i]))-;
if(p[i]<) res-=tmp;
else res+=tmp;
}
return res-;
}
void solve(ll N,int RR)
{
getR(RR); ll L=N,R=L+,ans;
while(L<=R){
ll Mid=(L+R)/;
if(cal(Mid)>=N) ans=Mid,R=Mid-;
else L=Mid+;
}
cout<<ans<<endl;
}
int main()
{
int T,R; ll N;
scanf("%d",&T);
while(T--){
cin>>N>>R;
solve(N,R);
}
return ;
}

HDU - 5297:Y sequence (迭代&容斥)的更多相关文章

  1. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  2. HDU 5297 Y sequence Y数列

    题意:给定正整数n和r.定义Y数列为从正整数序列中删除全部能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少. 比如n = 10. r = 3,则Y数列为2 3 5 6 7 1 ...

  3. HDU 5768 Lucky7 (中国剩余定理+容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...

  4. HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

    题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...

  5. GCD HDU - 1695 (欧拉 + 容斥)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  7. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  8. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

  9. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. $python虚拟化运行环境——virtualenv

    介绍 virtualenv是一种虚拟化环境,可以理解为创建了一个虚拟化的pyhon运行空间,可以从新安装各种库,而与本机环境互不影响,互相隔离. 安装及使用 首先要安装包管理工具pip(pip的使用详 ...

  2. WPF MVVM TreeView 实现 右键选中 右键菜单

    1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown=& ...

  3. 用OpenCV实现Photoshop算法(三): 曲线调整

    http://blog.csdn.net/c80486/article/details/52499919 系列文章: 用OpenCV实现Photoshop算法(一): 图像旋转 用OpenCV实现Ph ...

  4. SpringBoot 加载配置文件

    1.application.properties或application.yaml是SpringBoot默认的配置文件. 可以通过@Value注解 配合 ${......}来读取配置在属性文件中的内容 ...

  5. cf780c

                                                                                             C. Andryush ...

  6. [转]Markdown 公式指导手册(包含LaTeX)

    Cmd Markdown 公式指导手册 本文为转载文章,并且由于LaTeX的可能不能全部兼容,所以可能有部分公式无法在博客园显示,可以移步原网站. 本文固定链接: https://www.zybulu ...

  7. ubuntu 16.04 vscode + php debug

    1.vscode 安装PHP Debug扩展: 2.php环境配置: 1.安装xdebug扩展: sudo apt-get install php-xdebug 2.找到扩展的路径: chq@chq- ...

  8. 进程管理工具supervisor

    1. 简介 supervisor有两个组件:supervisord和supervisorctl,组成了client/server结构. supervisord负责读入配置文件,然后supervisor ...

  9. python 低版本一段扫描代码

    个人在做Linux渗透测试往内网跨的时候,通常我碰到的Linux环境都会是如下集中情况 1: DMZ,严格的DMZ,根本跨不到内网里去.这种最恶心了. 2:WEB SERVER,严格区分,工作机和工作 ...

  10. jQuery中hover和blur使用代理delegate无效的解决方法

    今天就遇到了这样的小问题: $(document).ready(function(){ $('.status_on').hover(function(){ $(this).html('点击禁用'); ...