HDU - 5297:Y sequence (迭代&容斥)
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 (迭代&容斥)的更多相关文章
- HDU 5297 Y sequence 容斥 迭代
Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...
- 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 ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法
题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...
- GCD HDU - 1695 (欧拉 + 容斥)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 4336 Card Collector —— Min-Max 容斥
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...
- HDU 2841 Visible Trees(容斥定理)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- ASP.NET MVC CheckBoxFor的int to bool
当我们使用CheckBoxFor类型需要使用bool ,可以将 int转换成bool <div class="form-group"> <label class= ...
- windows10下Python如何设置环境变量
1.右击“我的电脑”,选择“属性”, 2.选择“高级系统设置”, 3.选择“环境变量”, 4.在“系统变量”中选中“Path”,再点“新建”.(Python.Scripts两个目录都要加,只加Pyth ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
- 华丽的使用sublime写lua~ sublime lua相关必装插件推荐~~
缘起 lua脚本语言虽好,代码写得飞快,可是写错了调试起来却很困难,lua使用者经常容易犯得一个错误是--写错变量名了,if end 嵌套太多没匹配~,多打了一个逗号, --假设定义了一个变量 loc ...
- Oracle常见的几种登录方式
1.运行SQLPLUS工具 C:\Users\csb>sqlplus(回车) (输入账户)system(回车) (输入密码) (回车) 2.直接进入SQLPLUS命令提示符,无用户的登陆 C:\ ...
- git diff提示filemode发生改变解决办法
git diff提示filemode发生改变(old mode 100644.new mode 10075) Posted on 2016-11-15 16:55 我是孙海龙 阅读(2292) 评论( ...
- linux的一些操作
在终端输入cat /etc/issue 查看ubuntu的半磅不知道ubuntu特权用户root的密码时:Ubuntu在默认情况下是不启用root用户的,所以这对于一下对于linux命令不熟悉的用户在 ...
- python基础7 - 函数2
4. 使用元组让函数返回多个值 利用 元组 同时返回温度和湿度 def measure(): """返回当前的温度""" temp = 39 ...
- Java 基于JavaMail的邮件发送
http://blog.csdn.net/xietansheng/article/details/51673073 http://blog.csdn.net/xietansheng/article/d ...
- Mac 终端 Termial 高亮配置(PS1变量配置)
操作环境: 系统:Mac 10.12 编辑器:vim 一.无脑配置: 1. 打开中端输入: vi ~/.bash_profile 2. 打开并编辑 .bash_profile 文件: 按键盘“i”,进 ...