当前位置: 首页 > news >正文

P14139 题解

有意思的题目。

首先把式子拆成两部分:

\[\begin{aligned}A &= \sum_{x = 1}^n \left \lfloor \dfrac{x^2}{n} \right \rfloor \\B &= \sum_{x = 1}^n \left \lfloor \sqrt{nx} \right \rfloor \end{aligned} \]

个人认为 \(B\) 更好算,所以先算 \(B\)

考虑 \(\left \lfloor \sqrt{x} \right \rfloor\) 的意义,注意到:

\[\left \lfloor \sqrt{x} \right \rfloor = \sum_{y^2 \le x} 1 \]

则:

\[\begin{aligned}B &= \sum_{x = 1}^n \left \lfloor \sqrt{nx} \right \rfloor \\&= \sum_{x = 1}^n \sum_{y^2 \le nx} 1 \\&= \sum_{y = 1}^n \sum_{x = \lceil y^2 / n \rceil}^n 1 \\&= \sum_{y = 1}^n \left( n - \left \lceil \dfrac{y^2}{n} \right \rceil + 1 \right) \\&= n^2 + n - \sum_{y = 1}^n \left \lceil \dfrac{y^2}{n} \right \rceil \end{aligned} \]

我们发现,这个 \(\sum\)\(A\) 很相似啊,所以:

\[\begin{aligned}A + B&= n^2 + n + \sum_{x = 1}^n \left( \left \lfloor \dfrac{x^2}{n} \right \rfloor - \left \lceil \dfrac{x^2}{n} \right \rceil \right) \\&= n^2 + n - \sum_{x = 1}^n \left[ n \nmid x^2 \right] \\&= n^2 + \sum_{x = 1}^n \left[ n \mid x^2 \right] \end{aligned} \]

到这里式子就推不动了,所以只能从 \(n \mid x^2\) 入手。

\(n = ab^2\),其中 \(b\) 为最大的满足 \(b^2 \mid n\) 的整数。则 \(n \mid x^2 \iff ab^2 \mid x^2 \iff a^2 b^2 \mid x^2 \iff ab \mid x\)。那么:

\[\begin{aligned}A + B&= n^2 + \sum_{x = 1}^{ab^2} \left[ ab \mid x \right] \\&= n^2 + \left \lfloor \dfrac{ab^2}{ab} \right \rfloor \\&= n^2 + b \end{aligned} \]

那么只需要算 \(b\) 就可以了。受部分分启发,我们先做 \(n\) 为完全平方数的情况。容易发现,此时 \(b = \sqrt{n}\)

那么若 \(n\) 不为完全平方数呢?如果 \(n\) 只有两个质因子,那么 \(b = 1\)。否则每个质因子都 \(\le 10^6\),可以预处理 \(\le 10^6\) 的质数,然后对 \(n\) 分解质因数。设 \(n = \prod \limits_i p_i^{\alpha_i}\),则 \(b = \prod \limits_i p_i^{\lfloor \alpha_i / 2 \rfloor}\)

时间复杂度:\(O \left( \dfrac{T \sqrt[3]{n}}{\ln(n)} \right)\)

#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>using namespace std;
using i64 = long long;constexpr int MOD = 1e9 + 7, CURT_N = 1e6;vector<int> primes;void precompute()
{bitset<CURT_N + 1> not_prime(3);for (int i = 2; i <= CURT_N; ++i) {if (not_prime.test(i))continue;primes.push_back(i);for (i64 j = i64(i) * i; j <= CURT_N; j += i)not_prime.set(j);}
}inline int add_mod(i64 x, i64 y) { return (x % MOD + y % MOD + MOD) % MOD; }
inline int mul_mod(i64 x, i64 y) { return (x % MOD) * (y % MOD) % MOD; }inline bool is_square(i64 x)
{i64 root = sqrt(x);return root * root == x;
}int pow_mod(i64 x, i64 y)
{int res = 1;for (; y > 0; y >>= 1, x = mul_mod(x, x)) {if (y & 1)res = mul_mod(res, x);}return res;
}struct Solution {void main(){i64 n;cin >> n;int sq = 1;i64 orig_n = n;for (auto p : primes) {if (n % p != 0)continue;int cnt = 0;while (n % p == 0) {n /= p;++cnt;}sq = mul_mod(sq, pow_mod(p, cnt >> 1));}if (is_square(n))sq = mul_mod(sq, sqrt(n));cout << add_mod(sq, mul_mod(orig_n, orig_n)) << '\n';}
};int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int t;cin >> t;precompute();while (t-- > 0)Solution().main();return 0;
}
http://www.hskmm.com/?act=detail&tid=25095

相关文章:

  • AI 自我理解边界
  • 2025钢球厂家最新企业品牌推荐排行榜,轴承钢球,不锈钢球,碳钢球,精密钢球,440C不锈钢球推荐这十家公司!
  • 2025 年工业提升门厂家最新企业品牌推荐排行榜,汇峰节能科技彰显行业影响力!
  • 什么是偏微分方程?
  • 2025 权威推荐!电梯源头品牌 TOP5 排行榜:实力厂家精选,品质之选不容错过
  • 钉钉红包性能优化之路 - 实践
  • IIS反向代理tomcat
  • 2025混合机厂家最新企业品牌推荐排行榜,高效盘条式混合机,无重力混合机,犁刀式混合机,锥形混合机,卧式螺带混合机推荐这十家公司!
  • 2025离心泵厂家最新企业品牌推荐排行榜! 卧式离心泵,化工离心泵,多级离心泵,卧式多级离心泵,不锈钢离心泵推荐这十家公司!
  • 2025真空炉厂家最新企业品牌推荐排行榜,高温烧结真空炉,真空退火炉,智能铍铜真空炉推荐这十家公司!
  • 【论文阅读】Dolphin: Document Image Parsing via Heterogeneous Anchor Prompting - 实践
  • 2025 --【J+S 二十连测】-- 第二套 总结
  • 2025 蒸发器厂家最新企业品牌推荐排行榜,江苏纵横携手知名品牌,彰显蒸发器公司行业影响力
  • 题解:Luogu P11976 [KTSC 2021] 通信网络 / communication
  • 弦振动方程
  • 理论构建尝试整理
  • 2025聚合硫酸铁厂家最新企业品牌推荐排行榜,工业聚合硫酸铁,混凝剂聚合硫酸铁,固态聚合硫酸铁,粉末聚合硫酸铁,硫酸亚铁公司推荐!
  • 2025成型机厂家最新企业品牌推荐排行榜,冷弯成型机,卷帘门成型机,卷闸门成型机,彩钢瓦成型机,货架成型机推荐!
  • 2025 年 PP 管厂家最新推荐榜:甄选 pp 风管,PP 喷淋塔,pp 洗涤塔,pp 通风管道优质公司!
  • 解密并下载受DRM保护的MPD(DASH流媒体)加密视频 - 教程
  • 在PyCharm中运行 wandb.login();
  • 机器学习科学家分享技术写作艺术
  • AT VP 记录
  • rpm安装
  • 关于主体性介枚枚介的讨究
  • 2025索道厂家最新企业品牌推荐排行榜,城市交通索道,旅游索道,滑雪索道,单人固定抱索器拖牵索道,固定抱索器吊篮式索道公司推荐
  • 无向图三元环计数 小记
  • Python语法基础篇(含有类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
  • 2025 年探伤仪厂家最新企业品牌推荐排行榜,涡流探伤仪,超声波探伤仪,管材探伤仪,焊缝探伤仪,无损探伤仪推荐这十家公司!
  • 2025 年建筑工程施工总包最新推荐排行榜,以严格质量管控彰显行业实力推荐这十家公司!