CodeChef - SQRGOOD:Simplify the Square Root (求第N个含平方因子数)
Tiny Wong the chef used to be a mathematics teacher in a senior high school. At that time, he always used to tell his students that when there is a square root of some number in one’s final result, it should be simplified by factoring out the largest square divisor of this number. For example, √ 12 = 2 √ 3 . Therefore, if an integer n has a square divisor, i.e. there is a number d > 1 such that d2 divides n, then the square root of n needs to be simplified.
Tiny himself should generate for homework some number whose square root needs simplifying. Since he used to major in Computer Science, he prefers random numbers. Therefore, he randomly chose a number n and decided to use the n-th smallest number whose square root needs simplifying in today’s homework.
Since the n-th such number is too large for him to deal with, Tiny Wong is lost in thought. Would you please help him?
Input
The first and only line of the input contains a single integer n.
Note: the number n is not fixed for each test case. Instead, it will be generated dynamically, so it may be different for each run of your program. For each test case, n is generated in the following way: we have two fixed numbers L and R; n will be chosen uniformly at random from all integers between L and R inclusive.
Note 2: Due to dynamically generated test cases, the problem is technically configured as interactive, thus reading until EOF will not work. Attempting to use any input method that expects EOF at the end will result in TLE verdict.
Output
Print a single line containing one integer — the n-th number whose square root needs simplifying.
Constraints
- 1 ≤
n
- ≤ 10
18
Subtasks
Subtask #1 (6 points): 1 ≤ n ≤ 107
Subtask #2 (17 points): 1 ≤ n ≤ 1014
Subtask #3 (27 points): 1 ≤ n ≤ 1016
Subtask #4 (50 points): 1 ≤ n ≤ 1018
Example
Input: 4 Output: 12
Explanation
The first 4 numbers whose square roots need simplifying are 4, 8, 9 and 12.
思路:见:https://blog.csdn.net/gjghfd/article/details/79137620 。
//求第N个含平方因子数时,可以把二分范围限制到如此,而筛不含平方因子数的时候,可以把上界限制到2N。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const double pi=acos(-1.0);
map<int,int>M;
int mu[maxn],mu2[maxn],p[maxn>>],cnt; bool vis[maxn];
void init()
{
mu[]=; mu2[]=;
rep(i,,maxn-){
if(!vis[i]) p[++cnt]=i,mu[i]=-;
for(int j=,t;j<=cnt&&(t=p[j]*i)<maxn;j++){
mu[t]=-mu[i]; vis[t]=; //少做几次乘法
if(!(i%p[j])) {mu[t]=; break;}
}
}
rep(i,,maxn-) mu2[i]=mu2[i-]+(!mu[i]?:),mu[i]+=mu[i-];
}
int musum(int x)//莫比乌斯前缀和
{
if(x<maxn) return mu[x];
if(M.count(x))return M[x];
int res=;
for(int i=,j;i<=x;i=j+){
int k=x/i; j=x/k;
res-=musum(k)*(j-i+);
}
return M[x]=res;
}
ll nonfsum(ll x) //无平方因子前缀和
{
if(x<maxn) return mu2[x];
ll i=,res=,lst=,R,t;
for(;i*i*i<=x;i++) res+=(x/(i*i))*(mu[i]-lst),lst=mu[i];
for(res-=(t=x/(i*i))*lst;t;t--) res+=musum(sqrt(x/t));
return res;
}
int main(){
init();
ll N,ans,l,r,Mid;
scanf("%lld",&N);
l=N/(-/pi/pi),r=l+,l-=; //大致范围
l=max(l,1LL);
while(l<=r){
Mid=l+r>>;
if(Mid-nonfsum(Mid)>=N) ans=Mid,r=Mid-;
else l=Mid+;
}
printf("%lld\n",ans);
return ;
}
CodeChef - SQRGOOD:Simplify the Square Root (求第N个含平方因子数)的更多相关文章
- Project Euler 80:Square root digital expansion 平方根数字展开
Square root digital expansion It is well known that if the square root of a natural number is not an ...
- Project Euler 57: Square root convergents
五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...
- Codeforces 715A. Plus and Square Root[数学构造]
A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 612E - Square Root of Permutation
E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
- Square Root
Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...
- Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题
A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...
- Plus and Square Root
ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ...
随机推荐
- dockfile
dockerfile是对镜像的描述 新建一个dockfile文件 docker inspect
- pyhton3 hashlib模块
hashlib模块提供一下常量属性 hashlib.algorithms_guaranteed 获取保证在所有平台上此模块支持的哈希算法名称的集合 hashlib.algorithms_availab ...
- ORACLE对象模式
在oracle数据库中,数据对象是以模式(Schema)为单位进行组织和管理的.所谓模式是指一系列逻辑数据结构或对象的集合. 模式与用户相对应,一个模式只能被一个数据库用户所拥有,并且模式名称与这个用 ...
- 树莓派连接DHT11温湿度传感器(python)
介绍 DHT11作为一个廉价配件,同时包含了温度.湿度传感器,而且,编码使用也非常简单. 本文介绍如果在树莓派中使用 DHT11,代码是Python.如果有任何疑问,欢迎在下面留言. 接线 VCC接5 ...
- vue项目的webpack设置请求模拟数据的接口方法
最近在跟着视频写饿了吗vue项目,其中模拟数据由于webpack版本变化,跟视频中不一致,下方博客有解决方案,其实视频里面的还能看懂,现在webpack的服务都在插件包里了,好难找. 请参考:http ...
- 13.常见模块re-正则模块
1.正则 正则表达式是计算机科学的一个概念,正则表通常被用来检索.替换那些符合某个模式(规则)的文本.也就是说使用正则表达式可以在字符串中匹配出你需要的字符或者字符串,甚至可以替换你不需要的字符或者字 ...
- Pytorch的gather用法理解
先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...
- asp.net 5 (mvc 6) 获取网站的物理路径
public static IApplicationEnvironment GetApplication(this RazorPage page) { var ae = page.Context.Re ...
- ASP.NET MVC 5 访问在views文件夹中的JS文件。 ASP.NET MVC html与JS分离
修改VIEWS文件夹下的web.config文件, 加入下面红色字标识的内容: <system.webServer> <handlers> <r ...
- Linux服务器注意事项
1.在Linux新建一个tomcat目录,执行里面的文件运行的时候 会出现权限不足的提示?解决办法:这是因为新建的文件夹,对于可执行脚本,必须先授权,进入bin目录后,执行命令 chmod 764 ...