HDU-5778 abs
来源:互联网 发布:知乎怎么注册 编辑:程序博客网 时间:2024/05/16 15:05
原题链接
abs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1260 Accepted Submission(s): 439
Problem Description
Given a number x, ask positive integer y≥2 , that satisfy the following conditions:
1. The absolute value of y - x is minimal
2. To prime factors decomposition of Y, every element factor appears two times exactly.
1. The absolute value of y - x is minimal
2. To prime factors decomposition of Y, every element factor appears two times exactly.
Input
The first line of input is an integer T ( 1≤T≤50 )
For each test case,the single line contains, an integer x (1≤x≤1018 )
For each test case,the single line contains, an integer x (
Output
For each testcase print the absolute value of y - x
Sample Input
511124290871699579095
Sample Output
23656724470
设p1 = sqrt(x), p2 = p1 +1, P1向小方向枚举,p2向大方向枚举,找到一个p使p的每种质因子仅有一个且abs(p*p-x)达到最小.
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#include <map>#include <cmath>#define maxn 100005#define INF 1000000000using namespace std;typedef long long ll;int num[maxn], cnt;ll v[maxn];void prime(){for(ll i = 2; i < maxn; i++){if(num[i] == 0){v[cnt++] = i;}for(ll j = i * i; j < maxn; j += i) num[j] = 1;}}bool judge(ll p){if(p < 2) return false;for(int i = 0; i < cnt&& v[i] * v[i] <= p; i++){ll d = v[i] * v[i];if(p % d == 0) return false;}return true;}void solve(ll x){ll p1 = sqrt(x);ll p2 = p1 + 1;while(1){if(p1 >= 2 && x - p1 * p1 < p2 * p2 - x){if(judge(p1)){printf("%I64d\n", x - p1 * p1); return ;} p1--;}else{if(judge(p2)){printf("%I64d\n", p2 * p2 - x); return ;}p2++;}}}int main(){//freopen("in.txt", "r", stdin);int t;prime();scanf("%d", &t);while(t--){ll x;scanf("%I64d", &x);solve(x);} return 0;}
0 0
- hdu 5778 abs (数论)
- HDU 5778 abs
- HDU-5778 abs
- hdu 5778 abs
- hdu 5778 abs
- HDU-5778 abs(数论)
- HDU-5778 abs
- HDU 5778 abs
- HDU-5778-abs【思维】
- HDU 5778(abs 暴力)
- hdu 5778 abs【暴力枚举】
- HDU-5778-abs(暴力)
- HDU 5778 abs(数论)
- 【HDU】5778 - abs(思维)
- HDU 5778/BC 85C abs
- HDU 5778 abs (数学+暴力枚举)
- Hdu 5778 abs (暴力/二分)
- HDU 5778 abs 枚举 素数相关
- 网络编程释疑之:单台服务器上的并发TCP连接数可以有多少
- MTK平台Camera app中的模式切换概述
- yii的db解读
- java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/tv.danmaku.ijk.media.sample-2/lib/x86/libij
- OC 异常消息转发处理流程
- HDU-5778 abs
- js把毫秒转换为 xx小时xx分钟xx秒的通用方法
- 搜索(4):A*
- bulk collect
- binder学习笔记(十)—— 穿越到驱动层
- java.lang.NoSuchMethodException
- 工作流activity的学习总结
- 浅析nodejs实现Websocket的数据接收与发送
- uses-sdk:minSdkVersion 14 cannot be smaller than version 21 declared in library