SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)
4491. Primes in GCD TableProblem code: PGCD |
Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.
Input
First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.
Output
For each test case write one number - the number of prime numbers Johnny wrote in that test case.
Example
Input:
2
10 10
100 100
Output:
30
2791
题解参考:
http://quartergeek.com/eight-gcd-problems/
ans = sigma(p, sigma(d, μ(d) * (n/pd) * (m/pd)))
Let s = pd, then
ans = sigma(s, sigma(p, μ(s/p) * (n/s) * (m/s)))
= sigma(s, (n/s) * (m/s) * sigma(p, μ(s/p)))
Let g(x) = sigma(p, μ(x/p)), then
ans = sigma(s, (n/s) * (m/s) * g(s))
如果我们能预处理g(x)
的话就能和前面一样分块搞了。这个时候我们多么希望g(x)
和μ(x)
一样是积性函数。看完题解后,发现有一个不是积性函数,胜似积性函数的性质。由于题解没有给证明,所以就意淫了一个证明。
考虑质数p'
,g(p'x) = sigma(p | p'x, μ(p'x/p))
。
- 当
x % p' == 0
,那么考虑sigma中的变量p
的所有取值,它和g(x)
中p
是相同的。而μ(x)
这个函数,如果x
有平方因子的话就等于0,因此当p != p'
时μ(p'x/p) = 0
,当p == p'
是,μ(p'x/p) = μ(x)
。所以g(p'x) = μ(x)
。 - 当
x % p' != 0
,同样考虑p
,会发现它的取值只比g(x)
中的p
多出一个p'
。同理按照p
是否等于p'
讨论,可以得到g(p'x) = -g(x) + μ(x)
。
因此g(x)
可以在线性筛素数的时候算出。剩下的就是前缀和、分块了。
/* ***********************************************
Author :kuangbin
Created Time :2013-10-19 22:01:05
File Name :E:\2013ACM\专题学习\数学\莫比乌斯反演\SPOJ_PGCD.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
bool check[MAXN+];
int prime[MAXN+];
int mu[MAXN+];
int g[MAXN+];
int sum[MAXN+];
void Moblus()
{
memset(check,false,sizeof(check));
mu[] = ;
int tot = ;
for(int i = ; i <= MAXN; i++)
{
if(!check[i])
{
prime[tot++] = i;
mu[i] = -;
g[i] = ;
}
for(int j = ;j < tot;j++)
{
if(i * prime[j] > MAXN)break;
check[i*prime[j]] = true;
if(i % prime[j] == )
{
mu[i * prime[j]] = ;
g[i * prime[j]] = mu[i];
break;
}
else
{
mu[i * prime[j]] = -mu[i];
g[i * prime[j]] = -g[i] + mu[i];
}
}
}
sum[] = ;
for(int i = ;i <= MAXN;i++)
sum[i] = sum[i-] + g[i];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
Moblus();
int T;
int n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
if(n > m)swap(n,m);
long long ans = ;
int last = ;
for(int i = ;i <= n;i = last+)
{
last = min(n/(n/i),m/(m/i));
ans += (long long)(sum[last] - sum[i-])*(n/i)*(m/i);
}
printf("%lld\n",ans);
}
return ;
}
SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)的更多相关文章
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)
首先我们来看一道题 BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...
- [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)有多少对. ...
- 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
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...
- Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- ●BZOJ 2820 YY的GCD
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2820 题解: 莫比乌斯反演 先看看这个题:HDU 1695 GCD(本题简化版) HDU 1 ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- BZOJ 2820 YY的GCD(莫比乌斯函数)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820 题意:给定n,m.求1<=x<=n, 1<=y<=m且Gc ...
随机推荐
- 【51Nod】1055 最长等差数列 动态规划
[题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...
- Tju_Oj_3988Password
这个题是给树的前序和中序,输出后序. 做法是根据前序找根,根据根在中序中找中序的左右子树,根据左右子树长度找前序的左右子树,依此递归. 做过之后感觉还是比较基础的,废话不多说,上题上代码. Bob w ...
- Nginx使用笔记
本篇记录使用Nginx的一些tricks. 一.更改默认Web根目录 修改配置文件 Nginx默认的Web根目录是:/usr/share/nginx/html/,一般我们都是习惯的是:/var/www ...
- Django之模板语法
Django框架之第三篇模板语法(重要!!!) 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: ...
- 关于Python IDLE reload(sys)后无法正常执行命令的原因
转载自:http://blog.csdn.net/kxcfzyk/article/details/41414247?utm_source=tuicool&utm_medium=referral ...
- C# IEqualityComparer类型参数写法
最近在使用Union.Except时,由于默认的对比不太好使,所以需要自定义对比器,下面附上代码. class MaterialListComparer : IEqualityComparer< ...
- linux,mac安装sentry
linux,mac安装sentry 最近需要一个日志监视系统所以选择了sentry.以下是用mac安装,看需求量linux安装类似后面的文章会补充. 安装docker https://download ...
- Newtonsoft 反序列化字符串
string json=“[{“name”:”zhangsan”,”age”:”12”},{“name”:”zhangsan”,”age”:”12”}]” 方法1: JArray arr = (JAr ...
- mysql 用init-connect+binlog实现用户操作追踪做access的ip的log记录
在MYSQL中,每个连接都会先执行init-connect,进行连接的初始化.我们可以在这里获取用户的登录名称和thread的ID值.然后配合binlog,就可以追踪到每个操作语句的操作时间,操作人等 ...
- 关于NOIP2018初赛
题面 这次PJ初赛有点傻了,可能是因为兴华水土不服吧(在这荒度了六年级的光阴). 选择题 DDDBBAAAABABBBB 第四题 当时懵了,我啥也不知道,于是就开始蒙 A.LAN B.WAN C.MA ...