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. Android中在不同activity中进行自定义广播的解析

    相信有不少人和我一样曾经尝试过在同一个项目中的两个activity进行广播,发现怎么都实现不了.我也是困惑了好久才发现,这么搞本来就是不行的.首先在同一个项目下不同的activity之间广播没有意义, ...

  2. 调参贝叶斯优化(BayesianOptimization)

    from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score ...

  3. vue v-if:"TypeError: Cannot read property 'length' of undefined"

    在使用v-if判断一个数组大小为0时,会出现 length 是undefined的错误:[Vue warn]: Error in render: "TypeError: Cannot rea ...

  4. loadrunner常用函数集锦

    一.三个复制函数的区别: strcpy 原型:extern char *strcpy(char *dest,char *src);用法:#i nclude功能:把src所指由NULL结束的字符串复制到 ...

  5. 工具类封装之--CommonUtils

    /** * @file_name : CommonUtils.java * @author : * @date : 2018年3月15日 * Description: */ package cn.xx ...

  6. 【HNOI 2019】校园旅行

    Problem Description 某学校的每个建筑都有一个独特的编号.一天你在校园里无聊,决定在校园内随意地漫步. 你已经在校园里呆过一段时间,对校园内每个建筑的编号非常熟悉,于是你情不自禁的把 ...

  7. windows服务器环境问题---api-ms-win-crt-runtimel1-1-0.dll缺失解决

    安装VC++库 地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=48145

  8. Lintcode155-Minimum Depth of Binary Tree-Easy

    155. Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is ...

  9. Ubuntu: 安装自带的中文输入法

    1. 在终端安装输入法软件包 在终端输入 sudo apt-get install ibus-pinyin 2. 启用输入法 打开设置 找到Region&Language 找到Input So ...

  10. L1范数与L2范数​

    L1范数与L2范数​ ​ L1范数与L2范数在机器学习中,是常用的两个正则项,都可以防止过拟合的现象.L1范数的正则项优化参数具有稀疏特性,可用于特征选择:L2范数正则项优化的参数较小,具有较好的抗干 ...