洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301
https://www.luogu.org/problemnew/show/P3455
就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2了,升级版的整除分块,可以两个变量一起搞
预处理莫比乌斯函数的前缀和之后就可以每次$O(\sqrt{n}+\sqrt{m})$回答
那篇题解里面用了一个技巧:${\lfloor}\frac{{\lfloor}\frac{a}{b}{\rfloor}}{c}{\rfloor}={\lfloor}\frac{a}{bc}{\rfloor}$
(当然a,b,c都为正整数)
证了好久。。。
这么证:
设${\lfloor}\frac{a}{bc}{\rfloor}=p$,则p为整数,且$p<=\frac{a}{bc}<p+1$
则$pc<=\frac{a}{b}<pc+c$
而$pc$与$pc+c$都为整数
因此$pc<={\lfloor}\frac{a}{b}{\rfloor}<pc+c$
所以$p<=\frac{{\lfloor}\frac{a}{b}{\rfloor}}{c}<p+1$
所以${\lfloor}\frac{{\lfloor}\frac{a}{b}{\rfloor}}{c}{\rfloor}=p={\lfloor}\frac{a}{bc}{\rfloor}$
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define N 50100
ll prime[N+],len,mu[N+],dd[N+];
bool nprime[N+];
ll a,c,n,m,k,ans,ed;
int main()
{
ll i,j,T,TT;
mu[]=;
for(i=;i<=N;i++)
{
if(!nprime[i]) prime[++len]=i,mu[i]=-;
for(j=;j<=len&&i*prime[j]<=N;j++)
{
nprime[i*prime[j]]=;
if(i%prime[j]==) {mu[i*prime[j]]=;break;}
else mu[i*prime[j]]=-mu[i];
}
}
for(i=;i<=N;i++) dd[i]=dd[i-]+mu[i];
scanf("%lld",&T);
for(TT=;TT<=T;TT++)
{
scanf("%lld%lld%lld",&n,&m,&k);n/=k;m/=k;
ans=;
if(n>m) swap(n,m);
for(i=;i<=n;i=j+)
{
j=min(n,min(n/(n/i),m/(m/i)));
ans+=(dd[j]-dd[i-])*(n/i)*(m/i);
}
printf("%lld\n",ans);
}
return ;
}
https://www.luogu.org/problemnew/show/P2522
https://www.lydsy.com/JudgeOnline/problem.php?id=2301
这题基本一样的,就是加个容斥。。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define N 50100
ll prime[N+],len,mu[N+],dd[N+];
bool nprime[N+];
ll a,c,n,m,k;
ll calc(ll n,ll m)
{
if(n==||m==) return ;
ll ans=;
if(n>m) swap(n,m);
n/=k;m/=k;
for(ll i=,j;i<=n;i=j+)
{
j=min(n,min(n/(n/i),m/(m/i)));
ans+=(dd[j]-dd[i-])*(n/i)*(m/i);
}
return ans;
}
int main()
{
ll i,j,T,TT;
mu[]=;
for(i=;i<=N;i++)
{
if(!nprime[i]) prime[++len]=i,mu[i]=-;
for(j=;j<=len&&i*prime[j]<=N;j++)
{
nprime[i*prime[j]]=;
if(i%prime[j]==) {mu[i*prime[j]]=;break;}
else mu[i*prime[j]]=-mu[i];
}
}
for(i=;i<=N;i++) dd[i]=dd[i-]+mu[i];
scanf("%lld",&T);
for(TT=;TT<=T;TT++)
{
scanf("%lld%lld%lld%lld%lld",&a,&n,&c,&m,&k);
printf("%lld\n",calc(n,m)-calc(a-,m)-calc(n,c-)+calc(a-,c-));
}
return ;
}
洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301的更多相关文章
- 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)
先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...
- 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...
- 【刷题】洛谷 P3455 [POI2007]ZAP-Queries
题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...
- 洛谷P3455 [POI2007]ZAP-Queries(莫比乌斯反演)
传送门 设$$f(k)=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=k]$$ $$g(n)=\sum_{n|k}f(k)=\lfloor\frac{a}{n}\rflo ...
- 洛谷P3455 [POI2007]ZAP-Queries
题目大意: 给定\(n,m,k,\) 求 \[\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)==k]\] 莫比乌斯反演入门题,先进行一步转化,将每个\( ...
- 洛谷P3455 [POI2007]ZAP-Queries (莫比乌斯反演)
题意:求$\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)==d]$(1<=a,b,d<=50000). 很套路的莫比乌斯反演. $\sum_{i=1}^{n}\ ...
- [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
题面 传送门:洛咕 Solution 这题比这题不懂简单到哪里去了 好吧,我们来颓柿子. 为了防止重名,以下所有柿子中的\(x\)既是题目中的\(d\) 为了方便讨论,以下柿子均假设\(b>=a ...
- BZOJ 1101: [POI2007]Zap
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2262 Solved: 895[Submit][Status] ...
- [BZOJ1101][POI2007]Zap
[BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...
随机推荐
- linux 输入子系统(4) intput_dev 接口描述
Name struct input_dev - represents an input device Synopsis struct input_dev { const char * name; // ...
- Python开发【迭代器】
1.迭代器 1.1.迭代器创建:指定数据创建迭代器(使用iter()和next() ) x = [1, 2, 3] #定义一个列表:<class 'list'> y = iter(x) # ...
- Erlang Garbage Collector
Erlang Garbage Collector | Erlang Solution blog https://www.erlang-solutions.com/blog/erlang-garbage ...
- Java-JDK-bin-Java-JR
Java in JDK\bin vs. Java in JRE\bin (Java in General forum at Coderanch) https://coderanch.com/t/385 ...
- js数据类型简单介绍
JS数据类型 ECMAScript中有5种简单的数据类型:Undefined,Null,Boolean,Number,String.还有一种复杂的数据类型--Object(本质上是由一组无序的名值对组 ...
- 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)
走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...
- HDU1569 方格取数(2) —— 二分图点带权最大独立集、最小割最大流
题目链接:https://vjudge.net/problem/HDU-1569 方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- 一个点亮屏幕的service
这个版本是只能点亮不能解锁的版本(注意很多句子都被注释掉了,那部分是用来实现解锁屏幕的),达到了预期的效果,特此纪念. 把代码贴出来: package com.larry.msglighter; im ...
- SPOJ:PATHETIC STRINGS(分配问题&贪心)
Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the sa ...