【XSY1580】Y队列 容斥
题目大意
给你\(n,r\),求第\(n\)个不能被表示为\(a^b(2\leq b\leq r)\)的数
\(n\leq 2\times {10}^{18},r\leq 62\)
题解
我们考虑二分,求\(\leq m\)的不能被表示为\(a^b\)的数\(f(m)\)
我们先忽略\(1\)
我们钦定能被表示为\(a^2,a^3,a^5\)等\(b\)为质数的数,贡献为\(\lfloor\sqrt[2]{m}\rfloor-1,\lfloor\sqrt[3]{m}\rfloor-1\cdots\),这样也会包含当\(b\)为合数时的情况,例如\(a^4={(a^2)}^2\)
但我们算多了,例如\(a^3=b^2=c^6\),所以我们要减掉\(b\)为两个不同的质数的积的情况,即\(\lfloor\sqrt[6]{m}\rfloor-1,\lfloor\sqrt[10]{m}-1\rfloor\cdots\)
然后加上\(b\)为三个不同的质数的积的情况,减掉\(b\)为四个不同的质数的积的情况……
我们发现\(b=x\)时容斥系数为\(\mu(x)\)
当\(b>62\)时\(\lfloor\sqrt[b]{m}\rfloor=1\),所以不用继续往下算了
还有,开\(n\)次根号可以用pow,不过要传long double参数进去,不然就会炸精度。
但是这样子还会tle,因为有\(30000\)组数据
我们发现\(f(x)\approx\sqrt{x}\)
我们计算出\(f(n)\),然后每次把\(n\)加上\(n-f(n)\),可以很快得到答案
时间复杂度:\(O(???)\)
反正能过且常数巨大就对了
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
int p[100];
int u[100];
int b[100];
int mx[100];
int cnt;
ll n;
int k;
ll fp(ll a,ll b)
{
ll s=1;
while(b)
{
if(b&1)
s=s*a;
a=a*a;
b>>=1;
}
return s;
}
ll calc(ll n,ll x)
{
ll s=floor(ld(pow(ld(n),ld(1)/x)));
return s;
}
ll count(ll x)
{
int i;
ll s=0;
ll nw;
for(i=1;i<=62;i++)
if(mx[i]<=k&&u[i])
{
nw=calc(x,i)-1;
if(!nw)
break;
s+=u[i]*nw;
}
return s;
}
void solve()
{
scanf("%lld%d",&n,&k);
ll t=n;
ll s=count(t);
while(1)
{
t+=n-s;
s=count(t);
if(s==n)
break;
}
printf("%lld\n",t);
}
int main()
{
int i,j;
cnt=0;
memset(b,0,sizeof b);
u[1]=1;
mx[1]=1;
for(i=2;i<=62;i++)
{
if(!b[i])
{
p[++cnt]=i;
mx[i]=i;
u[i]=-1;
}
for(j=1;j<=cnt&&i*p[j]<=62;j++)
{
b[i*p[j]]=1;
mx[i*p[j]]=mx[i];
if(i%p[j]==0)
{
u[i*p[j]]=0;
break;
}
u[i*p[j]]=-u[i];
}
}
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}
【XSY1580】Y队列 容斥的更多相关文章
- HDU 5297 Y sequence 容斥 迭代
Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...
- HDU2841 (队列容斥)
Visible TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)
G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- HDU - 5297:Y sequence (迭代&容斥)
Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
/** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...
- hdu 5664 Lady CA and the graph(树的点分治+容斥)
题意: 给你一个有n个点的树,给定根,叫你找第k大的特殊链 .特殊的链的定义:u,v之间的路径,经过题给的根节点. 题解:(来自BC官方题解) 对于求第k大的问题,我们可以通过在外层套一个二分,将其转 ...
- HDU 4059 容斥初步练习
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- 【BZOJ-4455】小星星 容斥 + 树形DP
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 204 Solved: 137[Submit][Status] ...
- UVa12633 Super Rooks on Chessboard(容斥 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...
随机推荐
- Python Revisited Day 03 (组合数据类型)
目录 第三章 组合数据类型 3.1 序列类型 3.1.1 元组 3.1.2 命名的元组 (collections.nametuple()) 3.1.3 列表 (查询有关函数点这) 3.1.4 列表内涵 ...
- 动态规划-LIS最长上升子序列
优化链接 [https://blog.csdn.net/George__Yu/article/details/75896330] #include<stdio.h> #include< ...
- Python删除list里面的重复元素的俩种方法
1.使用set函数 In [116]: a=[1,2,3,2,1,3,4,5,6,5] In [117]: set(a) Out[117]: {1, 2, 3, 4, 5, 6} 2.使用字典函数 ...
- JSP页面的基本元素
JSP页面元素构成:静态内容.指令.表达式.小脚本.声明.注释. JSP指令包括: page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令. include指令:将一个外部文件嵌入到 ...
- ps昏暗室内照片调成暖色光亮效果
最终效果 一.打开素材图片,把背景图层复制一层,做HDR滤镜操作,如果你没有这款滤镜,可以去网上下载,参数及效果如下图. 二.复制一层,用Noise滤镜做降噪处理,参数及效果如下图. 三.新建一个图层 ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in range(128)
python报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in ...
- Azure系列2.1.15 —— SharedAccessBlobPolicy
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- [转帖]Windows批处理(cmd/bat)常用命令小结
Windows批处理(cmd/bat)常用命令小结 非常值得学习的文档 先放这里 有时间做实验, 转载自:“趣IT”微信公共号 前言 批处理文件(batch file)包含一系列 DOS命令,通常用于 ...
- [转帖]How To Be Successful
How To Be Successful http://blog.samaltman.com/how-to-be-successful 总结一下文章的重点: 1. Compound yourself2 ...
- 将表单数据转换为json代码分享
<body> <form action="#" method="post" id="form1"> <inpu ...