BZOJ2820 YY的GCD 莫比乌斯+系数前缀和
/**
题目:BZOJ2820 YY的GCD
链接:http://www.cogs.pro/cogs/problem/problem.php?pid=2165
题意:神犇YY虐完数论后给傻×kAc出了一题
给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对
kAc这种傻×必然不会了,于是向你来请教……
T = 10000
N, M <= 10000000
思路:
f(n)表示gcd==n的对数。
g(n)表示gcd的n的倍数的对数。
u(d/p) = mu[d/p]; ans = sigma[p是质数,p<=min(n,m)]sigma[p|d] (u(d/p)*g(d)) = sigma[p是质数,p<=min(n,m)]sigma[p|d] (u(d/p)*(n/d)*(m/d)) = sigma[1<=d<=min(n,m)](n/d)*(m/d)sigma[p是d的约数且p是素数](u(d/p)); 参考:http://www.cnblogs.com/candy99/p/6209609.html */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const LL INF = 1e10;
const int mod = 1e9 + ;
const int maxn = 1e7 + ;
int prime[maxn], tot, not_prime[maxn];
int mu[maxn], sum[maxn];
inline int read()
{
char c = getchar();
int x = ;
while(c<''||c>''){
c = getchar();
}
while(c>=''&&c<='') x = x*+c-,c = getchar();
return x;
}
//法2.
//线性筛
//g[i*p[j]]
//当p[j]|i时结果显然为miu(i)
//否则考虑mu(i*p[j]/pp),当p[j]=pp时为mu[i],p[j]!=pp时的所有的和就是-g(i),所以总的结果为mu(i)-g(i)
/*
void mobius()
{
mu[1] = 1;
tot = 0;
for(int i = 2; i < maxn; i++){
if(!not_prime[i]){
mu[i] = -1;
prime[++tot] = i;
sum[i] = 1;
}
for(int j = 1; prime[j]*i<maxn; j++){
not_prime[prime[j]*i] = 1;
if(i%prime[j]==0){
mu[prime[j]*i] = 0;
sum[prime[j]*i] = mu[i];
break;
}
mu[prime[j]*i] = -mu[i];
sum[prime[j]*i] = mu[i]-sum[i];
}
} for(int i = 1; i < maxn; i++) sum[i] += sum[i-1];
}*/
//法1.
//只需要枚举每个素数,将他的倍数的g更新就可以了
//由于有1/1+1/2+1/3+...+1/n=O(logn)这个结论
//因此每个质数枚举时是均摊O(logn)的(*n后好想,是nlogn,但是质数只有n/logn个)
//而质数恰好有O(n/logn)个 因此暴力枚举就是O(n)的
void mobius()
{
mu[] = ;
tot = ;
for(int i = ; i < maxn; i++){
if(!not_prime[i]){
mu[i] = -;
prime[++tot] = i;
}
for(int j = ; prime[j]*i<maxn; j++){
not_prime[prime[j]*i] = ;
if(i%prime[j]==){
mu[prime[j]*i] = ;
break;
}
mu[prime[j]*i] = -mu[i];
}
} for(int i = ; i <= tot; i++){
for(int j = prime[i]; j < maxn; j+=prime[i]){
sum[j] += mu[j/prime[i]];
}
} for(int i = ; i < maxn; i++) sum[i] += sum[i-]; }
LL solve(int n,int m)
{
if(n>m) swap(n,m);
LL ans = ;
int last;
for(int i = ; i <= n; i = last+){
last = min(n/(n/i),m/(m/i));
ans += (LL)(sum[last]-sum[i-])*(n/i)*(m/i);
}
return ans;
} int main()
{
freopen("YYnoGCD.in","r",stdin);
freopen("YYnoGCD.out","w",stdout);
int n, m;
int T;
T = read();
mobius();
while(T--)
{
n = read();
m = read();
printf("%lld\n",solve(n,m));
}
return ;
}
BZOJ2820 YY的GCD 莫比乌斯+系数前缀和的更多相关文章
- BZOJ2820:YY的GCD(莫比乌斯反演)
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- BZOJ2820 YY的GCD 【莫比乌斯反演】
BZOJ2820 YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, ...
- [BZOJ2820]YY的GCD
[BZOJ2820]YY的GCD 试题描述 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)
[BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...
- 【BZOJ2820】YY的GCD [莫比乌斯反演]
YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 求1<=x<=N, ...
- [Luogu P2257] YY的GCD (莫比乌斯函数)
题面 传送门:洛咕 Solution 推到自闭,我好菜啊 显然,这题让我们求: \(\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]\) 根 ...
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
随机推荐
- 详解vue静态资源打包中的坑与解决方案
本文主要解决: 1.vue-cli默认配置打包后部署至特定路径下静态资源路径错误问题; 2.静态资源打包使用相对路径后css文件引入图片路径错误问题. 一.问题 vue-cli 脚手架生成的默认打包配 ...
- spring的条件装配bean
1 应用程序环境的迁移 问题: 开发软件时,有一个很大的挑战,就是将应用程序从一个环境迁移到另一个环境. 例如,开发环境中很多方式的处理并不适合生产环境,迁移后需要修改,这个过程可能会莫名的出现很多b ...
- Polar Code主要研究者的个人主页(持续更新中........)
Polar Code主要研究者的个人主页(持续更新中........) 1. Polar码的编译码.以及List译码算法,都少不了Ido Tal这位大牛. http://webee.technion. ...
- 深入理解C#中的泛型(一)
为什么要有泛型? 请大家思考一个问题:由你来实现一个最简单的冒泡排序算法.假设没有使用泛型的经验.可能会毫不犹豫的写出下面代码: public class SortHelper { //參数为int数 ...
- leetcode 题解 || Letter Combinations of a Phone Number 问题
problem: Given a digit string, return all possible letter combinations that the number could represe ...
- 镜像上传和Dockerfile
一.镜像上传 1.在https://hub.docker.com 注册一个账号 2.创建一个仓库 3.取到containerID #docker ps 4.commit容器 #docker commi ...
- Windows进程通信 -- 共享内存
享内存的方式原理就是将一份物理内存映射到不同进程各自的虚拟地址空间上,这样每个进程都可以读取同一份数据,从而实现进程通信.因为是通过内存操作实现通信,因此是一种最高效的数据交换方法. 共享内存在 Wi ...
- js设置百分比保留两位小数
CreateTime--2017年8月23日11:03:31Author:Marydon js设置百分比保留两位小数 错误用法: var percent = (num1/num2) * 100%; ...
- Machine Learning:PageRank算法
1. PageRank算法概述 PageRank,即网页排名,又称网页级别.Google左側排名或佩奇排名. 在谷歌主导互联网搜索之前, 多数搜索引擎採用的排序方法, 是以被搜索词语在 ...
- Springmvc UPDATE 数据时 ORA-01858:a non-numeric character was found where a numeric was expected
ORA-01858:a non-numeric character was found where a numeric was expected 异常. 我的代码: 主要是绑定变量带出来的问题. 出错 ...