求一个最小的正整数x,使得(y + x) (y - x) = n成立

考虑一下n的分解因式。

可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可

但是要枚举到n / 2,这样会超时。

因为要使得a * b = n,那么a和b中最大的数字最多是sqrt(n),因为不可能是两个大于sqrt(n)的数字相乘得到n的(大过n了)

所以我可以枚举 1 -- sqrt(n)中n的约数,得到a和b,然后反转一下a和b,就是所有a * b = n的结果

例如18的约数

1、2、3、6、9、18

枚举到sqrt(18) = 4即可

当然这题不用反转。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
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> void work() {
int n;
scanf("%d", &n);
int t = sqrt(n * 1.0);
int ans = inf;
for (int i = ; i <= t; ++i) {
if (n % i != ) continue;
int a = n / i;
int b = i;
if ((a - b) & ) continue;
if (a == b) continue;
ans = min(ans, (a - b) / );
}
if (ans == inf) {
printf("-1\n");
} else {
printf("%d\n", ans);
}
return;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) {
work();
}
return ;
}

HDU 4143 A Simple Problem 分解因式的更多相关文章

  1. 【数论】HDU 4143 A Simple Problem

    题目内容 给出一个正整数\(n\),找到最小的正整数\(x\),使之能找到一个整数\(y\),满足\(y^2=n+x^2\). 输入格式 第一行是数据组数\(T\),每组数据有一个整数\(n\). 输 ...

  2. HDU 4143 A Simple Problem(枚举)

    题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...

  3. hdu 4143 A Simple Problem (变形)

    题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/ ...

  4. HDU 4143 A Simple Problem 题解

    题目 For a given positive integer n, please find the saallest positive integer x that we can find an i ...

  5. HDU 4267 A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. HDU 4267 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 2522 A simple problem (模拟)

    题目链接 Problem Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第 ...

  8. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  9. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

随机推荐

  1. 使用ceph命令提示handle_connect_reply connect got BADAUTHORIZER

    输入命令提示如下错误: [root@node1 ~]# rados -p testpool ls 2017-10-21 06:13:25.743045 7f8f89b6d700 0 -- 192.16 ...

  2. WPF bmp和二进制转换

    bmp转二进制: FileStream fs = File.OpenRead(filepath); //filepath文件路径 Byte[] tempBuff = new Byte[fs.Lengt ...

  3. ML 徒手系列 最大似然估计

    1.最大似然估计数学定义: 假设总体分布为f(x,θ),X1,X2...Xn为总体采样得到的样本.其中X1,X2...Xn独立同分布,可求得样本的联合概率密度函数为: 其中θ是需要求得的未知量,xi是 ...

  4. C#动态给EXCEL列添加下拉选项

    Microsoft.Office.Interop.Excel.Application excel=new Microsoft.Office.Interop.Excel.Application(); M ...

  5. 问题:C#调webservice超时;结果:C#调用webservice服务超时

    C#调用WebService服务时,报错,The operation has timed out,意思是“操作超时”. 方法/步骤 首先修改服务端配置 WebService服务所在站点为服务端,它提供 ...

  6. 关于JAVA中的回调接口传值机制

    详见博文http://blog.csdn.net/xiaanming/article/details/8703708

  7. koa1创建项目

    1.一定要全局安装(koa1.2和koa2都己经支持)npm install koa-generator -g 2.koa1.2 生成一个test项目,切到test目录并下载依赖 koa testcd ...

  8. PCL 不同类型的点云之间进行类型转换

    PCL 不同类型的点云之间进行类型转换 可以使用PCL里面现成的函数pcl::copyPointCloud(): #include <pcl/common/impl/io.h> pcl:: ...

  9. HTML5-A*寻路算法

    设置起点 设置终点 设置障碍 清除障碍 允许斜向跨越

  10. 3、Linux下配置Java环境

    转载:http://blog.sina.com.cn/s/blog_c5a35e780102wtxl.html 生物信息很多软件都是用java写的,所以需要在linux上配置java运行环境.平台上的 ...