bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309
凭着上课所讲和与 Narh 讨论推出式子来;
竟然是第一次写数论分块!所以迷惑了半天;
在预处理的筛中也犯了愚蠢的错误...总之全仰仗 Narh 提点了...
所以具体题解就看这里咯:https://www.cnblogs.com/Narh/p/9740786.html
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e7+;
int T,a[xn],b[xn],c[xn],tot,pri[xn],cnt;
ll s[xn];
bool vis[xn];
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void init()
{
int mx=1e7;
for(int i=;i<=mx;i++)
{
if(!vis[i])a[i]=b[i]=c[i]=,pri[++cnt]=i;
if(a[i]==)s[i]=s[i-]+((c[i]&)?:-); else s[i]=s[i-];
for(int j=,k;j<=cnt&&(ll)i*pri[j]<=mx;j++)
{
vis[k=i*pri[j]]=;
if(i%pri[j])//新增pri[j]
{
if(!a[i]||(a[i]&&b[i]!=))a[k]=,b[k]=;
else a[k]=,b[k]=;
c[k]=c[i]+;
}
else
{
int w=k,t=;
while(w%pri[j]==)w/=pri[j],t++;
if(w==||(a[w]&&b[w]==t))a[k]=,b[k]=t;
else a[k]=,b[k]=;
c[k]=c[i];
}
}
}
}
int main()
{
T=rd(); init();
while(T--)
{
int n,m; n=rd(); m=rd();
ll ans=;
if(n>m)swap(n,m);
for(int i=,nxt;i<=n;i=nxt+)
ans+=(ll)(n/i)*(m/i)*(s[nxt=min(n/(n/i),m/(m/i))]-s[i-]);
printf("%lld\n",ans);
}
return ;
}
bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块的更多相关文章
- bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- 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 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 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 ...
随机推荐
- (转)详解shell中>/dev/null 2>&1到底是什么
转 原文地址:https://blog.csdn.net/zouli415/article/details/80651526 前言 相信大家经常能在shell脚本中发现>/dev/null 2& ...
- vs2010 静态使用 opencv 2.46 库
下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...
- 跨多种环境部署 Gearman -改善应用程序性能和降低服务器负载
您可能想要将工作扩散到一个大型机器群体中,或者想要在不同语言和环境之间共享功能,那么开放源码的 Gearman 服务可以让您轻松地将工作分布到网络中的其他机器.本文将介绍 Gearman 的一些典型应 ...
- Python基础之 一 文件操作
文件操作 流程: 1:打开文件,得到文件句柄并赋值给一个变量 2:通过句柄对文件进行操作 3:关闭文件 模式解释 r(读) , w(写) ,a(附加)r+(读写的读), w+(读写的写),a+(读附加 ...
- Spring Data Redis与Jedis的选择(转)
说明:内容可能有点旧,需要在业务上做权衡. Redis的客户端有两种实现方式,一是可以直接调用Jedis来实现,二是可以使用Spring Data Redis,通过Spring的封装来调用.应该使用哪 ...
- 《Spring设计思想》AOP设计基本原理
0.前言 Spring 提供了AOP(Aspect Oriented Programming) 的支持, 那么,什么是AOP呢?本文将通过一个另外一个角度来诠释AOP的概念,帮助你更好地理解和使用Sp ...
- Guice 学习(八)AOP (面向切面的编程)
Guice的AOP还是非常弱的.眼下只支持方法级别上的,另外灵活性也不是非常高. 看例如以下演示样例: Guice支持AOP的条件是: 类必须是public或者package (default) 类不 ...
- C#调用国家气象局天气预报接口
原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...
- MongoDB 自己定义函数
定义 db.system.js.insert({ _id : "TestConcat", value : function TestConcat(s1, s2){ return s ...
- ubuntu怎么打开.7z和.rar的压缩文件
通过 sudo apt-get install p7zip-rar就会自己主动安装p7zip-full和p7zip-rar两个软件. 之后就能够通过鼠标右键选择 提取到此处 直接解压压缩文件. den ...