hdu 5778 abs(分解质因子,枚举平方数,数论)
2016-07-30 22:37
447 查看
abs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 157 Accepted Submission(s): 63
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.
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)
Output
For each testcase print the absolute value of y - x
Sample Input
5
1112
4290
8716
9957
9095
Sample Output
23
65
67
244
70
Source
BestCoder Round #85
题意:给你x求一个y满足abs(y-x)最小并且y分解质因子后所有的指数都是2,输出abs(y-x)
思路:由于y质因数分解式中每个质因数均出现2次,那么y是一个完全平方数,设y=z*z,题目可转换成求z,使得每个质因数出现1次. 我们可以暴力枚举z,检查z是否符合要求,显然当z是质数是符合要求,由素数定理可以得,z的枚举量在logn级别
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int devide(long long n) { int cnt=0; for(long long i=2; i*i<=n; i++) { if(n%i==0) { int num=0; while(n%i==0) { num++; n/=i; if(num>1) return 0; } if(num!=1) return 0; } } return 1; } long long solve(long long n) { if(n<=4) return 4-n; long long t=sqrt(n),m,l,ans; for(l=0;; l++) { m=t+l; if(m*m>=n&&devide(m)) { ans=abs(m*m-n); break; } } long long k=n-ans; for(long long i=0;;i++) { m=t-i; if(m*m<=k) break; if(devide(m)) ans=min(ans,abs(m*m-n)); } return ans; } int main() { int T; long long n; scanf("%d",&T); while(T--) { scanf("%I64d",&n); printf("%I64d\n",solve(n)); } return 0; }
相关文章推荐
- hdu 5778 abs (数论)
- HDU-5778 abs(数论)
- HDU 5778 abs(数论)
- hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
- HDU --- 5778 abs 【枚举平方数 + 思维】
- hdu 1299 数论 分解素因子
- HDU-5778-abs【思维】
- 【HDU】4135 Co-prime(容斥原理简单应用 质因子的分解)
- hdu1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 各种模板
- HDU 5778(abs 暴力)
- HDU - 4135Co-prime 容斥 分解质因子
- hdu-5778 abs(暴力枚举)
- HDU 3826 Squarefree number (分解质因子 + 巧思)
- Hdu 5778 abs (暴力/二分)
- hdu-4344-Mark the Rope-大数分解质因子模板
- 2017 Multi-University Training Contest - Team 4 :Counting Divisors(数论:素数筛选+分解质因子+求因子数)
- HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
- HDU 5778 abs (数学+暴力枚举)
- HDU 5428-The Factor(分解质因子)
- hdu 5505 GT and numbers(分解质因子)