C. Sad powers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You're given Q queries of the form (L, R).

For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = ap.

Input
The first line contains the number of queries Q (1 ≤ Q ≤ 105).

The next Q lines contains two integers L, R each (1 ≤ L ≤ R ≤ 1018).

Output
Output Q lines — the answers to the queries.

Example
input
Copy
6
1 4
9 9
5 7
12 29
137 591
1 1000000
output
2
1
0
3
17
1111
Note
In query one the suitable numbers are 1 and 4.

题目大意:

找到L—R之间的所有可以由x的p次幂所表示的数

思路:

虽然暴力是个好东西,但wa题也是很容易。

最初的想法是:遍历每个数字的每次幂,但很明显是行不通的。有趣的是,走不通的路,也还要试一下看看能过几个样例。然后wa在了第三个样例上。。。有兴趣可以看一眼真·暴力代码。如下:

 #include<stdio.h>
#include<string.h>
int vis[]; typedef long long ll; ll push_pow(ll a,ll b)
{
ll ans=;ll base=a;
while(b)
{
if(b%) ans*=base;
base*=base;
b/=;
}
return ans;
} ll slove(ll l,ll r)
{
memset(vis,,sizeof(vis));
int flag=;ll ans1=,ans2=;
for(int i=;flag;i++)
{
for(int j=;;j++)
{
if(l>=push_pow(i,j)&&!vis[push_pow(i,j)])
{
ans1++;
}
if(r>=push_pow(i,j)&&!vis[push_pow(i,j)])
{
vis[push_pow(i,j)]=;ans2++;
}
else if(j==&&!vis[push_pow(i,j)])
{
flag=;
break;
}
else break;
}
}
return ans2-ans1;
} int main()
{
ll T,l,r;
scanf("%lld",&T);
for(int o=;o<=T;o++)
{
scanf("%lld%lld",&l,&r);
if(l==) printf("%lld\n",+slove(l-,r));
else printf("%lld\n",slove(l-,r));
}
return ;
}

真·暴力

暴力不是ac的秘诀,那么这道题该怎么解决呢?在思考无果的情况下,百度。。。

事实它告诉我,这题不是我原先可以解决的。于是乎,涨知识了。

言归正传,我们可以把问题分割,分成两部分,一部分是二次方,一部分是p次方(p>2)。

二次方不必说,二分查找很容易解决问题。

主要是p次方,当p=3时,1e18开三次根号1e6,复杂度在可接受范围内。

而当p>3时,由于幂函数的增幅极大,所以到大于1e18花费的时间是很少的,以最多次幂的2举例,1e18没有爆longlong,那么次数少于64,很明显,花费也是可以接受的。

AC代码如下:

 #include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; typedef long long ll;
vector<ll>q; int root(ll x) //找根
{
ll left=;ll right=1e9; //left必须从0开始,因为调用函数时会有x==0的情况
ll mid,ans;
while(left<=right)
{
mid=(left+right)>>;
if(mid*mid<=x)
{
ans=mid;
left=mid+;
}
else
{
right=mid-;
}
}
return ans;
} void init() //p>2时,可以由x的p次方表示的数
{
q.clear();
for(ll i=;i<=1e6;i++)
{
double t=1.0*i*i*i;
ll s=i*i*i;
while(t<2e18)
{
ll root_s=root(s);
if(root_s*root_s<s)
q.push_back(s);
t*=i;s*=i;
}
}
sort(q.begin(),q.end()); //下面会解释
unique(q.begin(),q.end());
} int main()
{
init();
ll T;
ll l,r;
scanf("%lld",&T);
for(int i=;i<=T;i++)
{
scanf("%lld%lld",&l,&r);
ll ans1=upper_bound(q.begin(),q.end(),r)-lower_bound(q.begin(),q.end(),l);//下面会解释
ll ans2=root(r)-root(l-);
printf("%lld\n",ans1+ans2);
}
return ;
}

unique()函数 

sort(q.begin(),q.end());
unique(q.begin(),q.end());   

独一无二的,该函数的作用是把相邻的两个相同的数字中消去一个,让我想起了天梯赛的1-8(AI牛批!

相邻的两个相同的数字,那么sort()函数的作用就体现在为unique()函数服务上了。

upper_bound()与lower_bound()函数

ll ans1=upper_bound(q.begin(),q.end(),r)-lower_bound(q.begin(),q.end(),l);

upper_bound()作用是返回[l,r)中第一个大于某个数字的元素地址,lower_bound()的作用是返回[l,r)中第一个大于等于某个数字的元素地址。

CodeForce-955C的更多相关文章

  1. codeforce 955c --Sad powers 思路+二分查找

    这一题的题意是   定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...

  2. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  3. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  4. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  5. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  6. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  7. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  8. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  9. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  10. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

随机推荐

  1. redis集群部署+节点端口修改+数据恢复

    环境:OS:Centos 7Redis: 3.2.11主 从192.168.1.118:7001 192.168.1.118:8001192.168.1.118:7002 192.168.1.118: ...

  2. gjt常用命令---chalee

    Git常用命令 一. git 基本操作流程 1. 从远程分支拉取并创建新的分支 git pull origin [远程分支名]:[本地分支名] // 从远程分支迁出本地分支,并切换到新的本地分支 gi ...

  3. 2018年-2019年第二学期第五周C#学习个人总结

    在本周我学习了第五章面向对象高级中的5.3多态,5.3多态中主要包括重写父类方法,多态的实现,base关键字,里氏转换原则,Object类.在重写父类方法中要求当重写父类的方法时,要求子类的方法名,参 ...

  4. 在Pycharm中使用Pandas时输出结果中列被省略的解决办法

    在使用pycharm学习pandas的过程中我发现好多时候会发生不能输出所有列的情况,上网搜了一下,发现解决的办法是使用一个输出控制的函数. 在下面的代码中我们只是输出starbucks_store_ ...

  5. P4717 【模板】快速沃尔什变换

    思路 FWT的模板 FWT解决这样的卷积 \[ C_k=\sum_{i\otimes j=k} A_iB_j \] \(\otimes\)可能是and,or,xor等位运算 几个式子 FWTand: ...

  6. error_Could not load file or assembly

    原文链接 Could you be missing the loaded assembly from your configuration file? Ensure you have somethin ...

  7. CentOS7攻克日记(三) —— 安装Python3.6

    我是在EVERNOTE上面写的,本来格式是有代码段的,结果复制上来就没有了,所以会有一点乱,我就不调整了   我主要安装的是python环境,这一篇主要解决一下python的问题.在这里给个建议,安装 ...

  8. Unity3d外包团队:Unity3d最新版本更新内容

    GPU Instancing Improvement 只能改进了一些功能吧,原GPU Instancing shader可参考 https://docs.unity3d.com/Manual/GPUI ...

  9. libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

    In Xcode 9 and Swift 4: Print exception stack to know the reason of the exception: Go to show break ...

  10. 关于在html中直接引入less文件遇到的小问题

    由于想直接在html页面上调用less文件,所以直接在代码上直接引入 <script src="bower_components/less/dist/less.js"> ...