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-trunc函数、round 函数、ceil函数和floor函数---处理数字函数使用
0.round函数 按照指定小数位数进行四舍五入运算. SELECT ROUND( number, [ decimal_places ] ) FROM DUAL #number : 待处理数值 de ...
- 巩固javaweb的第二十六天
正则表达式 正则表达式提供了一种高级的.但不直观的字符串匹配和处理的方法.它描述了一种 字符串匹配的模式,可以用来判断一个字符串是否满足某种格式,或者一个字符串是否含 有某个子串等. 1. 字符集 正 ...
- adult
adult是adolescere (grow up)的过去分词. egg - embryo [胚胎] - foetus [就要出生的胎儿] - toddler [刚会走路] - adolescent ...
- 移动开发之h5学习大纲
移动开发学习形式:授课.自学 1.html5 css3 htm5shiv.js response.js 2.流式布局 自适应布局 盒模型 弹性盒模型 响应式布局3.iscroll swiper boo ...
- java_IO总结(一)
所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...
- ArrayList删除特定元素的方法
最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...
- 【Linux】【Shell】【Basic】Programming
shell脚本编程: 编程语言的分类:根据运行方式 编译运行:源代码-->编译器(编译)-->程序文件 解释运行:源代码-->运行时启动解释器,又解释器边解释边运行 根据其编程过程中 ...
- JUC概述
JUC概述1: 首先是进程和线程的概念: 进程:是指系统在系统中正在运行的一个应用程序,程序一旦运行就是进程,进程是资源分配的最小单位 线程:进程之内独立执行,是程序执行的最小单位 线程的六大状态:在 ...
- 基于Kubernetes的hpa实现pod实例数量的自动伸缩
Pod 是在 Kubernetes 体系中,承载用户业务负载的一种资源.Pod 们运行的好坏,是用户们最为关心的事情.在业务流量高峰时,手动快速扩展 Pod 的实例数量,算是玩转 Kubernetes ...
- CF469A I Wanna Be the Guy 题解
Content 小 A 和小 B 正在玩一个游戏,游戏一共有 \(n\) 关,而两个人各只能通过 \(p_A,p_B\) 个关卡.问他们能否通过合作通关这个游戏. 数据范围:\(1\leqslant ...