library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test LCMConvolution
(test/yukicoder/7107.test.cpp)

Depends on

Code

// verification-helper: PROBLEM https://yukicoder.me/problems/7107

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../mod/mint.cpp"
#include "../../math/moebius.cpp"
#include "../../convolution/divisor.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;

  using M = Mint<int, 998244353>;
  vector<M> emp(n+1,0),way(n+1,0);
  M cnt{0};
  M all{0};

  auto sign=moebius(n);
  using ll = long long;
  for(int i=1;i<=n;i++){
    emp[i]=M((ll)sign[i])*M(1).pow(n/i);
    way[i]=M((ll)sign[i])*M(2).pow(n/i);
    cnt+=emp[i];
    all+=way[i];
  }

  LCMConvolution::zeta(way);
  LCMConvolution::zeta(emp);

  vector<M> dp0(n+1,0),dp1(n+1,0),dp2(n+1,0);
  for(int i=1;i<=n;i++){
    dp0[i]=emp[i]*emp[i];
    dp1[i]=emp[i]*way[i];
    dp2[i]=way[i]*way[i];
  }

  LCMConvolution::moebius(dp0);
  LCMConvolution::moebius(dp1);
  LCMConvolution::moebius(dp2);

  M ans=(all-cnt)*(all-cnt);
  M cof=M(3)/M(4);
  for(int i=1;i<=n;i++){
    ans+=dp0[i];
    ans-=dp1[i]*M(2);
    ans+=dp2[i]*cof.pow(n/i);

    ans-=dp0[i];
    ans+=dp1[i]*M(2);
    ans-=dp2[i];
  }

  cout<<ans<<endl;
  return 0;
}
#line 1 "test/yukicoder/7107.test.cpp"
// verification-helper: PROBLEM https://yukicoder.me/problems/7107

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "mod/mint.cpp"

#line 3 "mod/mint.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename T, T MOD = 1000000007>
struct Mint{
  inline static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator+() const{return *this;}
  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "math/moebius.cpp"

#line 3 "math/moebius.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// [0, n]
vector<int> moebius(int n){
  n++;
  vector<int> pr(n),sq(n);
  using ll = long long;
  for(ll i=2;i<n;i++){
    if(pr[i]) continue;
    for(ll j=i;j<n;j+=i) pr[j]=i;
    for(ll j=i*i;j<n;j+=i*i) sq[j]=1;
  }
  vector<int> sign(n,0);
  sign[1]=1;
  for(ll i=2;i<n;i++){
    if(sq[i]) continue;
    sign[i]=-sign[i/pr[i]];
  }
  return sign;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 1 "convolution/divisor.cpp"

#line 3 "convolution/divisor.cpp"
using namespace std;
#endif
// https://noshi91.hatenablog.com/entry/2019/09/23/002445
//BEGIN CUT HERE
// O(n \log \log n)
namespace DivisorTransform{
  template<typename T, typename F>
  void inc(vector<T> &as,F f){
    assert(as[0]==T(0));
    int n=as.size();
    vector<bool> sieve(n,false);
    for(int p=2;p<n;p++){
      if(sieve[p]) continue;
      for(int k=1;k*p<n;k++){
        sieve[k*p]=true;
        f(as[k],as[k*p]);
      }
    }
  }
  template<typename T, typename F>
  void dec(vector<T> &as,F f){
    assert(as[0]==T(0));
    int n=as.size();
    vector<bool> sieve(n,false);
    for(int p=2;p<n;p++){
      if(sieve[p]) continue;
      for(int k=(n-1)/p;k!=0;--k){
        sieve[k*p]=true;
        f(as[k],as[k*p]);
      }
    }
  }
}
namespace GCDConvolution{
  template<typename T>
  void zeta(vector<T> &as){
    auto f=[](T &lo,T &hi){lo+=hi;};
    DivisorTransform::dec(as,f);
  }
  template<typename T>
  void moebius(vector<T> &as){
    auto f=[](T &lo,T &hi){lo-=hi;};
    DivisorTransform::inc(as,f);
  }
}
namespace LCMConvolution{
  template<typename T>
  void zeta(vector<T> &as){
    auto f=[](T &lo,T &hi){hi+=lo;};
    DivisorTransform::inc(as,f);
  }
  template<typename T>
  void moebius(vector<T> &as){
    auto f=[](T &lo,T &hi){hi-=lo;};
    DivisorTransform::dec(as,f);
  }
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 10 "test/yukicoder/7107.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;

  using M = Mint<int, 998244353>;
  vector<M> emp(n+1,0),way(n+1,0);
  M cnt{0};
  M all{0};

  auto sign=moebius(n);
  using ll = long long;
  for(int i=1;i<=n;i++){
    emp[i]=M((ll)sign[i])*M(1).pow(n/i);
    way[i]=M((ll)sign[i])*M(2).pow(n/i);
    cnt+=emp[i];
    all+=way[i];
  }

  LCMConvolution::zeta(way);
  LCMConvolution::zeta(emp);

  vector<M> dp0(n+1,0),dp1(n+1,0),dp2(n+1,0);
  for(int i=1;i<=n;i++){
    dp0[i]=emp[i]*emp[i];
    dp1[i]=emp[i]*way[i];
    dp2[i]=way[i]*way[i];
  }

  LCMConvolution::moebius(dp0);
  LCMConvolution::moebius(dp1);
  LCMConvolution::moebius(dp2);

  M ans=(all-cnt)*(all-cnt);
  M cof=M(3)/M(4);
  for(int i=1;i<=n;i++){
    ans+=dp0[i];
    ans-=dp1[i]*M(2);
    ans+=dp2[i]*cof.pow(n/i);

    ans-=dp0[i];
    ans+=dp1[i]*M(2);
    ans-=dp2[i];
  }

  cout<<ans<<endl;
  return 0;
}
Back to top page