总时间限制:
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. java的缓冲流及使用Properties集合存取数据(遍历,store,load)

    缓冲流 概述 字节缓冲流:BufferedInputStream,BufferedOutputStream 字符缓冲流:BufferedReader,BufferedWriter 缓冲流原理 缓冲区是 ...

  2. day12 keepalived高可用

    day12 keepalived高可用 一.高可用介绍 1.什么是高可用 部署在整个集群中的一个高可用软件,作用是创建一个VIP(虚拟IP),在整个集群中有且只有一个机器上生成VIP,当这台机器出现问 ...

  3. 设计和实现OLAP解决方案 [转]

    第一讲 简介首先,啥叫数据仓库? 数据仓库就是数据的仓库!用外文说叫Data Warehouse,简称DW. 是不是哐当倒下一片啊,要不咱换个专业点的说法? 数据仓库是一个面向主题的.集成的.相对稳定 ...

  4. Redis集群的三种模式

    一.主从模式 通过持久化功能,Redis保证了即使在服务器重启的情况下也不会损失(或少量损失)数据,因为持久化会把内存中数据保存到硬盘上,重启会从硬盘上加载数据. 但是由于数据是存储在一台服务器上的, ...

  5. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  6. 12.Vue.js 表单

    这节我们为大家介绍 Vue.js 表单上的应用. 你可以用 v-model 指令在表单控件元素上创建双向数据绑定. <div id="app"> <p>in ...

  7. SQL注入 (1) SQL注入类型介绍

    SQL注入 SQL注入介绍与分类 1. 什么是sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2. sql注入类型 按照注入 ...

  8. 1945-祖安say hello-string

    1 #include<bits/stdc++.h> 2 char str[100][40]; 3 char s[1005]; 4 5 int remark[2000][2] = { 0 } ...

  9. Mysql解决主从慢同步问题

    目录 一.简介 为何有延迟 二.观察 三.解决办法 参数 多线程 组提交 一.简介 一般主从复制,有三个线程参与,都是单线程:Binlog Dump(主) ----->IO Thread (从) ...

  10. mysql 连接数过多内存增长过快

    mysql连接允许长连接和短链接,但是本身建立连接会有很大开销所以一般连接mysql会使用长连接.但是全部是使用长连接后,可能会出现mysql有时占用内存涨的特别快,这是因为mysql在执行过程中临时 ...