結果

問題 No.1760 Setwise Coprime
ユーザー beetbeet
提出日時 2021-11-21 11:56:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,910 bytes
コンパイル時間 1,978 ms
コンパイル使用メモリ 204,408 KB
実行使用メモリ 4,920 KB
最終ジャッジ日時 2023-09-04 20:40:23
合計ジャッジ時間 10,692 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
権限があれば一括ダウンロードができます

ソースコード

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

// 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

const int MAX = 2e5+10;
int pr[MAX]={};
int sq[MAX]={};
int sign[MAX]={};

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

  for(int i=2;i<MAX;i++){
    if(pr[i]) continue;
    for(int j=i;j<MAX;j+=i) pr[j]=i;
    for(int j=i*i;j<MAX;j+=i*i) sq[j]=1;
  }

  int n;
  cin>>n;

  sign[1]=1;
  for(int i=2;i<=n;i++){
    if(sq[i]) continue;
    sign[i]=-sign[i/pr[i]];
  }

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

  for(int i=1;i<=n;i++){
    way[i]=M(sign[i])*M(2).pow(n/i);
    emp[i]=M(sign[i])*M(1).pow(n/i);
    all+=way[i];
    cnt+=emp[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+=dp2[i]*cof.pow(n/i);
    ans-=M(2)*dp1[i];
    ans+=dp0[i];

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

  cout<<ans<<endl;
  return 0;
}
0