此题是 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的更多相关文章

  1. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  2. Pythagorean Triples

    Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. codeforces-707 C. Pythagorean Triples

    C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Pythagorean Triples 707C

    Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theor ...

  5. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

  6. Pythagorean Triples毕达哥斯拉三角(数学思维+构造)

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

  7. codeforces707C:Pythagorean Triples

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

  8. codeforces 707C C. Pythagorean Triples(数学)

    题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...

  9. Codeforces Round #368 (Div. 2) C

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

随机推荐

  1. PHP无法用下标访问

    php数组分为普通数组和关联数组,普通数组可以用下标访问,而关联数组不可以.

  2. 微信中h5页面用window.history.go(-1)返回上一页页面不会重新加载问题

    问题描述: 在实际开发中遇到这样一个问题,业务需求涉及到返回上一页问题,第一时间想到了window.history.go(-1)方法,这样做本身没有任何问题,但是在微信中,安卓手机还好返回上一页页面会 ...

  3. Python栈的学习资料

    持续更新... 1. 基础 Python for Everybody的视频课程,称得上深入浅出 https://www.py4e.com/ 2. 进阶 偏重实践应用,快速上手,稀饭~ https:// ...

  4. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read.write 0. 导语 在上一篇博客里面,基于OMAPL138的字符驱动_GPIO驱动AD9833(一)之 ...

  5. Spring配置文件一直报错的根源所在

    跳坑后的感悟总结 Spring在配置文件中经常会报XML错误,以下是几种常见的解决办法 方式一:打开eclipse-->Project-->Clean ;清除一下 方式二:查看xml配置文 ...

  6. struts2官方 中文教程 系列八:异常处理

    在本教程中,我们将探讨如何启用Struts 2框架处理web应用程序生成的任何未捕获的异常.Struts 2提供了健壮的异常处理,包括能够自动记录任何未捕获的异常,并将用户重定向到错误web页面. 贴 ...

  7. Python的re模块的常用方法

    一.re的match与search方法 1.re.match方法 re.match 尝试从字符串的起始位置匹配一个模式,匹配成功re.match方法返回一个匹配的对象,如果不是起始位置匹配成功的话,m ...

  8. FRDM-KL25Z开发板上电试用

    1. 硬件平台:FRDM-KL25Z开发板,先看下板子,Cortex M0+的内核,板子上自带MMA8451Q的三轴加速度传感器,触摸滑动按键,openSDA调试器.MCU主频48MHz,有16KB ...

  9. Linux 文件与目录管理命令

    处理目录的常用命令 常见的处理目录的命令: ls: 列出目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录,语法:mkdir [-mp] 目录名称 -m :配置文件的权限 -p ...

  10. SQL 注入教程

    SQL 注入测评教程 1     准备 安装包:Burpsuit.Python27.sqlmap 2     安装配置 2.1    Burpsuit 1)       解压Burpsuit 2)   ...