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. 算法(第四版)C# 习题题解——2.1

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  2. 搭建Flutter-iOS开发环境

    https://www.jianshu.com/p/b50a92afbef1 1.获取Flutter工程 git clone -b beta https://github.com/flutter/fl ...

  3. Android文件各种存储路径的比较

    1.File cacheDir = context.getCacheDir(); 应用内部存储空间(数据文件私有)文件存储到这个路径下,不需要申请权限,当应用被卸载的时候,目录下的文件会被删除. 需要 ...

  4. C#Razor模板引擎简单使用

    引用 install-package RazorEngine 使用 public class TestDemo { private string name; public int Age { get ...

  5. (转)AutoML for Data Augmentation

    AutoML for Data Augmentation 2019-04-01 09:26:19 This blog is copied from: https://blog.insightdatas ...

  6. tail -f 报错 file truncated

    操作: 循环覆盖向tmp 文件写入坐标 tmp: -45.6976089525,-26.1528715421,-0.0188627654187 报错如下: -15.2517398838,-5.1216 ...

  7. 编译原理中DFA最小化

    关于编译原理最小化的操作,专业术语请移步至:http://www.360doc.com/content/18/0601/21/11962419_758841916.shtml 这里只是记录一下个人的理 ...

  8. 自定义 Cordova插件详解

    一.Cordova的基础点 在混合式应用中,我们通过现有的Cordova插件,可以轻松的在 H5 上调用手机native的功能.现有的Cordova插件能满足平时大部分的开发需求,然而,有时候找不到合 ...

  9. python函数注释,参数后面加冒号:,函数后面的箭头→是什么?

    https://blog.csdn.net/sunt2018/article/details/83022493

  10. Windows server2008服务器设置多用户登录

    添加用户 右击我的电脑-->管理-->本地用户和组-->新用户 启用远程服务并添加远程用户 启用 右键我的电脑--->属性--->远程设置--->勾上允许远程连接到 ...