zoukankan      html  css  js  c++  java
  • 51nod1675 序列变换

    link

    题意:

    给定长为n的序列a,b,下标从1开始,问有多少对x,y满足gcd(x,y)=1且$a_{b_x}=b_{a_y}$?

    $nleq 10^5.$

    题解:

    $a_{b_x}$和$b_{a_y}$是个幌子,定义成$A_x$和$B_y$就好了,没有什么影响。

    考虑倍数反演:记f(i)表示i=gcd(x,y)的方案数;g(i)表示i|gcd(x,y)的方案数。g(i)可以枚举倍数得到。那么有:

    $$egin{equation}g(n)=sum_{n|d}f(d)Longrightarrow f(n)=sum_{n|d}mu(frac{d}{n})g(d)end{equation}$$

    证明

    这里只需要$f(1)=sum_{i=1}^{n}mu(i) imes g(i)$。

    复杂度$mathcal{O}(nlog n)$。

    code:

     1 #include<bits/stdc++.h>
     2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
     3 #define ll long long
     4 #define inf 1000000001
     5 #define y1 y1___
     6 using namespace std;
     7 char gc(){
     8     static char buf[100000],*p1=buf,*p2=buf;
     9     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    10 }
    11 #define gc getchar
    12 ll read(){
    13     char ch=gc();ll x=0;int op=1;
    14     for (;!isdigit(ch);ch=gc()) if (ch=='-') op=-1;
    15     for (;isdigit(ch);ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
    16     return x*op;
    17 }
    18 #define N 100005
    19 int n,a[N],b[N],t[N],mu[N],p[N],cnt;ll ans,g[N];bool vis[N];
    20 void init(int n){
    21     mu[1]=1;
    22     rep (i,2,n){
    23         if (!vis[i]) p[++cnt]=i,mu[i]=-1;
    24         for (int j=1;j<=cnt&&i*p[j]<=n;j++){
    25             vis[i*p[j]]=1;
    26             if (i%p[j]==0) break;
    27             mu[i*p[j]]=-mu[i];
    28         }
    29     }
    30 }
    31 int main(){
    32     n=read();init(n);
    33     rep (i,1,n) a[i]=read();
    34     rep (i,1,n) b[i]=read();
    35     rep (d,1,n){
    36         for (int i=d;i<=n;i+=d) t[b[a[i]]]++;
    37         for (int i=d;i<=n;i+=d) g[d]+=t[a[b[i]]];
    38         for (int i=d;i<=n;i+=d) t[b[a[i]]]--;
    39     }
    40     rep (i,1,n) ans+=mu[i]*g[i];
    41     printf("%lld
    ",ans);
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    python中的赋值与深浅拷贝
    PAT甲级:1089 Insert or Merge (25分)
    PAT甲级:1064 Complete Binary Search Tree (30分)
    navicat 15 破解教程
    SQL
    以初学者的角度理解:SQL实现关系除法
    线性回归与梯度下降(ML作业)
    海明码
    CRC校验原理
    Jupyter notebook总是卡在int[*]怎么解决?
  • 原文地址:https://www.cnblogs.com/bestFy/p/9416648.html
Copyright © 2011-2022 走看看