CodeForce-955C
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的更多相关文章
- codeforce 955c --Sad powers 思路+二分查找
这一题的题意是 定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
- codeforce 367dev2_c dp
codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- 强连通分量&hdu_1269&Codeforce 369D
强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...
- 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
随机推荐
- Intellij IDEA 远程链接hive,执行hsql
https://note.youdao.com/share/?id=75d82d9c5fab1e00de41c504a54dfcde&type=note#/
- Nuget安装包
Selenium.WebDriver Selenium.WebDriver.ChromeDriver Selenium.Firefox.WebDriver Selenium.WebDriver.IED ...
- javascript prop和attr的区别
在网上找了一些资料,通过自己去寻找,尝试,此次做一次总结. 1/ attr()是jQuery 1.0版本就有的函数, prop()是jQuery 1.6版本新增的函数.毫无疑问,在1.6之前, ...
- 论文笔记:AdaScale: Towards real-time video object detection using adaptive scalingAdaScale
AdaScale: Towards real-time video object detection using adaptive scaling 2019-02-18 16:14:17 Paper: ...
- Markdown简记
一篇好的博客文档离不开一个优秀的编辑器.借此篇文章介绍一下编写该博客markdown的语法,方便大家写出更好更漂亮的文档.重点是点击左上角的Github链接,有主题源码哦 一篇好的博客文档离不开一个优 ...
- JS构造函数、原型对象、隐含参数this
This 解析器再调用函数每次都会向函数内部传递一个隐含的参数this,this指向的是一个对象(函数执行的上下文对象) 1.以函数形式调用时,this永远是window. 2.以方法形式调用时,th ...
- HDU 1560 DNA sequence(DNA序列)
HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- C# 批量新增的两种方法。
public class Test { private static readonly string strConnection = ""; public static void ...
- 动态 DP 学习笔记
不得不承认,去年提高组 D2T3 对动态 DP 起到了良好的普及效果. 动态 DP 主要用于解决一类问题.这类问题一般原本都是较为简单的树上 DP 问题,但是被套上了丧心病狂的修改点权的操作.举个例子 ...
- Unity自定义定时器,模拟协程,脱离MonoBehavior控制
using System; using System.Collections.Generic; using System.Timers; public class PETimer { private ...