hdu-5778 abs(暴力枚举)

news/2024/7/7 19:47:05

题目链接:

abs

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 131072/131072 K (Java/Others)


Problem Description
Given a number x, ask positive integer y2, 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 ( 1T50)
For each test case,the single line contains, an integer x ( 1x1018)

 

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
题意:
给一个x,然后让你找一个y,分解质因子后y的质因子次幂均为2,使abs(y-x)最小;
思路:
枚举x周围的那些数,看是否符合条件然后更新答案就好了;
AC代码:
/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
************************************************ */  #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));typedef  long long LL;template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n');
}const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e5+4;
const double eps=1e-8;int cnt,vis[N],prime[N];
inline void Init()
{cnt=0;For(i,2,maxn){if(!vis[i]){for(int j=2*i;j<maxn;j+=i)vis[j]=1;prime[++cnt]=i;}}
}inline int check(LL  x)
{for(int i=1;i<=cnt;i++){if(x<prime[i])break;if(x%prime[i]==0){x=x/prime[i];if(x%prime[i]==0)return 0;}}return 1;
}int main()
{       int t;read(t);Init();while(t--){LL n;read(n);LL temp=sqrt(n+0.5),ans=inf;int flag=0;for(LL i=0; ;i++){if(check(temp+i)&&(temp+i)*(temp+i)>=2){ans=min(ans,abs((temp+i)*(temp+i)-n));flag=1;}if(check(temp-i)&&temp>i&&(temp-i)*(temp-i)>=2)ans=min(ans,abs((temp-i)*(temp-i)-n)),flag=1;if(flag&&i>=6)break;}cout<<ans<<"\n";}return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5722733.html


http://lihuaxi.xjx100.cn/news/267123.html

相关文章

验证ArrayList是线程不安全的集合

package collectionSafe;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Test { /* 在本例中&#xff0c;验证ArrayList是线程不安全的容器 主要思路&#xff1a; 循环同时开启1000个线程&#xff0c;每个线程的作用…

Windows系统下的 vbs病毒生成器使用方法!

1&#xff0c;压缩包里的东西~ 下载并解压“病毒生成器”压缩文件&#xff0c;打开对应的文件夹&#xff0c;双击"vir1.exe" 应用程序图标。 下载时杀毒软件会自动将其识别为病毒&#xff0c;建议关闭主机上运行的杀毒软件 双击软件启动~&#xff1a;如果有杀毒软件…

深度学习在不同领域的应用,我去,这也行!?

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达作者丨Giant知乎&#xff08;已授权&#xff09;来源丨https://www.zhihu.com/question/47563637/answer/1997061212编辑丨极市平台导读 本文介绍几款深度学习领域的有趣应用…

XShell连接Deepin

为什么80%的码农都做不了架构师&#xff1f;>>> 先让deepin安装openssh sudo apt-get install openssh-serverchkconfig ssh on 转载于:https://my.oschina.net/enzo/blog/110518

archlinux常用的包管理器

pacman -Sy abc 和源同步后安装名为abc的包 pacman -S abc 从本地数据库中得到abc的信息&#xff0c;下载安装abc包 pacman -Sf abc 强制安装包abc pacman -Ss abc 搜索有关abc信息的包 pacman -Si abc 从数据库中搜索包abc的信息 pacman -Syu 同步源&#xff0c;并更新系统 pa…

怎样用Python控制图片人物动起来?一文就能Get!

作者 | 李秋键责编 | 李雪敬头图 | CSDN 下载自视觉中国出品 | AI科技大本营&#xff08;ID&#xff1a;rgznai100&#xff09;引言&#xff1a;近段时间&#xff0c;一个让梦娜丽莎图像动起来的项目火遍了朋友圈。而今天我们就将实现让图片中的人物随着视频人物一起产生动作。…

逻辑回归api介绍

逻辑回归api介绍 sklearn.linear_model.LogisticRegression(solverliblinear, penalty‘l2’, C 1.0) 【solver是用什么样的梯度下降算法来解决】 solver可选参数:{liblinear, sag, saga,newton-cg, lbfgs}&#xff0c; 默认: liblinear&#xff1b;用于优化问题的算法。 对于…

一文读懂机器学习模型评估

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文对机器学习模型评估指标进行了完整总结。机器学习的数据集一般被划分为训练集和测试集&#xff0c;训练集用于训练模型&#xff0c;测试集则用于评估模型。针对不同的…