1046:Square Number
- 总时间限制:
- 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,y1 #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的更多相关文章
- Square Number & Cube Number
Square Number: Description In mathematics, a square number is an integer that is the square of an in ...
- 山东省第六届省赛 H题:Square Number
Description In mathematics, a square number is an integer that is the square of an integer. In other ...
- SDUT 3258 Square Number 简单数学
和上一题一样,把平方因子除去,然后对应的数就变成固定的 #include <cstdio> #include <iostream> #include <algorithm ...
- HDU 2281 Square Number Pell方程
http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...
- Leetcode Perfect Square
这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...
- 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 ...
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- 山东省第六届ACM省赛 H---Square Number 【思考】
题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
随机推荐
- Oracle-除了会排序,你对ORDER BY的用法可能一无所知!
导读 为什么只有ORDER BY后面可以使用列别名 为什么不推荐使用ORDER BY后接数字来排序 为什么视图和子查询里面不能使用ORDER BY -- 小伙伴们在进行SQL排序时,都能很自然的使用 ...
- php操作mongodb手册地址
php操作mongodb手册地址: http://php.net/manual/zh/class.mongocollection.php
- docker可视化管理Portainer
Portainer是一款轻量级docker容器管理平台,占用资源少,支持集群,支持权限分配. $ docker volume create portainer_data$ docker run -d ...
- 日常Java 2021/9/29
StringBuffer方法 public StringBuffer append(String s) 将指定的字符串追加到此字符序列. public StringBuffer reverse() 将 ...
- 8. LINUX shell 环境变量
wc –l file 计算文件行数, wc -w file 计算文件中的单词数, wc -c file 计算文件中的字符数 查看文件内容: cat .more
- D3-更改x轴的标签
记录,上代码
- Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The c ...
- Reactor之发射器(Flux、Mono)转换操作函数
数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...
- Mysql资料 主键
目录 一.简介 二.操作 三.技巧 一.简介 主键意味着表中每一行都应该有可以唯一标识自己的一列(或一组列). 一个顾客可以使用顾客编号列,而订单可以使用订单ID,雇员可以使用雇员ID 或 雇员社会保 ...
- Jenkins配置java项目
目录 一.场景介绍 二.项目配置 配置插件 配置项目 一.场景介绍 在部署完Jenkins后,需要将现有的maven项目(Jenkis的开源插件),放到Jenkins上,用于自动化运维的改造. 项目地 ...