BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)
2005: [Noi2010]能量采集
Time Limit: 10 Sec Memory Limit: 552 MB
Submit: 4493 Solved: 2695
[Submit][Status][Discuss]
Description
Input
仅包含一行,为两个整数n和m。
Output
仅包含一个整数,表示总共产生的能量损失。
Sample Input
5 4
【样例输入2】
3 4
Sample Output
36
【样例输出2】
20
对于100%的数据:1 ≤ n, m ≤ 100,000。
HINT
Source
析:首先能看出来,对于每个点,gcd(i,i),就说明它是第几个点,所以我们可以用 f(x) 表示最大公因数包含x的点对的个数,为什么不直接表示最大公因数是x的点对的个数呢,因为实在是不好求啊,所以换一种表示方法,很明显f(x) = (m/x) * (n/x),那么答案应该是什么呢,这里需要用容斥,减去后面的就好了。
也可以用莫比乌斯反演来求,F(x)表示gc(i, j)==x的点对的个数,G(x)表示gcd(i, j)=x的倍数的个数。然后就可以用莫比乌斯反演来求了
代码如下:
数论 + 容斥
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 3e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL f[maxn]; int main(){
scanf("%d %d", &n, &m);
int mmin = min(n, m);
LL ans = 0;
for(int i = mmin; i; --i){
f[i] = (LL)(m/i) * (n/i);
for(int j = i + i; j <= mmin; j += i)
f[i] -= f[j];
ans += f[i] * (2 * i - 1);
}
printf("%lld\n", ans);
return 0;
}
莫比乌斯反演:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 3e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool vis[maxn];
int prime[maxn];
int mu[maxn]; void Moblus(){
mu[1] = 1;
int tot = 0;
for(int i = 2; i < maxn; ++i){
if(!vis[i]) prime[tot++] = i, mu[i] = -1;
for(int j = 0; j < tot; ++j){
if(i * prime[j] >= maxn) break;
vis[i*prime[j]] = 1;
if(i % prime[j] == 0){
mu[i*prime[j]] = 0;
break;
}
else mu[i*prime[j]] = -mu[i];
}
}
} LL G[maxn]; int main(){
Moblus();
scanf("%d %d", &n, &m);
for(int i = 1; i < maxn; ++i) G[i] = (LL)(m/i) * (n/i);
LL ans = 0;
int k = min(n, m);
for(int i = 1; i <= k; ++i){
LL num = 0;
for(int j = 1; j * i <= k; ++j)
num += mu[j] * G[j*i];
ans += num * (2*i-1);
}
printf("%lld\n", ans);
return 0;
}
BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)的更多相关文章
- BZOJ 2005 [Noi2010]能量採集 (容斥)
[Noi2010]能量採集 Time Limit: 10 Sec Memory Limit: 552 MB Submit: 2324 Solved: 1387 [id=2005"> ...
- BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )
一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) * 2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...
- BZOJ 2005: [Noi2010]能量采集
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 3312 Solved: 1971[Submit][Statu ...
- bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MB[Submit][Status][Discuss] Description 栋栋 ...
- BZOJ 2005: [Noi2010]能量采集(容斥+数论)
传送门 解题思路 首先题目要求的其实就是\(\sum\limits_{i=1}^n \sum\limits_{j=1}^m [(gcd(i,j)-1)*2+1)]\),然后变形可得\(-n*m+2\s ...
- 【刷题】BZOJ 2005 [Noi2010]能量采集
Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...
- 洛谷P1447 [NOI2010]能量采集(容斥)
传送门 很明显题目要求的东西可以写成$\sum_{i=1}^{n}\sum_{j=1}^m gcd(i,j)*2-1$(一点都不明显) 如果直接枚举肯定爆炸 那么我们设$f[i]$表示存在公因数$i$ ...
- BZOJ 2005: [Noi2010]能量采集(莫比乌斯反演)
http://www.lydsy.com/JudgeOnline/problem.php?id=2005 题意: 思路: 首先要知道一点是,某个坐标(x,y)与(0,0)之间的整数点的个数为gcd ...
- BZOJ 2005: [Noi2010]能量采集 [莫比乌斯反演]
题意:\((0,0)\)到\((x,y),\ x \le n, y \le m\)连线上的整点数\(*2-1\)的和 \((0,0)\)到\((a,b)\)的整点数就是\(gcd(a,b)\) 因为. ...
随机推荐
- 关于xshell连接阿里云服务器后报错的问题,git安装失败,找不到git包
1.如果安装git出现这样的错误的,在接下来键入这样一行命令 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-c ...
- Windows系统崩溃后快速恢复Oracle数据库的妙招
Windows系统崩溃后快速恢复Oracle数据库,以下是操作步骤 假设oracle数据安装在d:\\oracle文件夹中,数据库名称orcl 1>将崩溃的数据库安装目录"d:\\or ...
- Git 安装和使用教程(更加详细)
转载至:https://www.cnblogs.com/smuxiaolei/p/7484678.html#undefined Git 安装和使用教程 git 提交 全部文件 git add . g ...
- XHR的对象及用法
function createXHR(){ //检测原生XHR对象是否存在,如果存在刚返回它的新实例: //如果不存在,则检测ActiveX对象; //如果两个都不存 ...
- 解决sublime3不能编辑插件default settings的问题
一.遇见问题 今天给sublime安装了View In Browser,想更改一下默认启动的浏览器 preferences-Package settings-View In Browser-setti ...
- nginx日志
相关知识可参考文章:nginx日志格式及自定义日志配置 1.查看nginx的log配置 1)vim /etc/nginx/nginx.conf 打开为 user nginx;worker_proces ...
- 线性表(java)
线性表 概念:零个或者多个数据元素的有限序列. 特点:除了第一个元素没有前驱结点,最后一个元素没有后继结点外,其它元素有且仅有一个直接前驱和一个直接后继结点.元素的个数必定为有限个. 实现: 定义一个 ...
- win32多线程程序设计
标题是一本书名,写得挺有意思的,是今天早上同事带过来的,我借过来看了一会儿. 然后按照书里面前面几章的内容敲了一些代码,跑了几个例子看了一下. 创建线程的函数: HANDLE CreateThread ...
- 超级好看!巧用PS将风光人像打造成唯美的小星球效果!
本篇教大家如何巧用PS将风光人像照片打造成唯美的小星球!教程讲解过程很细致,理论实操简单易懂,最终完成效果灰常的漂亮,喜欢的小伙伴赶快收走学起来吧! 原图: 效果图: PS:1.对齐图像,选中所有图, ...
- 论坛:Error:No result defined for action cn.itcast.oa.view.action.TopicAction and result
使用了<s:hidden name="forumId" value="#forum.id"/> 可以改为: <s:hidden name=&q ...