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)\) 因为. ...
随机推荐
- 如何开发简单的javaweb项目,jsp+javabean+servlet
一.相关的软件下载和环境配置 1.下载并配置JDK. 2.下载eclipse. 3.下载并配置apache-tomcat(服务器). 4.下载MySQL(数据库). 5.下载Navicat for M ...
- iOS 打包方式
https://www.cnblogs.com/wengzilin/p/4601684.html
- webpack(一) 安装使用 之css使用注意
在webpackDemo 文件夹中新建 style.css,index.html style.css 中将背景色设为红色. body{ background-color: red; } he'llWo ...
- 将之前的Power idea公司的数据按照下图所示的格式在屏幕上显示出来。
之前的文章 示例代码如下 assume cs:codesg ;将整个data段看作是一个数组,长度一共为 ;21*4+21*4+2*21=168+42=210字节 data segment db ' ...
- 当前上下文中不存在viewbag
参考链接:http://www.cnblogs.com/chas/p/5076297.html view文件夹下的web.config中的appsetting节点中缺少了 <add key=&q ...
- git 远程仓库与本地项目关联
在git 中创建一个项目或仓库如起名blog,生成README.md文件,在本地创建一个项目名为blog ,blog里面是代码,此时执行 git remote add origin <ssh协 ...
- html标签二
1.没有前后顺序的信息列表<ul> <li></li> <li></li></ul>2.有序列表 <ol> < ...
- Eclipse.Error.gen already exists but is not a source folder.
在Eclipse ADT来开发Android App时会遇到以下问题:"myproject/gen already exists but is not a source folder. Co ...
- Laravel Session() 失效的问题
之前因为自己自定义了后台的路由,然后路由定义的乱七八糟的. 突然发现session失效了,记录一下,避免后者遇坑. 路由组统一通过web中间件或者存在于一个中间件中 protected $middle ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...