【HDU】5778 - abs(思维)
来源:互联网 发布:淘宝装修生成 编辑:程序博客网 时间:2024/06/10 01:04
点击打开题目
abs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1359 Accepted Submission(s): 477
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
Source
BestCoder Round #85
妈呀好紧张,bc结束之前5分钟AC了。
他要每个质因数个数都为2,那么开一下根号,就是说这个数质因数个数都为1,写一个函数就行了。
把最初的 n 开根号得 t ,如果 t * t == n 而且 t 不满足条件的话,那么分别从 t 的两边找,如果 t * t < n ,那么从 t + i 和 t - i + 1找( i 从 0 开始)。
代码如下:(刚开始总是wa,小情况考虑不到,所以代码比较复杂,思路还是清楚的)
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))bool check(__int64 x){ if (x == 1) return false; for (int i = 2 ; i <= sqrt(x) ; i++) { if (x % i == 0) { x /= i; if (x % i == 0) return false; } } return true;}int main(){ int u; __int64 n; __int64 res; scanf ("%d",&u); while (u--) { scanf ("%I64d",&n); if (n == 1) { printf ("3\n"); continue; } __int64 ans = sqrt(n); if (check(ans) && ans * ans == n) { printf ("0\n"); continue; } if (ans * ans != n) { for (int i = 1 ; ; i++) { __int64 t1 = 0,t2 = 0; if (check(ans+i)) { res = (ans+i)*(ans+i); res -= n; t1 = abs(res); } if (check(ans-i+1)) { res = (ans-i+1)*(ans-i+1); res -= n; t2 = abs(res); } if (t1 && t2) { res = min (t1,t2); break; } else if (t1 || t2) { res = max (t1,t2); break; } } } else { for (int i = 0 ; ; i++) { __int64 t1 = 0,t2 = 0; if (check(ans+i)) { res = (ans+i)*(ans+i); res -= n; t1 = abs(res); } if (check(ans-i)) { res = (ans-i)*(ans-i); res -= n; t2 = abs(res); } if (t1 && t2) { res = min (t1,t2); break; } else if (t1 || t2) { res = max (t1,t2); break; } } } printf ("%I64d\n",abs(res)); } 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 abs(数论)
- HDU-5778 abs
- HDU 5778 abs
- hdu 5778 abs【暴力枚举】
- 《Java源码分析》:Condition
- 关于Android主线程(ActivityThread)源代码分析以及一些特殊问题的非常规方法
- Qt 设置全局窗口,
- sql server 2008 R2 x86下载及安装说明
- JVM---第一篇内存区域划分
- 【HDU】5778 - abs(思维)
- 设计模式之建造模式
- Android TextView的垂直滚动效果
- BZOJ 1031 [JSOI2007]字符加密Cipher
- linux兴趣小组暑假第六次讲座总结
- AndroidUI组件-LinearLayout 线性布局
- 【JavaScript】JS实现继承最常用的方法
- Activity中的onCreate()
- 在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题(原文:http://www.cnblogs.com/izhanjun/p/)