此题是 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加密解密算法 这里主要介绍三种常用的加密解密算法:方法一: /** * @param $string 要加密/解密的字符串 * @param string $operation 类型,ENCOD ...

  2. 基于webSocket的聊天室

    前言 不知大家在平时的需求中有没有遇到需要实时处理信息的情况,如站内信,订阅,聊天之类的.在这之前我们通常想到的方法一般都是采用轮训的方式每隔一定的时间向服务器发送请求从而获得最新的数据,但这样会浪费 ...

  3. 【tp5.1】七牛云上传图片

    composer安装: composer require qiniu/php-sdk 配置使用: 在tp5.1的配置文件app.php中配置七牛云的参数 'qiniu' => [ 'access ...

  4. PHP icov转码报错解决方法,iconv(): Detected an illegal character in input string

    iconv(): Detected an illegal character in input string 错误解决方法 //转码 function iconv_gbk_to_uft8($strin ...

  5. Spark-源码-Spark-Submit 任务提交

    Spark 版本:1.3 调用shell, spark-submit.sh args[] 首先是进入 org.apache.spark.deploy.SparkSubmit 类中调用他的 main() ...

  6. JavaSE 第二次学习随笔(四)

    ---------------------------------------------------------------------------------------------------- ...

  7. Python爬虫爬取豆瓣电影之数据提取值xpath和lxml模块

    工具:Python 3.6.5.PyCharm开发工具.Windows 10 操作系统.谷歌浏览器 目的:爬取豆瓣电影排行榜中电影的title.链接地址.图片.评价人数.评分等 网址:https:// ...

  8. Python3 列表,元组,字典,字符串知识小结

    一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...

  9. 在WPF中自定义控件(1)

    原文:在WPF中自定义控件(1)    在WPF中自定义控件(1):概述                                                   周银辉一, 不一定需要自定 ...

  10. 剁了xp,醉了win7

    装完win7,安装各种软件完毕,重启,然并卵.  cpu,内存飙升!! svchost.exe这个进程内存发疯了一样往上飙升 从 几十兆  到占用1个多G, 纳尼, 总共物理内存才2G. ╮(╯▽╰) ...