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 ...
随机推荐
- Chrome Google 快捷键
窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl 键,然后点击链接 在新标签页中打开链接 按住 Shift 键,然后点击链接 在新窗口中打开链接 Alt+F4 关闭当前窗口 Ctrl+ ...
- QQ群排名霸屏技术居然是这样简单
最近做了一些收费的QQ群,收多少钱,一块钱的入门费,也就是说进入我的QQ群必须要1块钱的会费. 我的QQ群主要是干嘛呢,放些电影,比如说市面上电影院,正在播放的,最新最热门的,火爆的一些电影. 先前呢 ...
- zabbix监控nginx服务状态
nginx需要安装--with-http_stub_status_module模块 $ nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 ...
- ruby 数据类型String
一.字符串创建 单引号包含,不支持转义符和内嵌表达式#{}(插值符) str = 'hello world!' 双引号包含 str = "hello world!" 使用%,%Q, ...
- PHP中的mysql_unbuffered_query与mysql_query的区别
对于mysql_query大家都很熟悉,下面先简单介绍下mysql_unbuffered_query mysql_unbuffered_query (PHP 4 >= 4.0.6, PHP 5) ...
- Element-ui学习使用
这是我使用Element-ui的布局,排布的一个界面,原本我是使用WinfowsForm来做的一个摄像头注册以及查询的小工具,目前我关注前后端的开发,所以就想着能不能把这么个小工具,我用前后端的形式开 ...
- TreeMap与LinkedHashMap的区别
TreeMap是根据元素的内部比较器进行排序的,它可以根据key值的大小排序: LinkedHashMap是保持存放顺序的. TreeMap采用红黑树算法,遍历效率高: LinkedHashMap采用 ...
- Mysql综合练习作业50题
#作业库create database db8 charset utf8; #年级表create table class_grade(gid int not null primary key auto ...
- shell eval命令使用
eval命令将会首先扫描命令行进行所有的置换,然后再执行该命令. 该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两次扫描. 这些需要进行两次扫描的变量有时被称为复杂变量.不过这些变量本 ...
- php杂记——1(基础知识与文件读写)
1.变量前面需要加美元符号"$",常量则不需要: define('PRICE',100); echo PRICE; 2.用一个变量的值作为另一个变量的名称可以得到类似C中的指针变量 ...