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,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 }



时间: 2024-10-10 15:21:11

1046:Square Number的相关文章

PKU Online Judge 1046 Square Number

一道类似高中数学竞赛的构造题目,我们应该是把a写成b的某种形式然后得出答案,唉- 思路来自http://www.voidcn.com/blog/u013614281/article/p-2701125.html- a*a + ab = (a+t)*(a+t) a*b = 2*a*t+t*t a = t*t/(b-2t) 如果b是奇数,那么t = (b-1)/2 如果b是偶数,如果(b-2)/2是偶数,t = (b-2)/2 如果b是偶数,而且(b-2)/2是奇数,那么t = (b - 4)/2

山东省第六届“浪潮杯”ACM程序设计大赛:D:Square Number

Description: In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinc

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

Description In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct

【第七届山东省ACM竞赛】Square Number

思路比较明确,就是一个数,如果和另外一个数乘起来是个平方数的话,那么满足一个条件 数A可以分解成为n1 个 a1,n2 个 a2 -- 数B可以分解成为m1个 a1,m2 个 a2-- 这满足的条件是(ni + mi) % 2 == 0 一个数的分解出来奇个数的因子乘起来得到的值为v,找之前有几个数他的奇个数因子成积为v 代码如下: #include<cmath> #include<cstdio> #include<cstring> #include<iostre

SDUT 3258 Square Number 简单数学

和上一题一样,把平方因子除去,然后对应的数就变成固定的 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=1e6+5; const int INF=0x3f3f3f3f; int vis[N],prime[1005],cnt; void getp

UVA 11461 Square Numbers解题报告

Discription A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). In

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

题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct intege

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s

Project Euler 98:Anagramic squares 重排平方数

Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively, we form a square number: 1296 = 362. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square num