ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

D. Pythagorean Triples

2021-02-16 13:57:38  阅读:261  来源: 互联网

标签:Triples frac 2a2 Pythagorean triple formula he


题目来源

一、题目

A Pythagorean triple is a triple of integer numbers (a,b,c) such that it is possible to form a right triangle with the lengths of the first cathetus, the second cathetus and the hypotenuse equal to a, b and c, respectively. An example of the Pythagorean triple is (3,4,5).
Vasya studies the properties of right triangles, and he uses a formula that determines if some triple of integers is Pythagorean. Unfortunately, he has forgotten the exact formula; he remembers only that the formula was some equation with squares. So, he came up with the following formula: c=a^2−b.
Obviously, this is not the right formula to check if a triple of numbers is Pythagorean. But, to Vasya’s surprise, it actually worked on the triple (3,4,5): 5=3^2−4, so, according to Vasya’s formula, it is a Pythagorean triple.
When Vasya found the right formula (and understood that his formula is wrong), he wondered: how many are there triples of integers (a,b,c) with 1≤a≤b≤c≤n such that they are Pythagorean both according to his formula and the real definition? He asked you to count these triples.

给定n,问多少1≤a≤b≤c≤n,使直角三角形 (a,b,c) 满足c=a^2−b

二、输入

The first line contains one integer t (1≤t≤104) — the number of test cases.
Each test case consists of one line containing one integer n (1≤n≤109).

第一行测试样例数t
第二行整数n

三、输出

For each test case, print one integer — the number of triples of integers (a,b,c) with 1≤a≤b≤c≤n such that they are Pythagorean according both to the real definition and to the formula Vasya came up with.

满足条件的三角形数量

四、样例

input:

3
3
6
9

output:

0
1
1

Note
The only Pythagorean triple satisfying c=a2−b with 1≤a≤b≤c≤9 is (3,4,5); that’s why the answer for n=3 is 0, and the answer for n=6 (and for n=9) is 1.

五、思路 + 代码

  1. 联立 c 2 = a 2 + b 2 c^2=a^2+b^2 c2=a2+b2 与 c = a 2 − b c=a^2-b c=a2−b 得 c 2 = b + c + b 2 c^2=b+c+b^2 c2=b+c+b2
  2. 整理得 ( c + b ) ( c − b ) = b + c (c+b)(c-b)=b+c (c+b)(c−b)=b+c 即 c = b + 1 c=b+1 c=b+1
  3. 将 c = b + 1 c=b+1 c=b+1代入 c = a 2 − b c=a^2-b c=a2−b 得 b = a 2 − 1 2 b=\frac{a^2-1}{2} b=2a2−1​

整理得:

  1. a = a a=a a=a
  2. b = a 2 − 1 2 b=\frac{a^2-1}{2} b=2a2−1​
  3. c = a 2 + 1 2 c=\frac{a^2+1}{2} c=2a2+1​

对于a,b,c须满足:

  1. a , b , c a,b,c a,b,c皆为正整数,所以 a 2 − 1 2 \frac{a^2-1}{2} 2a2−1​与 a 2 + 1 2 \frac{a^2+1}{2} 2a2+1​要为正整数,所以a要为奇数
  2. a > 0 a>0 a>0
  3. b > = a b>=a b>=a即 b = a 2 − 1 2 > = a b=\frac{a^2-1}{2}>=a b=2a2−1​>=a得 a > = 2 a>=2 a>=2
  4. c > b c>b c>b即 a 2 + 1 2 > a 2 − 1 2 \frac{a^2+1}{2}>\frac{a^2-1}{2} 2a2+1​>2a2−1​显然成立

综上,满足 a 2 + 1 2 < = n \frac{a^2+1}{2}<=n 2a2+1​<=n的所有奇数 a ( a > = 3 ) a(a>=3) a(a>=3)的个数即为所求。

代码如下:

#include<iostream>
using namespace std;
typedef long long ll;
ll n;
int main() {
	int t;
	cin >> t;
	while (t--) {
		ll cnt = 0;
		cin >> n;
		for (ll a = 3; (a * a + 1) / 2 <= n; a += 2) {
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

标签:Triples,frac,2a2,Pythagorean,triple,formula,he
来源: https://blog.csdn.net/qq_43697906/article/details/113824299

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有