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 ...
随机推荐
- 浏览器 Event对象 及 属性 的兼容处理
摘自: http://blog.csdn.net/jiachunfeng/article/details/6448186 event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以, ...
- ubuntu+anaconda+python安装各版本tensorflow
一.安装anaconda 1.去官网下载anaconda linux版本即可 选择合适的版本下载即可 2.安装Aanconda: 打开终端(Ctrl+Alt+t)进入到下载的目录一般在home 下的D ...
- CentOS 7 安装各个桌面版本
http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...
- iOS动画进阶 - 手摸手教你写 Slack 的 Loading 动画
如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博客叫手摸手教你写 Slack 的 Loading 动画,看着挺炫,但是是安卓版的,寻思的着仿造着写一篇iOS版的,下面是我写这个动画 ...
- [POI2013]BAJ-Bytecomputer
题目描述 A sequence of integers from the set is given. The bytecomputer is a device that allows the foll ...
- 【bzoj1009】[HNOI2008]GT考试(矩阵快速幂优化dp+kmp)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 这道题一看数据范围:$ n<=10^9 $,显然不是数学题就是矩乘快速幂优 ...
- 解决com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes异常
错误截图: 解决方法: 用root进入mysql终端,执行以下命令: alter database hive character set latin1;
- C语言之非常简单的几道题
C语言之非常简单的几道题(还是写写),比较简单吧,主要有几道题的数据类型(如,第三题)和语句顺序(如,第二题)需要注意一小下下. 1. 求表达式S=1*2*3……*N的值大于150时,最小的N的值 / ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Listview_简单使用_(Virtual)
1.代码来自于“C:\Program Files (x86)\Borland\Delphi7\Demos\Virtual Listview” 1.1.是使用 ListView来显示数据 1.2.自己管 ...