hihoCoder #1872 : Pythagorean triple
此题是 2018 年 ICPC Asia Beijing Regional Contest 的 C 题。
题目大意
求斜边长度不超过 $n$($ n \le 10^9$) 的勾股数的数量。不计两直角边的顺序,即勾股数 $(a, b, c)$ 和 $(b, a, c)$ 视作同一组。
分析
这是一道颇为经典的计数问题。
请先阅读维基百科上的 Pythagorean triple 条目。
设斜边为 $n$ 的勾股数有 $f(n)$ 个。又设斜边为 $n$ 的本原勾股数有 $g(n)$ 个。于是有
$ f(n) = \sum_{d \mid n} g(d)$ 。
令 $F$ 为 $f$ 的前缀和,令 $G$ 为 $g$ 的前缀和。有
\begin{aligned}
F(n) &= \sum_{i = 1}^{n} f(n) \\
&= \sum_{i = 1}^{n} \sum_{d \mid i} g(d) \\
&= \sum_{i = 1}^{n} G(\floor{n / i})
\end{aligned}
根据 $G$ 的定义,有
\begin{aligned}
G(n) &= \sum_{i = 1}^{n} g(i) \\
&=\sum_{\substack{1 \le x \le n \\ x \text{ is odd} } } \sum_{\substack{1 \le y \le n \\ y \text{ is even}}} [x^2 + y^2 \le n] [\gcd(x, y) = 1] \\
&= \frac{1}{2} \left(\sum_{1 \le x \le n } \sum_{1 \le y \le n } - \sum_{\substack{1 \le x \le n \\ x \text{ is odd}} } \sum_{\substack{1 \le y \le n \\ y \text{ is odd}} } \right) [x^2 + y^2 \le n] [\gcd(x, y) = 1]
\end{aligned}
而
\begin{aligned}
& \left(\sum_{1 \le x \le n } \sum_{1 \le y \le n } - \sum_{\substack{1 \le x \le n \\ x \text{ is odd}} } \sum_{\substack{1 \le y \le n \\ y \text{ is odd}} } \right) [x^2 + y^2 \le n] [\gcd(x, y) = 1] \\
&= \left(\sum_{1 \le x \le n } \sum_{1 \le y \le n } - \sum_{\substack{1 \le x \le n \\ x \text{ is odd}} } \sum_{\substack{1 \le y \le n \\ y \text{ is odd}} } \right) [x^2 + y^2 \le n] \sum_{d \mid \gcd(x, y)} \mu(d) \\
&= \sum_{1\le d \le \sqrt{n/2}} \mu(d) \left(\sum_{1 \le x \le n } \sum_{1 \le y \le n } - \sum_{\substack{1 \le x \le n \\ x \text{ is odd}} } \sum_{\substack{1 \le y \le n \\ y \text{ is odd}} } \right) [x^2 + y^2 \le n] [d \mid x] [d \mid y] \\
&= \sum_{1\le d \le \sqrt{n/2}} \mu(d) \left(\sum_{ 1 \le i \le n/d } \sum_{1 \le y \le n } - \sum_{\substack{1 \le i \le n/d \\ di \text{ is odd}} } \sum_{\substack{1 \le y \le n \\ y \text{ is odd}} } \right) [(id)^2 + y^2 \le n] [d \mid y] \\
&= \sum_{1\le d \le \sqrt{n/2}} \mu(d) \left(\sum_{ 1 \le i \le \sqrt{n}/d } \sum_{1 \le j \le \sqrt{n-(id)^2}/d } - \sum_{\substack{1 \le i \le \sqrt{n}/d \\ di \text{ is odd}} } \sum_{\substack{1 \le j \le \sqrt{n-(id)^2}/d \\ dj \text{ is odd}} } \right) 1 \\
&= \sum_{1\le d \le \sqrt{n/2}} \mu(d) \left(\sum_{ 1 \le i \le \sqrt{n}/d } \floor{ \frac{\sqrt{n-(id)^2}}{d} } - [d \text{ is odd}] \sum_{\substack{1 \le i \le \sqrt{n}/d \\ i \text{ is odd}} } \sum_{\substack{1 \le j \le \sqrt{n-(id)^2}/d \\ j \text{ is odd}} } 1 \right) \\
&= \sum_{1\le d \le \sqrt{n/2}} \mu(d) \left(\sum_{ 1 \le i \le \sqrt{n}/d } \floor{ \frac{\sqrt{n-(id)^2}}{d} } - [d \text{ is odd}] \sum_{\substack{1 \le i \le \sqrt{n}/d \\ i \text{ is odd}} } \floor{\frac{\frac{\sqrt{n-(id)^2}}{d} + 1}{2}} \right)
\end{aligned}
TODO:复杂度分析。
Implementation
预处理 $G$ 的前 2000 万项。
注意:代码不完整。
int main() {
FAST_READ
cout << fixed << setprecision(1);
#ifdef LOCAL
ifstream in("main.in");
cin.rdbuf(in.rdbuf());
#endif
const int nax = 1e9 + 1;
// println(nax);
const int pre_n = 2e7;
vl pre_G(pre_n + 1); // pre-calculate some items of G
const int max_v = sqrt(pre_n);
stp(i, 1, max_v + 1, 2) {
const int i2 = i * i;
const int max_j = sqrt(pre_n - i2);
stp (j, 2, max_j + 1, 2) {
if (__gcd(i, j) == 1) {
pre_G[i2 + j * j]++;
}
}
}
rng (i, 1, pre_n + 1) {
pre_G[i] += pre_G[i - 1];
}
const int max_d = sqrt(nax/2);
const auto mu = get_mu(max_d);
auto G = [&mu, &pre_G, pre_n](int n) { // # of primitive Pythagorean triples with c <= n
if (n <= pre_n) return pre_G[n];
ll ans = 0;
const int max_gcd = sqrt(n / 2);
const int tmp = (int)sqrt(n);
rng (d, 1, max_gcd + 1) {
ll sum = 0;
const int max_i = tmp / d;
for (int i = 1; i <= max_i; ) {
const int arg = int(sqrt(n - sq(i*d))) / d;
const int j = int(sqrt(n - sq(arg * d))) / d;
sum += (j - i + 1) * arg;
if (d & 1) {
sum -= (j - i + 1 + (i & 1)) / 2 * ((arg + 1) / 2);
}
i = j + 1;
}
ans += sum * mu[d];
}
return ans / 2;
};
auto F = [&](int n) { // # of Pythagorean triples with c <= n
ll ans = 0;
for (int i = 1; i <= n; ) {
int arg = n / i;
int j = n / arg;
ans += 1LL * (j - i + 1) * G(arg);
i = j + 1;
}
return ans;
};
int T; scan(T); rep (T) {
int n; scan(n);
println(F(n));
}
#ifdef LOCAL
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
hihoCoder #1872 : Pythagorean triple的更多相关文章
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Pythagorean Triples
Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces-707 C. Pythagorean Triples
C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Pythagorean Triples 707C
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theor ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...
- Pythagorean Triples毕达哥斯拉三角(数学思维+构造)
Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...
- codeforces707C:Pythagorean Triples
Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...
- codeforces 707C C. Pythagorean Triples(数学)
题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #368 (Div. 2) C
Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...
随机推荐
- springmvc的类型转换器converter
这个convter类型转换是器做什么用的? 他是做类型转换的,或者数据格式化处理.可以把数据在送到controller之前做处理.变成你想要的格式或者类型.方便我们更好的使用. 比如说你从前台传过来一 ...
- PC时代 常用搜索引擎高级指令 勿忘
PC时代,高级指令辅助检索,高效输出既定的需求,被广泛运用于Search Engine. 布局search入口的平台,高级指令都不可或缺.现今,高级指令的高效性,仍然主要体现在搜索引擎检索过程中. i ...
- PHP实现SMTP邮件的发送实例
当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...
- Django的aggregate()和annotate()函数的区别
aggregate() aggregate()为所有的QuerySet生成一个汇总值,相当于Count().返回结果类型为Dict. annotate() annotate()为每一个QuerySet ...
- Leecode刷题之旅-C语言/python-9.回文数
/* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...
- 一笔画问题 南阳acm42(貌似没用到什么算法)
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- go学习笔记-包处理
包处理 package是go管理代码的重要工具,用于组织 Go 源代码,提供了更好的可重用性与可读性. 可见性 变量或函数名的首字母大写时,其就是可导出的,小写时则是不可导出的. 函数和变量的可访问性 ...
- HBase-site.xml 常见重要配置参数
HBase 常见重要配置参数 (1) Hbase.rpc.timeout rpc 的超时时间,默认 60s,不建议修改,避免影响正常的业务,在线上环境刚开始配置的是 3 秒,运行半天后发现了大量的 t ...
- KMP算法(查找子序列)
KMP类似暴力,但是不会和暴力完全一样,回溯到起点. 简单的说 假如 模板链字符串是: abcabcabcabd 寻找abcabd 在模板链出现的次数,并且输出该次数 ...
- OrCAD把原理图中的器件添加到原理图库
1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...