luoguP1447 [NOI2010]能量采集
2024-09-04 01:29:37
https://www.luogu.org/record/22874213
题目大意:给定n和m,求Σ(1<=i<=n)Σ(1<=j<=m)GCD(i,j)* 2-1
i和j的限制不同,传统的线性筛法失效了,这里我们考虑容斥原理
设:f(x) = ΣΣ[ gcd(i,j)==x ]
那么答案即为 Σf(x)* x, x:1->n //规定n<m, 即gcd(n,m)<=n
g(x) = Σ[ x | gcd(i,j) ] = n/x * m/x = f(x) + f(2x) + ... + f(n/x * x)
i: x, 2x...n/x * x, j: x, 2x...m/x * x。 所以gcd(i,j)为x倍数的有 (n/x) * (m/x)
移项一下,用原先预处理好的g[]算f[], f(x) = g(x) - f(2x) - f(3x) - ... - f(n/x* x)
倒过来算f(x),那么f(2x)...f(n/x * x)就都算好了
#include<cstdio>
#include<iostream>
using namespace std;
#define MAX 100000+999
#define ll long long
ll f[MAX];
int n,m;
int main() {
scanf("%d%d",&n,&m);
if(n > m) swap(n, m);
for(int i = 1; i <= n; i++) f[i] = (ll)(n/i) * (m/i);// 注意加括号!!! //注意变longlong
for(int i = n; i >= 1; i--) {//求f[i]
for(int j = i+i; j <= n; j+=i) {
f[i] -= f[j];
}
}
ll ans = 0;
for(int i = 1; i <= n; i++)
ans += f[i]*i;
// for(int i = 1; i <= n; i++) printf("%d\n",f[i]);
printf("%lld",(ans<<1) - (ll)n*m);//把Σ变一下
}
最新文章
- tcpdump抓取HTTP包
- How to make your assembly more secure from referencing by unauthorized bits
- ReferenceQueue<;T>;随笔
- oracle RAC的VIP和scan
- 给jar包进行数字签名(2014-06-28记)
- c++出错记录
- 再议Unity 3D
- Scala学习之for 循环和 yield 的例子
- Win环境下Oracle小数据量数据库的物理备份
- 最新的Android版本和API Level的对应关系表
- 在android系统上写C语言程序--开机启动该程序不进入安卓系统
- 浅入深出Vue:工具准备之PostMan安装配置及Mock服务配置
- Ping++支付
- service cloudera-scm-server restart报错 Unable to retrieve remote parcel repository manifest
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
- Yii2 nginx配置伪静态
- (转)SQL知识_Sql日期时间格式转换
- JPQL和SQL的比较
- Python中为什么要使用self?
- init进程接管孤儿进程的验证
热门文章
- MSSQL注入:显错注入及反弹注入
- 使用 Scrapy 的 ImagesPipeline 下载图片
- 修改robotframework的元素定位方式,使之支持带括号的xpath定位方式
- 201871010102-常龙龙《面向对象程序设计(java)》第十四周学习总结
- AcWing&#160;21.&#160;斐波那契数列
- 洛谷 P4017 最大食物链计数
- 转载-SpringBoot结合线程池解决多线程问题实录;以及自己的总结
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 10
- IT兄弟连 Java语法教程 数组 多维数组 二维数组的初始化
- git报错:failed to push some refs to &#39;git@github.com:JiangXiaoLiang1988/CustomerHandl