bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 1303 Solved: 819
[Submit][Status][Discuss]
Description
对于正整数n,定义f(n)为n所含质因子的最大幂指数。例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0。
给定正整数a,b,求sigma(sigma(f(gcd(i,j)))) (i=1..a, j=1..b)。
Input
第一行一个数T,表示询问数。
接下来T行,每行两个数a,b,表示一个询问。
Output
对于每一个询问,输出一行一个非负整数作为回答。
Sample Input
7558588 9653114
6514903 4451211
7425644 1189442
6335198 4957
Sample Output
14225956593420
4332838845846
15400094813
HINT
【数据规模】
T<=10000

#pragma GCC optimize(2)
#pragma G++ optimzie(2)
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iostream> #define N 10000007
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m;
int pri[N],tot;
int t[N],last[N],g[N];
bool flag[N]; void init()
{
for (int i=;i<N;i++)
{
if(!flag[i])
{
pri[++tot]=i;
last[i]=t[i]=g[i]=;
}
for(int j=;pri[j]*i<N&&j<=tot;j++)
{
int x=i*pri[j];flag[x]=true;
if(i%pri[j]==)
{
last[x]=last[i];
t[x]=t[i]+;
if(last[x]==)g[x]=;
else g[x]=(t[last[x]]==t[x]?-g[last[x]]:);
break;
}
else
{
last[x]=i;
t[x]=;
g[x]=(t[i]==?-g[i]:);
}
}
}
for (int i=;i<N;i++)g[i]+=g[i-];
/* for (int i=11;i<=20;i++)
cout<<"xzpxzpxzpxzpxzpxzp==laji="<<g[i]<<endl;*/
}
ll solve(int n,int m)
{
if(n>m)swap(n,m);ll res=;
for (int i=,last;i<=n;i=last+)
{
last=min(n/(n/i),m/(m/i));
res+=1ll*(n/i)*(m/i)*(g[last]-g[i-]);
}
return res;
}
int main()
{
init();
int T=read();
while(T--)
{
n=read(),m=read();
printf("%lld\n",solve(n,m));
}
}
bzoj 3309 DZY Loves Math 莫比乌斯反演的更多相关文章
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...
- BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]
题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...
- BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表
有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- 【BZOJ3309】DZY Loves Math 莫比乌斯反演+线性筛(好题)
[BZOJ3309]DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10 ...
- 【BZOJ3309】DZY Loves Math - 莫比乌斯反演
题意: 对于正整数n,定义$f(n)$为$n$所含质因子的最大幂指数.例如$f(1960)=f(2^3 * 5^1 * 7^2)=3$,$f(10007)=1$,$f(1)=0$. 给定正整数$a,b ...
随机推荐
- thinkPHP5.0 save和saveAll,新增和更新的问题
今天遇到一个问题,在模型中使用save保存数据之后,使用saveAll继续新增数据,结果报 缺少更新条件,网上搜了下发现一篇文章https://www.jianshu.com/p/1848f61de6 ...
- TP3.2.3 页面跳转后 Cookie 失效 —— 参考解决方案
一.问题描述 接手一个项目,使用ThinkPhp3.2.3,在线上环境( Centos7.4 + Nginx1.14 + MySQL5.7 + PHP7.2.4 )运行没有问题, 在本地环境( php ...
- Spark Streaming 交互 Kafka的两种方式
一.Spark Streaming连Kafka(重点) 方式一:Receiver方式连:走磁盘 使用High Level API(高阶API)实现Offset自动管理,灵活性差,处理数据时,如果某一时 ...
- python+scrapy环境搭建步骤描述
Python3(3.5.4)搭建爬虫系统步骤描述: 1.下载python安装包,路径:https://www.python.org/downloads/windows/ 选择3.5.4版本64位的安 ...
- 无序数组中第K大的数
1. 排序法 时间复杂度 O(nlogn) 2. 使用一个大小为K的数组arr保存前K个最大的元素 遍历原数组,遇到大于arr最小值的元素时候,使用插入排序方法,插入这个元素 时间复杂度,遍历是 O( ...
- 边缘检测 opencv
本次实验使用了两种方法进行了边缘检测,分别使用到了opencv中的两个API函数为Canny()和Sobel()函数.实验后加了Scharr滤波器,它其实是基于Sobel()函数的. 这三个API中的 ...
- Kings(状压DP)
Description 用字符矩阵来表示一个8x8的棋盘,'.'表示是空格,'P'表示人质,'K'表示骑士.每一步,骑士可以移动到他周围的8个方格中的任意一格.如果你移动到的格子中有人质(即'P'), ...
- 2 js的20/80关键知识
1. 2 var a = 1; undefined a 1 alert(a); undefined var b = true; var c = "Hi"; undefined al ...
- 如何使用malloc申请一个二位数组
fscanf(file, "%d", &iVertexNum); // Read number of Vertices double **G = (double **)ma ...
- linux查看80端口占用情况
netstat -anltp|grep 80