题目链接:http://codeforces.com/gym/101982/attachments

题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c,d],求这样的x,y有多少对。

解题思路:

第一种反演思路:

把下界变换一下

代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e7+;
ll a,b,c,d,mu[maxn],sum[maxn],prime[maxn],tot;
void getMobius(int N){
for(int i=;i<=N;i++) prime[i]=;
mu[]=;
tot=;
for(int i=;i<=N;i++){
if(prime[i]){
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot&&prime[j]*i<=N;j++){
prime[prime[j]*i]=;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
}
int main()
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
getMobius(1e7);
ll ans=;
for(int i=;i<=min(b,d);i++)
ans+=mu[i]*(b/i-(a-)/i)*(d/i-(c-)/i);
printf("%lld\n",ans);
return ;
}

第二种反演思路:

右边全部都是已知的,枚举下可取范围内的d(也就是原来n的倍数,这里n是1)

可以利用容斥原理,先求出[1,b]和[1,d],再减去[1,a-1]和[1,d]以及[1,b]和[1,c-1],最后加上多减的部分[1,a-1]和[1,c-1]。

并且很显然,推演最后得到的式子是可以经过整除分块优化的,只需要预处理出莫比乌斯函数的前缀和即可。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e7+;
ll a,b,c,d,mu[maxn],sum[maxn],prime[maxn],tot;
void getMobius(int N){
for(int i=;i<=N;i++) prime[i]=;
mu[]=;
tot=;
for(int i=;i<=N;i++){
if(prime[i]){
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot&&prime[j]*i<=N;j++){
prime[prime[j]*i]=;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
}
ll solve(ll n,ll m){
ll res=;
for(ll l=,r=;l<=min(n,m);l=r+){
r=min(n/(n/l),m/(m/l));
res+=(sum[r]-sum[l-])*(n/l)*(m/l);
}
return res;
}
int main()
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
getMobius(1e7);
sum[]=;
for(int i=;i<=1e7;i++) sum[i]=sum[i-]+mu[i];
printf("%lld\n",solve(b,d)-solve(a-,d)-solve(c-,b)+solve(a-,c-));
return ;
}

Gym - 101982B Coprime Integers (莫比乌斯反演)的更多相关文章

  1. 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分

    Coprime Arrays CodeForces - 915G Let's call an array a of size n coprime iff gcd(a1, a2, ..., *a**n) ...

  2. 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演

    [题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...

  3. CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和

    传送门 差分是真心人类智慧--完全不会 这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfl ...

  4. Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)

    Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...

  5. F. Coprime Subsequences 莫比乌斯反演

    http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...

  6. nyoj CO-PRIME 莫比乌斯反演

    CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 This problem is so easy! Can you solve it? You are ...

  7. gym 101982 B题 Coprime Integers

    题目链接:https://codeforces.com/gym/101982/attachments 贴一张图吧: 题目意思就是给出四个数字,a,b,c,d,分别代表两个区间[a,b],[c,d],从 ...

  8. CF915G Coprime Arrays (莫比乌斯反演)

    CF915G Coprime Arrays 题解 (看了好半天终于看懂了) 我们先对于每一个i想,那么 我们设 我们用莫比乌斯反演 有了这个式子,可比可以求出△ans呢?我们注意到,由于那个(i/d) ...

  9. Coprime (单色三角形+莫比乌斯反演(数论容斥))

    这道题,先说一下单色三角形吧,推荐一篇noip的论文<国家集训队2003论文集许智磊> 链接:https://wenku.baidu.com/view/e87725c52cc58bd631 ...

随机推荐

  1. vue+element-ui实现行数可控的表格输入

    element的table中使用 <template slot-scope="scope"> </template> 包裹想要插入的input,或者sele ...

  2. SpringAOP术语

    2019-03-10/21:12:31 参考博客:MiroKlose AOP术语 1.通知: 通知定义了切面要完成的工作内容和何时完成工作,就是什么时候去做辅助功能,功能具体是什么代码 五种类型 Be ...

  3. Ueditor图片上传功能的配置

    之前的项目中碰到过图片上传功能的配置问题,但是没有记录下来,今天有个朋友突然又问到了我这个问题,当时没想起来之前怎么解决的,后来看了Ueditor的官方文档才回想起来. 官网文档巨多,一般大家遇到问题 ...

  4. PostGIS计算矢量切片(二)--按值渲染

    方案背景     今年三月份写了一篇postgis计算矢量切片,参考了网上资料给出了一份很粗糙的相关方案(文章写的更粗糙).当时的方案中只能针对gis形状进行渲染,而不能用属性渲染.针对这个情况,本文 ...

  5. android中使用afinal一行源码显示网络图片

    下面代码是关于android中使用afinal一行显示网络图片的代码. public class DemoActivity extends FinalActivity { @Override publ ...

  6. LEDE 虚拟机安装

    虽然我对路由器没什么兴趣,但是紧跟潮流还是有必要的,现在因为网络闭关锁国政策,很多人都想自己搭配一台私人的服务器,不想被商业公司左右数据安全.我感觉这个是一个商机,建议大家可以朝这个方向发展. 这里最 ...

  7. vue build报copy-webpack-plugin] unable to locate异常的解决方法

    ERROR in [copy-webpack-plugin] unable to locate 'J:\xxx\xxx\xxx\xxx\static' at 'J:\xxx\xxx\xxx\xxx\s ...

  8. 如何查看linux中文件打开情况

    前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. linux下有哪些文件 在介绍lsof命 ...

  9. SQLAchemy模块

    老师的博客:http://www.cnblogs.com/wupeiqi/articles/5713330.html 有一篇习详细的博客: http://www.keakon.net/2012/12/ ...

  10. Django ORM 使用原生 SQL

    使用原生sql的 方法 : raw # row方法:(掺杂着原生sql和orm来执行的操作) res = CookBook.objects.raw('select id as nid from epo ...