4491. Primes in GCD Table

Problem 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 (莫比乌斯反演)的更多相关文章

  1. 【莫比乌斯反演】关于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( ...

  2. [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)有多少对. ...

  3. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  4. 【刷题】BZOJ 2820 YY的GCD

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...

  5. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  6. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  7. ●BZOJ 2820 YY的GCD

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2820 题解: 莫比乌斯反演 先看看这个题:HDU 1695 GCD(本题简化版) HDU 1 ...

  8. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  9. BZOJ 2820 YY的GCD(莫比乌斯函数)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820 题意:给定n,m.求1<=x<=n, 1<=y<=m且Gc ...

随机推荐

  1. J2EE简介

    一,J2EE概念: J2EE的全称为,Java2 Platform Enterprise Edition,Java或java2平台企业版,他是基于java平台或java2平台的标准版,保留并扩展了J2 ...

  2. 20155306 2016-2017-2 《Java程序设计》第八周学习总结

    20155306 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 java.util.loggging包提供了日志功能相 ...

  3. 第11月第21天 php引用 codeigniter cakephp

    1. class CI_Controller { private static $instance; /** * Constructor */ public function __construct( ...

  4. 洛谷 P3916 【图的遍历】反向加边+dfs

    前言: 对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值.因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而dfs一旦你回溯完环上某个点就不会在更新这个点的答案了,做不到 ...

  5. 【ORACLE】创建表空间

    CREATE TABLESPACE dna36 DATAFILE 'D:\oracle\oradata\orcl\dna36.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M ...

  6. xml json

    简单概括的话就是,xml本身是一种格式规范,是一种包含了数据以及数据说明的文本格式规范. 比如,我们要给对方传输一段数据,数据内容是“too young,too simple,sometimes na ...

  7. [转]C++11 随机数学习

    相对于C++ 11之前的随机数生成器来说,C++11的随机数生成器是复杂了很多.这是因为相对于之前的只需srand.rand这两函数即可获取随机数来说,C++11提供了太多的选择和东西. 随机数生成算 ...

  8. input文本框禁止修改文本——disabled和readonly属性的作用及区别

    1.input文本框禁止修改文本 disabled属性:<input type="text" name="name" value="xxx&qu ...

  9. 002_docker构建zookeeper环境

    最近因为要维护公司zk环境,所以自己先得搞一套先玩玩 git地址=>https://github.com/jplock/docker-zookeeper/tree/v3.4.9 一.build ...

  10. Workman启动失败的解决方法 stream_socket_server() has been disabled for security reasons

    1.报如下错误,问题是php版本太低 php -ini 看下你的版本 http://doc2.workerman.net/how-distributed.html 参考: https://blog.c ...