列表

详情


1925. 统计平方和三元组的数目

一个 平方和三元组 (a,b,c) 指的是满足 a2 + b2 = c2 的 整数 三元组 ab 和 c 。

给你一个整数 n ,请你返回满足 1 <= a, b, c <= n 的 平方和三元组 的数目。

 

示例 1:

输入:n = 5
输出:2
解释:平方和三元组为 (3,4,5) 和 (4,3,5) 。

示例 2:

输入:n = 10
输出:4
解释:平方和三元组为 (3,4,5),(4,3,5),(6,8,10) 和 (8,6,10) 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int countTriples(int n) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-07-12 14:30:00

const mx = 250
var c2 [mx*mx + 1]bool
func init() {
	for i := 1; i <= mx; i++ {
		c2[i*i] = true
	}
}

func countTriples(n int) (ans int) {
	for i := 1; i <= n; i++ {
		for j := 1; j < i; j++ {
			if s := i*i + j*j; s <= n*n && c2[s] {
				ans += 2
			}
		}
	}
	return
}

上一题