bzoj 3309 DZY Loves Math——反演+线性筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309
像这种数据范围,一般是线性预处理,每个询问 sqrt (数论分块)做。
先反演一番。然后 f( ) 还不能一个就花 log 的时间,所以要分析性质。
设 n 一共 m 个质因数,其中最大的指数是 t 。
已有 Σ(d|n) f(d)*u(n/d) ,如果 u( ) 的部分含有指数>=2的质因子,就无贡献;所以 u( ) 里每种质因数选1个或0个,一共 2^m 种。
如果 n 里有一个质因子的指数<t ,则卷积的值是0。因为 u 含有的所有集合可以分成含该因子、不含该因子两部分。这两部分含有的集合个数相同,u的符号正好相反,值相同(因为该质因子的有无不影响 f( ) 的值,因为 f( ) 的值是 t 或 t-1),所以求和为0。
所以 n 的质因子必须齐次。那么只有 u( ) 含有所有质因子的时候,f( ) 的值才是 t-1 ,否则都是 t 。除了这两项,其余消成0;再考虑 u( ) 的符号,于是 Σ(d|n) f(d)*u(n/d) = (-1)^(m+1)。
设 g(n) = Σ(d|n) f(d)*u(n/d) ,则 g( ) 可以线性筛。只要记录每个数是否齐次、如果齐次的话次数是几、一共多少种质因子,就能筛了。然后每个询问数论分块即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e7+;
int T,n,m,g[N],pri[N],cnt,c[N],v[N];
ll ans;
bool fx[N],vis[N];
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void calc(int a,int b,int &x,int &y)
{
x=; y=;
while(a%b==)y++,a/=b;
x=a;
}
void init()
{
int lm=1e7;
for(int i=;i<=lm;i++)
{
if(!vis[i])pri[++cnt]=i,g[i]=g[i-]+,fx[i]=c[i]=v[i]=;
else g[i]=g[i-]+(fx[i]?(v[i]&?:-):); for(int j=,k;j<=cnt&&((ll)i*pri[j]<=lm);j++)
{
vis[k=i*pri[j]]=;
if(i%pri[j]==)
{
int d,a;calc(i,pri[j],d,a);
if(d==)
fx[k]=,c[k]=a+,v[k]=;
else if(fx[d]&&c[d]==a+)
fx[k]=,c[k]=c[d],v[k]=v[d]+;
break;
}
else if(fx[i]&&c[i]==)
fx[k]=,c[k]=,v[k]=v[i]+;
}
}
}
int main()
{
init();
T=rdn();
while(T--)
{
n=rdn(); m=rdn(); int nt1,nt2;
if(n>m) swap(n,m);
for(int i=;i<=n;i=min(nt1,nt2)+)
{
nt1=(n/i); nt2=(m/i);
ll d=(ll)nt1*nt2*(g[min(nt1=n/nt1,nt2=m/nt2)]-g[i-]);
ans+=d;
}
printf("%lld\n",ans); ans=;
}
return ;
}
bzoj 3309 DZY Loves Math——反演+线性筛的更多相关文章
- bzoj 3560 DZY Loves Math V - 线性筛 - 扩展欧几里得算法
给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). Input 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. Output 仅一行答案. Sampl ...
- ●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 ...
- 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 [莫比乌斯反演 线性筛]
题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...
- 【BZOJ3309】DZY Loves Math(线性筛)
题目: BZOJ 3309 分析: 首先,经过一番非常套路的莫比乌斯反演(实在懒得写了),我们得到: \[\sum_{T=1}^n \sum_{d|T}f(d)\mu(\frac{T}{d})\lfl ...
- bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表
有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...
随机推荐
- HDU 5304(Eastest Magical Day Seep Group's Summer-环加外向树生成树计数)[Template:Kirchhoff矩阵]
Eastest Magical Day Seep Group's Summer Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 655 ...
- 怎样让Eclipse的智能提示像VS一样霸气
说起来用Eclipse也有一段时间了.相信每个用过的人都知道他的智能提示功能真的是糟糕透了,与VisualStudio2008简直不是一个档次的!我就纳闷了,他为什么不弄好一点呢.今天我实在是忍不住了 ...
- vue-router钩子beforeRouteEnter函数获取到this实例
官方文档: const Foo = { template: `...`, beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 ...
- nyoj43 24 Point game(DFS)
题目43 题目信息 pid=43" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 讨论 ...
- python(32)- 模块练习Ⅱ:使用正则表达式实现计算器的功能
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...
- FTPClient listFiles 阻塞问题
Android端使用 FTPClient 实现上传文件到到filezilla server(filezilla server部署在阿里云服务器)出现 listFiles阻塞.具体的现象是 Ftp Cl ...
- C# Select SelectMany 区别
string[] text = { "Today is 2018-06-06", "weather is sunny", "I am happy&qu ...
- iLBC简要介绍
iLBC(internet lowbitrate codec):是全球著名语音引擎提供商Global IP Sound开发,它是低比特率的编码解码器,提供在丢包时具有的强大的健壮性.iLBC 提供的语 ...
- POI-----POI操作Excel-4、字体
- 05 referer头与防盗链
像上图中的这个效果,当我们在网页里引用站外图片时,常出现这样的情况. ??? 服务器是怎么样知道,这个图片是在站外被引用的呢? 还有在网站的统计结果,统计用户从何而来,如下图 ??? 统计时,是如何得 ...