题目来源

一、题目

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. 联立 c2=a2+b2c^2=a^2+b^2c2=a2+b2 与 c=a2−bc=a^2-bc=a2−b 得 c2=b+c+b2c^2=b+c+b^2c2=b+c+b2
  2. 整理得 (c+b)(c−b)=b+c(c+b)(c-b)=b+c(c+b)(c−b)=b+c 即 c=b+1c=b+1c=b+1
  3. 将c=b+1c=b+1c=b+1代入c=a2−bc=a^2-bc=a2−b 得 b=a2−12b=\frac{a^2-1}{2}b=2a2−1​

整理得:

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

对于a,b,c须满足:

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

综上,满足a2+12<=n\frac{a^2+1}{2}<=n2a2+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;
}

D. Pythagorean Triples相关推荐

  1. D. Pythagorean Triples(1487D)(打表找规律 + 二分)

    D. Pythagorean Triples(1487D)(打表找规律 + 二分) 题目来源:D. Pythagorean Triples 题意: 给定一个 n,求满足以下条件的数对 (a, b, c ...

  2. hdu2017青岛网络赛Pythagoras(Tree of primitive Pythagorean triples)

    题面: Given a list of integers a0,a1,a2,⋯,a2k−1. Pythagoras triples over 109 are all solutions of x2+y ...

  3. C. Pythagorean Triples

    链接:https://codeforces.com/problemset/problem/707/C Katya studies in a fifth grade. Recently her clas ...

  4. python:实现Pythagorean triples毕氏三元数(附完整源码)

    python:实现Pythagorean triples毕氏三元数 limit = int(input("Enter upper limit:")) c = 0 m = 2 whi ...

  5. CF707C Pythagorean Triples 题解

    CF707C Pythagorean Triples 题解 题目 链接 字面描述 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 样例 #2 样例输入 #2 样例输 ...

  6. JAVA:实现是否为Pythagorean Triples毕达哥斯拉三角数算法(附完整源码)

    JAVA:实现是否为Pythagorean Triples毕达哥斯拉三角数算法 package com.thealgorithms.maths;public class PythagoreanTrip ...

  7. Codeforces Round #368 (Div. 2) problem: (C) Pythagorean Triples

    本题就一个公式 n^2+((n^2-1)/2)^2=((n^2+1)/2)^2 0.当n==1或n==2时,不存在结果. 1.当n为奇数时此公式求得的数还是整数,成立 2.当n为偶数时分两种情况: ( ...

  8. Codeforces Round #368 (Div. 2) C. Pythagorean Triples

    题目链接:http://codeforces.com/contest/707/problem/C 题意: 直角三角形的三边都为整数,给出其中一边n,求另外两边m.k. (1 ≤ n ≤ 109)  ( ...

  9. Codeforces-1487 D. Pythagorean Triples(数学)

    思路: 数学题,打表可以找出规律,不过我们也可以推一下. c2=a2+b2c^2 = a^2 + b^2c2=a2+b2 c=a2−bc = a^2 - bc=a2−b ⇒\Rightarrow⇒c2 ...

最新文章

  1. Codeforces Round #597 (Div. 2)题解A~D
  2. linux中配置DHCP基本操作
  3. PHP课程20161114
  4. MyBatis-12MyBatis动态SQL之【choose when otherwise】
  5. 盘启动盘_小白教你ULTRAISO制作U盘启动盘
  6. 从放弃迅雷和IDM到自己开发下载工具 1
  7. php做图书管理系统绪论,基于PHP图书管理系统的设计与实现本科毕业论文
  8. bzoj4173:数学
  9. UEditor之实现配置简单的图片上传示例
  10. 巧妙去掉多余的安全删除硬件图标
  11. 干货 | Elasticsearch 索引生命周期管理 ILM 实战指南
  12. mac中实用的录音软件有哪些?
  13. mumu模拟器显示服务器出错,用mumu模拟器显示错误代码
  14. f开头的流媒体软件_流媒体直播工具(Streamon)
  15. OpenCV在图片上画线和矩形
  16. 再谈中国的收入不平等问题
  17. 怎样用css3设计出向上向下的小箭头
  18. c语言 Linux CURL发送Http get请求 带参数
  19. Jmeter(十八):硬件性能监控指标
  20. Pandas读取某一列(特定列,指定列)为列表

热门文章

  1. contiki学习笔记(七)contiki系统
  2. 【Adobe】Photoshop :Mac 系统 Photoshop 软件更换许可指引
  3. ARM_kafka搭建
  4. 几种混沌系统混沌模型
  5. 自然语言处理文本分析_通过自然语言处理释放文本分析的力量
  6. Keil工程文件建立以及.hex文件的生成
  7. Android 通知栏图标
  8. 信用飞疑似信息泄露致用户被骗近4万元,平台借款利率高达75%
  9. 订单系统设计 —— 数据同步与监控
  10. ‘XXXX’ was compiled with optimization - stepping may behave oddly; variables may not be available