枚举$d=gcd(i,j)$

然后大力反演

——来自Popoqqq的博客。

然后大力讨论后面的函数的意义即可。

http://blog.csdn.net/popoqqq/article/details/42122413

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define maxn 10000005
#define inf 0x3f3f3f3f int g[maxn],pr[maxn],top,a[maxn],b[maxn];
bool vis[maxn]; void init()
{
memset(vis,false,sizeof vis);
F(i,2,maxn-1)
{
if (!vis[i])
{
pr[++top]=i;
g[i]=1;
a[i]=1;
b[i]=i;
}
F(j,1,top)
{
if ((ll)i*pr[j]>=maxn) break;
vis[i*pr[j]]=true;
if (i%pr[j]==0)
{
a[i*pr[j]]=a[i]+1;
b[i*pr[j]]=b[i]*pr[j];
int tmp=i/b[i];
if (tmp==1) g[i*pr[j]]=1;
else g[i*pr[j]]=(a[tmp]==a[i*pr[j]])?-g[tmp]:0;
break;
}
a[i*pr[j]]=1;
b[i*pr[j]]=pr[j];
g[i*pr[j]]=(a[i]==1?-g[i]:0);
}
}
F(i,2,maxn-1) g[i]=g[i-1]+g[i];
} int t,n,m; int main()
{
init();
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);ll ans=0;
if (n>m) n^=m^=n^=m;
for (int i=1,last=0;i<=n;i=last+1)
{
last=min(n/(n/i),m/(m/i));
ans+=((ll)g[last]-g[i-1])*(n/i)*(m/i);
}
printf("%lld\n",ans);
}
}

  

BZOJ 3309 DZY Loves Math ——莫比乌斯反演的更多相关文章

  1. bzoj 3309 DZY Loves Math 莫比乌斯反演

    DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1303  Solved: 819[Submit][Status][Dis ...

  2. bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...

  3. 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| ...

  4. BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表

    有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...

  5. 【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) ...

  6. ●BZOJ 3309 DZY Loves Math

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...

  7. BZOJ 3309: DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 761  Solved: 401[Submit][Status ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. AzureARM 使用 powershell 扩容系统磁盘大小

    azure中的虚拟机,windows磁盘大小为127G,linux磁盘大小为30G,在很多时候部署应用程序时直接部署到系统磁盘内导致磁盘后期容量不够需要扩容,在执行分区扩容前我们需要先通过Potal或 ...

  2. Yii2中多表关联查询(with、join、joinwith)

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order         (id  order_name   custome ...

  3. perl在linux下通过date获取当前时间

    perl处理文件的时候最好添加上 处理的时间戳,获取系统的时间又多种方法,但是反引号是最原始的,不需要其他外界条件和lib的支持. my $now = `date "+%F %T" ...

  4. (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码

    http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...

  5. 46.Maximum Product Subarray(最大乘积子数组)

    Level:   Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...

  6. kmp 模板

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> ...

  7. OpenCV Haar AdaBoost源码改进据说是比EMCV快6倍

    <pre name="code" class="cpp">#include "Haar.h" #include "lo ...

  8. ccf_201712-2

    题目 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1 ...

  9. 简单的Redis数据迁移

    dump迁移 1.安装redis-dump工具 sudo apt-get install ruby rubygems ruby-devel -y gem sources --add http://ge ...

  10. Tcp 三次握手 四次分手

    看了 余晟以为的 “tcp没那么难吧”,算是对三次握手,四次分手有了一点点理解,记录下来以方便自己以后的查看. 原文链接:https://mp.weixin.qq.com/s?__biz=MzA3MD ...