結果

問題 No.1760 Setwise Coprime
ユーザー beetbeet
提出日時 2021-11-21 12:04:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 4,018 bytes
コンパイル時間 2,055 ms
コンパイル使用メモリ 208,152 KB
実行使用メモリ 7,692 KB
最終ジャッジ日時 2023-09-04 20:49:09
合計ジャッジ時間 3,946 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 21 ms
6,928 KB
testcase_22 AC 18 ms
6,324 KB
testcase_23 AC 22 ms
7,412 KB
testcase_24 AC 11 ms
5,132 KB
testcase_25 AC 20 ms
6,788 KB
testcase_26 AC 15 ms
5,892 KB
testcase_27 AC 7 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 23 ms
7,368 KB
testcase_30 AC 22 ms
7,572 KB
testcase_31 AC 9 ms
4,504 KB
testcase_32 AC 21 ms
7,180 KB
testcase_33 AC 15 ms
6,064 KB
testcase_34 AC 23 ms
7,432 KB
testcase_35 AC 19 ms
6,576 KB
testcase_36 AC 25 ms
7,644 KB
testcase_37 AC 25 ms
7,692 KB
testcase_38 AC 26 ms
7,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

#define call_from_test
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;}

// [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;
}

// 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);
  }
}

#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;
}
0