求一个最小的正整数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. Poj 2503 Babelfish(Map操作)

    一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...

  2. Ubuntu下安装软件

    在ubuntu当中,安装应用程序有三种方法,分别是:apt-get,dpkg安装deb和make install安装源码包三种. apt-get方法 使用apt-get install来安装应用程序算 ...

  3. Webpack打包之后[-webkit-box-orient: vertical]样式丢失

    背景:项目是用的vue全家桶套餐 今天在工作中遇到一个问题,需求是要求文字只能显示3行,超过3行则隐藏且显示 '...', 于是我加了如下样式在标签里面: display: -webkit-box;- ...

  4. Python-requests取消SSL验证的警告InsecureRequestWarning解决办法

    使用requests模块请求一个证书无效的网站的话会直接报错 可以设置verify参数为False解决这个问题 # -*- coding:utf-8 -*- __author__ = "Mu ...

  5. Matlab2012a下配置LibSVM—3.18

    1.下载最新版LibSVM 点击此处打开网页,点击zip file下载最新版的文件并解压放在任何目录下,建议放在安装目录便于查找.如我的文件解压在路径C:\ProgramFiles\MATLAB\R2 ...

  6. c# Aspose.Words插入饼图PieChart

    private static void Main(string[] args) { Document doc = new Document(); DocumentBuilder builder = n ...

  7. p1197&bzoj1015 星球大战

    传送门(洛谷) 传送门(bzoj) 题目 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的 ...

  8. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  9. NPOI office操作

    写excel FileStream file = new FileStream(@"test.xls",FileMode.Create); hssfworkbook.write(f ...

  10. 【并发编程】Future模式添加Callback及Promise 模式

    Future Future是Java5增加的类,它用来描述一个异步计算的结果.你可以使用 isDone 方法检查计算是否完成,或者使用 get 方法阻塞住调用线程,直到计算完成返回结果.你也可以使用  ...