HDU 5778 abs 数学
http://acm.hdu.edu.cn/showproblem.php?pid=5778
这题的意思就是找离x最近的一个数y,且y是一个完全平方数,还是所有质因子都只能出现两次的完全平方数
一开始的思路是直接枚举这个差值,然后去两边找,val - res和val + res找,然后超时了。
其实也很正常,因为两个完全平方数的间隔实在太大了。中间有很多的数字,
那么把思路换一下,
题意是找a^2 <= x <= b^2这样子的东西,那么可以同时开方,a <= sqrt(x) <= b
这样找到的结果是一样的,然后就相当于找一个没有质因子出现过两次的数字了。
先来证明一下为什么筛素数到1e6就够了。我们要判断到1e9之内。
我们现在要找的是,不能出现多个相同质因子相乘出来的数,因为我们开方了嘛。那么,筛到1e6就够了。
对于小的素数,那么肯定可以啦,2 * 2 * ...那些肯定可以筛出来,那么大素数呢?
就是2 * bigprime <= 1e9的那些数。那些也是可以筛出来的,对于大于1e6的素数,我们确实没办法判断,但是如果它在
前面的1e6那里的小素数,都没有出现多个相同质因子相乘的话,那就是可以得了。筛不到的大质因子,就默认可以了。
因为没可能是两个bigPrime相乘的。爆了1e9了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + ;
int prime[maxn], total;
bool check[maxn];
void initprime() {
for (int i = ; i <= maxn - ; ++i) {
if (!check[i]) {
prime[++total] = i;
}
for (int j = ; j <= total; ++j) {
if (i * prime[j] > maxn - ) break;
check[i * prime[j]] = ;
if (i % prime[j] == ) break;
}
}
}
bool isok(LL val) {
if (val < ) return false;
bool flag = false;
for (int i = ; i <= total; ++i) {
if (val < prime[i]) break;
if (val % prime[i] == ) {
val /= prime[i];
if (val % prime[i] == ) return false;
flag = true;
// while (val % prime[i] == 0) {
// val /= prime[i];
// has++;
// if (has == 3) return false;
// flag = true;
// }
}
}
// if (val != 1) return false;
return true;
}
void work() { // big prime * big prime TLE && 2^6 not ok
LL val;
// scanf("%I64d", &val);
cin >> val;
LL tpos = val;
val = (LL)sqrt(val * 1.0);
LL mid = 2e18;
if (isok(val)) {
mid = val * val;
}
LL big = 2e18, small = 2e18;
for (int res = ; ; ++res) {
if (isok(val + res)) {
big = (val + res) * (val + res);
break;
}
}
for (int res = ; ; ++res) {
if (val - res < ) break;
if (isok(val - res)) {
small = (val - res) * (val -res);
break;
}
}
LL ans = min(abs(tpos - big), abs(tpos - small));
ans = min(ans, abs(tpos - mid));
// cout << big << " " << mid << " " << small << endl;
cout << ans << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
// IOS;
initprime();
// cout << isok(33) << endl;
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
HDU 5778 abs 数学的更多相关文章
- HDU 5778 abs (枚举)
abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...
- HDU 5778 abs (素数,暴力)
题意:给定一个数x,求正整数y≥2y\geq 2y≥2,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 析:由于y质因数分解式中每个质因数均出现2次,那么 ...
- HDU 5778 abs
题意转化一下就是寻找一个数P,要求P质因素分解完后,质因素没有重复,还要保证abs(P*P-x)最小. 暴力,在sqrt(x)附近向下向上分别枚举一下. #pragma comment(linker, ...
- HDU 5778 abs (暴力枚举)
abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem De ...
- HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...
- HDU 1030 Delta-wave 数学题解
给出一个数字塔,然后求沿着数字之间的边走,给出两个数字,问其路径最短的长度是多少. 看似一条搜索题目,只是有一定做题经验的人都知道,这个不是搜索题,直接搜索肯定超时. 这个是依据规律计算的数学题目. ...
- HDU 5673 Robot 数学
Robot 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 Description There is a robot on the origi ...
- HDU 5914 Triangle 数学找规律
Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...
- HDU 2493 Timer 数学(二分+积分)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2493 题意:给你一个圆锥,水平放置,圆锥中心轴与地面平行,将圆锥装满水,在圆锥某一表面开一个小洞,流出来 ...
随机推荐
- java获取class的几种方式
以获取Hello.class为例 public class Hello { public static void main(String[] args) { // TODO Auto-generate ...
- Docker and Go: why did we decide to write Docker in Go?
Docker and Go: why did we decide to write Docker in Go? | Hacker News https://news.ycombinator.com/i ...
- 关闭mongodb 集群
[root@hadoop1 ~]# ps -aux | grep mongodb; root ? Sl : : /usr/local/mongodb/bin/mongod -f /usr/local/ ...
- OOalv 实现带出栏位描述
.类定义 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR E ...
- (24) java web的struts2框架的使用-action参数自动封装与类型转换
structs可以对参数进行自动封装,做法也很简单. 一,action参数自动封装: 1,可以直接在action类中,声明public的属性,接受参数. 2,属性也是是private,如果是priva ...
- 关于JAVA中的前期绑定 后期绑定(动态绑定)
前期绑定,在程序执行前根据编译时类型绑定,调用开销较小,如C语言只有前期绑定这种方法调用. 后期绑定,是指在运行时根据对象的类型进行绑定,又叫动态绑定或运行时绑定.实现后期绑定,需要某种机制支持,以便 ...
- mysql优化----explain的列分析
sql语句优化: : sql语句的时间花在哪儿? 答: 等待时间 , 执行时间. 等待时间:看是不是被锁住了,那就不是语句层面了是服务端层面了,看连接数内存. 执行时间:到底取出多少行,一次性取出1万 ...
- python 将屏幕输出定向到变量中
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import subprocess as sp def main(): cmd = 'syst ...
- webpack与grunt/glub 的比较
webpack.grunt.glub 都是前端打包的工具: grunt/gulp 的工作方式是:在一个配置文件中,指明对某些文件进行压缩.组合.检查等任务的具体步骤,然后在运行中输入相应的命令. we ...
- [RK3288]PMU配置(RK808)【转】
本文转载自:http://www.javashuo.com/content/p-6398007.html 硬件原理 pmic 电路原理 平台概述 RK808 PWM 介绍 驱动分析 dts 驱动流程 ...