总时间限制:
1000ms
内存限制:
65536kB
描述

给定正整数b,求最大的整数a,满足a*(a+b) 为完全平方数

输入
多组数据,第一行T,表示数据数。对于每组数据,一行一个正整数表示b。
T <= 10^3, 1 <= b <= 10^9
输出
对于每组数据,输出最大的整数a,满足a*(a+b)为完全平方数
样例输入
3
1
3
6
样例输出
0
1

2
思路:a*(a+b);gcd(a,b) = gcd(a,a+b),这个符合辗转相除,然后a*(a+b)=gcd^2(a1)*(a1+b1),那么a1与a1+b1互质
然后a1必定为一个数的平方x1^2,(a1+b1)为某个数的平方y^2;那么gcd(x,y)=1,然后b1=(x+y)(x-y);那么a=gcd*x^2,b1为
b的因子,所以枚举b1,再枚举b1的因子解出x,y

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 using namespace std;
9 typedef long long LL;
10 LL gcd(LL n,LL m) {
11 if(m == 0)return n;
12 else return gcd(m,n%m);
13 }
14 LL slove(LL n);
15 int main(void) {
16 int T;
17 scanf("%d",&T);
18 while(T--) {
19 LL n;
20 scanf("%lld",&n);
21 printf("%lld\n",slove(n));
22 }
23 return 0;
24 }
25 LL slove(LL n) {
26 LL maxx = -1;
27 LL x = sqrt(1.0*n);
28 int i,j;
29 for(i = 1; i <= x ; i++) {
30 if(n%i==0) {
31 int x1 = i;
32 int x2 = n/i;
33 for(j = 1; j <= sqrt(1.0*x1); j++) {
34 if(x1%j == 0) {
35 LL k = x1/j;
36 //if(gcd(k,j)==1)
37 {
38 if((k+j)%2==0) {
39 LL xx = (k-j)/2;
40 LL yy = (k+j)/2;
41 if(gcd(xx,yy)==1)
42 maxx = max(maxx,xx*xx*x2);
43 }
44 }
45 }
46 }
47 for(j = 1; j <= sqrt(1.0*x2); j++) {
48 if(x2%j == 0) {
49 LL k = x2/j;
50 //if(gcd(k,j)==1)
51 {
52 if((k+j)%2==0) {
53 LL xx = (k-j)/2;
54 LL yy = (k+j)/2;
55 if(gcd(xx,yy)==1)
56 maxx = max(maxx,xx*xx*x1);
57 }
58 }
59 }
60 }
61 }
62 }
63 return maxx;
64 }



1046:Square Number的更多相关文章

  1. Square Number & Cube Number

    Square Number: Description In mathematics, a square number is an integer that is the square of an in ...

  2. 山东省第六届省赛 H题:Square Number

    Description In mathematics, a square number is an integer that is the square of an integer. In other ...

  3. SDUT 3258 Square Number 简单数学

    和上一题一样,把平方因子除去,然后对应的数就变成固定的 #include <cstdio> #include <iostream> #include <algorithm ...

  4. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  5. Leetcode Perfect Square

    这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...

  6. Interview Check If n Is A Perfect Square

    Check if a given number is a perfect square with only addition or substraction operation. eg. 25 ret ...

  7. hdu 6125 -- Free from square(状态压缩+分组背包)

    题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...

  8. 山东省第六届ACM省赛 H---Square Number 【思考】

    题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...

  9. HDU 6125 Free from square 状态压缩DP + 分组背包

    Free from square Problem Description There is a set including all positive integers that are not mor ...

随机推荐

  1. 基于 Helm 快速部署 Wordpress

    Helm 是 Kubernetes 中的一个开源软件包管理工具,Rainbond 从 5.3.1 版本开始支持部署 Helm 应用.实现 Helm 应用的便捷部署,访问控制.使 Rainbond 用户 ...

  2. 用python写的推箱子搜索程序

    1 # -*- coding: gbk -*- 2 from functools import reduce 3 from copy import deepcopy 4 import re 5 def ...

  3. day35前端基础之BOM和DOM

    day35前端基础之BOM和DOM BOM操作 简介 BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进行"对话". ...

  4. 如何删除苹果电脑垃圾文件-7个高级技巧释放大量苹果Mac

    硬盘空间用尽是一件很让人头疼的事情,尤其是MacBook Air等设备上的固态硬盘可用的储存空间很少.下面[微IT]为大家介绍7个高级技巧来释放大量的硬盘空间,当然这些高级技巧更改了系统功能和文件,必 ...

  5. C++ 素数对猜想

    我的解法是先将2到n的所有素数全部列出来,再计算.将全部的素数列出来用了一个叫"埃拉托色尼筛法"的方法. 算法参照这里:https://www.sohu.com/a/2526745 ...

  6. Android 利用Settings.Global属性跨应用定义标志位

    https://blog.csdn.net/ouzhuangzhuang/article/details/82258148 需求 需要在不同应用中定义一个标志位,这里介绍下系统级别的应用和非系统级别应 ...

  7. Oracle之DBMS_LOCK包用法详解

    概述与背景 某些并发程序,在高并发的情况下,必须控制好并发请求的运行时间和次序,来保证处理数据的正确性和完整性.对于并发请求的并发控制,EBS系统可以通过Concurrent Program定义界面的 ...

  8. 队列——Java实现

    1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...

  9. jquery总结和注意事项

    1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...

  10. 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge

    Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...