nssl 1458 HR的疑惑

题目

求\([1\sim n]\)中有多少个正整数\(x\)满足

\[\sqrt[y]{x}\in N^{+},y>1
\]

其中\(n\leq 10^{18}\)


分析

枚举指数,想要不重复必然是质数或互不相同的质数之积,

容斥求方案数,对于一个指数\(x\),不考虑重复的答案为\(\lfloor\sqrt[x]{n}\rfloor\)

注意特判1,将1每次统计时都减掉,最后再加上1


代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define rr register
using namespace std;
typedef long long lll; lll n; int xo[66011],sum,ans;
const int prime[17]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
signed main(){
scanf("%lld",&n),xo[0]=0;
for (rr int i=1;i<65536;++i) xo[i]=xo[i&(i-1)]+1;
for (rr int i=0;i<17;++i){
rr int now=pow(n,1.0/prime[i]),sum=now-1;
if (now<2) break;
for (rr int j=1;j<(1<<i);++j){
rr int G=1,flag=1;
for (rr int k=0;k<i;++k)
if ((j>>k)&1){
if (G>now/prime[k]) {flag=0; break;}
G*=prime[k];
}
if (!flag) continue;
rr int t=pow(now,1.0/G)-1;
if (t<1) continue;
sum+=(xo[j]&1)?-t:t;
}
ans+=sum;
}
return !printf("%d",ans+1);
}

nssl 1460 逛机房


分析

以完全平方数为源点广搜预处理所有答案,

原来的删除变成了添加,要注意0以及不能删掉数字


代码

#include <cstdio>
#include <cctype>
#include <cstring>
#define rr register
using namespace std;
const int N=1000011;
int dis[N],q[N],ox[N],head,tail;
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
inline void doit(int y,int x){
if (dis[y]>dis[x]+1&&ox[y]>=ox[x])
dis[y]=dis[x]+1,q[++tail]=y;
}
signed main(){
memset(dis,42,sizeof(dis)),head=1,tail=0,dis[0]=0;
for (rr int i=1;i<=1000;++i) dis[i*i]=0,q[++tail]=i*i;
for (rr int i=10;i<N;i*=10) ox[i]=1; --tail;
for (rr int i=11;i<N;++i) ox[i]+=ox[i-1];
while (head<=tail){
rr int x=q[head++];
for (rr int i=0;i<10;++i)
for (rr int j=0;j<6;++j)
switch (j){
case 0:doit(x/10*10+i,x); break;
case 1:doit((x/100*10+i)*10+(x%10),x); break;
case 2:doit((x/1000*10+i)*100+(x%100),x); break;
case 3:doit((x/10000*10+i)*1000+(x%1000),x); break;
case 4:doit((x/100000*10+i)*10000+(x%10000),x); break;
case 5:doit(i*100000+(x%100000),x); break;
}
if (x>99999) continue;
for (rr int i=0;i<10;++i) doit(x*10+i,x),doit((x/10*10+i)*10+(x%10),x);
for (rr int i=0;i<10;++i) doit((x/100*10+i)*100+(x%100),x);
for (rr int i=0;i<10;++i) doit((x/1000*10+i)*1000+(x%1000),x);
for (rr int i=0;i<10;++i) doit((x/10000*10+i)*10000+(x%10000),x);
for (rr int i=0;i<10;++i) doit(i*100000+(x%100000),x);
}
for (rr int Q=iut();Q;--Q)
print(dis[iut()]),putchar(10);
return 0;
}

#容斥,广搜#nssl 1458 HR的疑惑 nssl 1460 逛机房的更多相关文章

  1. [SCOI2010]幸运数字(容斥+爆搜)

    在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是 ...

  2. 【Luogu】P2567幸运数字(容斥爆搜)

    题目链接 先预处理出幸运数,把成倍数关系的剔掉,然后用容斥原理搜索一下. 这里的容斥很像小学学的那个“班上有n个同学,有a个同学喜欢数学,b个同学喜欢语文……”那样. #include<cstd ...

  3. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  4. 【做题】51NOD1518 稳定多米诺覆盖——容斥&dp

    题意:求有多少种方案,用多米诺骨牌覆盖一个\(n\times m\)的棋盘,满足任意一对相邻行和列都至少有一个骨牌横跨.对\(10^9+7\)取模. \(n,m \leq 16\) 首先,这个问题的约 ...

  5. 2019.01.17 bzoj1853: [Scoi2010]幸运数字(容斥+dfs)

    传送门 搜索菜题,然而第一次没有注意然后爆longlonglong longlonglong了. 题意:称所有数位由6,86,86,8组成的数为幸运数字,问一个一个区间[l,r][l,r][l,r]中 ...

  6. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. 【BZOJ1853】幸运数字(搜索,容斥)

    [BZOJ1853]幸运数字(搜索,容斥) 题面 BZOJ 洛谷 题解 成功轰下洛谷rk1,甚至超越了一个打表选手 这题思路很明显吧,先搞出来所有范围内的合法数字,然后直接容斥, 容斥的话显然没有别的 ...

  8. BZOJ1042:[HAOI2008]硬币购物(DP,容斥)

    Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...

  9. bzoj 1853 容斥 + 搜索

    思路:先把所有幸运数字找出来, 把没有用的去掉,然后爆搜容斥,因为最多只会搜十几个就超过限制了, 所以是可行的. #include<bits/stdc++.h> #define LL lo ...

  10. uoj#422. 【集训队作业2018】小Z的礼物(MIn-Max容斥+插头dp)

    题面 传送门 题解 好迷-- 很明显它让我们求的是\(Max(S)\),我们用\(Min-Max\)容斥,因为\(Min(S)\)是很好求的,只要用方案数除以总方案数算出概率,再求出倒数就是期望了 然 ...

随机推荐

  1. git回退至指定版本,并更新远程仓库

    1. git log   查到commit记录 2.复制 commit 后面的id 3. git reset --hard  commit 后面的id   // 回退 4. 强制更新远程仓库  git ...

  2. Singularity容器

    """参考文档 https://apptainer.org/user-docs/master/build_a_container.html ""&qu ...

  3. matplotlib画图中x轴过于密集的解决办法

    import matplotlib.ticker as ticker ax.xaxis.set_major_locator(ticker.MultipleLocator(base=10)) # tic ...

  4. jupyterlab安装和优化

    说明 JupyterLab(官网https://jupyter.org)是一个交互式的代码编辑器,打开它会打开一个网页,可以在其中编写代码,即时执行,快速得到结果(包括代码返回值.统计图和界面交互图) ...

  5. Hello-FPGA CoaXPress 2.0 FPGA DEVICE IP Core Demo

    Hello-FPGA CoaXPress 2.0 Device FPGA IP Core Demo 1     说明 本手册针对Helllo-FPGA的CoaXPress 2.0 DEVICE FPG ...

  6. 游戏H5引擎Canvas屏幕自适应CSS代码

    canvas.style = `touch-action: none; width:${ width }px; height:${ height }px; cursor: inherit;`;

  7. 【Azure 应用服务】应用服务连接 Azure MySQL 一直失败,报错 Create connection error

    问题描述 App Service上部署的Java应用,连接 Azure Database for MySQL 失败.错误信息:Create connection error, url: jdbc:my ...

  8. 使用 Docker 部署 Fiora 在线聊天室平台

    一.Fiora 介绍 Fiora 简介 Fiora 是一款开源免费的在线聊天系统. GitHub:https://github.com/yinxin630/fiora Fiora 功能 注册账号并登录 ...

  9. dist目录打war包命令 jar -cvf yourName_web.war *

    进入dist目录 "build:war": "cd dist && jar -cvf ../yourName_web.war *",

  10. linux c 打印时间最简单的实例

    最简单的代码,能够解决最棘手的问题,才是解决工程师的需要: #include <stdio.h> #include <time.h> #include <unistd.h ...