51nod 1675 序列变换(莫比乌斯反演)
来源:互联网 发布:淘宝客网站建站 编辑:程序博客网 时间:2024/06/05 16:09
1675 序列变换
题目来源: 原创
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
lyk有两序列a和b。
lyk想知道存在多少对x,y,满足以下两个条件。
1:gcd(x,y)=1。
2: abx = bay 。
例如若a={1,1,1},b={1,1,1}。那么存在7对,因为除了x=2,y=2或x=3,y=3外都满足条件。
Input
第一行一个数n(1<=n<=100000)。接下来一行n个数,表示ai(1<=ai<=n)。接下来一行n个数,表示bi(1<=bi<=n)。
Output
一行表示答案
Input示例
31 1 11 1 1
Output示例
7
alpq654321 (题目提供者)
Visual C++的运行时限为:1000 ms ,空间限制为:131072 KB 示例及语言说明请按这里
允许其他 AC 的用户查看此代码,分享代码才能查看别人的代码并有机会获得勋章
一道莫比乌斯反演模板题。
我们可以定义:
则
由莫比乌斯反演第二种形式,得到
当
总时间复杂度
#include<set>#include<map> #include<stack> #include<queue> #include<vector> #include<string>#include<math.h> #include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<algorithm> #include<functional> using namespace std; #define ll long long #define inf 1000000000 #define mod 1000000007 #define maxn 200008#define lowbit(x) (x&-x) #define eps 1e-9ll mu[maxn],a[maxn]={1,1},b[maxn];ll aa[maxn],bb[maxn],sum[maxn],n;ll work(ll x){ll i,res=0;for(i=x;i<=n;i+=x)sum[aa[bb[i]]]++;for(i=x;i<=n;i+=x)res+=sum[bb[aa[i]]];for(i=x;i<=n;i+=x)sum[aa[bb[i]]]--;return res;}ll solve(){ll ans=0,i;for(i=1;i<=n;i++)if(mu[i])ans+=mu[i]*work(i);return ans;}int main(void){ll i,j,cnt=0;mu[1]=1;for(i=2;i<maxn;i++){if(a[i]==0)b[++cnt]=i,mu[i]=-1;for(j=1;j<=cnt && i*b[j]<maxn;j++){a[b[j]*i]=1;if(i%b[j]==0){mu[i*b[j]]=0;break;}elsemu[b[j]*i]=-mu[i];}}scanf("%lld",&n);for(i=1;i<=n;i++)scanf("%lld",&aa[i]);for(i=1;i<=n;i++)scanf("%lld",&bb[i]);printf("%lld\n",solve());return 0;}
阅读全文
0 0
- 51nod 1675 序列变换 莫比乌斯反演
- 51nod 1675 序列变换 莫比乌斯反演
- 51nod 1675 序列变换(莫比乌斯反演)
- 51nod 1675 序列变换 莫比乌斯反演(第二种形式)
- 51nod 欢乐手速场1(迎新春)-B序列变换(莫比乌斯反演)
- 51nod 1675 序列变换 (简单莫比乌斯)
- 51Nod 1675 莫比乌斯反演
- 51nod 序列变换 [容斥原理+莫比乌斯函数]【数论+组合数学】
- 浅谈变换(证明)→反演→莫比乌斯反演→线筛运用
- 51nod 1190 最小公倍数之和 V2(莫比乌斯反演)
- 51nod 1190 最小公倍数之和 V2(莫比乌斯反演)
- 51nod 1355 斐波那契的最小公倍数 (数论+莫比乌斯反演)
- 51nod 1192GCD表中的质数(莫比乌斯反演+双分块)
- 数学(论)里的一些定理(莫比乌斯反演,傅立叶变换,数论变换...)
- [莫比乌斯反演 积性函数前缀和] 51Nod 1190 最小公倍数之和 V2
- [莫比乌斯反演 复杂度分析] 51Nod 1222 最小公倍数计数
- [莫比乌斯反演] 51Nod 1584 加权约数和
- 51nod 1192 Gcd表中的质数 莫比乌斯反演
- 如何自学好编程
- jquery和JavaScript动态创建节点
- 【一步步学OpenGL 1】-《打开一个窗口》
- JAVA类连接JDBC mysql
- Java日记(7)— 访问控制符
- 51nod 1675 序列变换(莫比乌斯反演)
- 怎样进行可行性研究?
- Window安装Nginx并设置为开机启动
- Android Path相关APIs
- 加密篇
- 唯一一道IO题
- Codeforces 499C:Crazy Town(计算几何)
- 训练日记-22
- 【基础知识整理】什么是启发式?