結果

問題 No.1781 LCM
ユーザー tko919tko919
提出日時 2021-12-19 00:12:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,037 ms / 5,000 ms
コード長 4,341 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 205,660 KB
実行使用メモリ 7,724 KB
最終ジャッジ日時 2023-08-09 23:56:02
合計ジャッジ時間 9,987 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 5 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,384 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,384 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 4 ms
4,384 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 1,032 ms
7,416 KB
testcase_22 AC 1,026 ms
7,416 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 1,025 ms
7,724 KB
testcase_26 AC 1,037 ms
7,540 KB
testcase_27 AC 1,029 ms
7,400 KB
testcase_28 AC 868 ms
6,984 KB
testcase_29 AC 226 ms
5,132 KB
testcase_30 AC 236 ms
5,040 KB
testcase_31 AC 5 ms
4,376 KB
testcase_32 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

template<int mod=998244353>struct fp {
    int v; static int get_mod(){return mod;}
    int inv() const{
        int tmp,a=v,b=mod,x=1,y=0;
        while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
        if(x<0){x+=mod;} return x;
    }
    fp(ll x=0){init(x%mod+mod);}
    fp& init(int x){v=(x<mod?x:x-mod); return *this;}
    fp operator-()const{return fp()-*this;}
    fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
    fp& operator+=(const fp& x){return init(v+x.v);}
    fp& operator-=(const fp& x){return init(v+mod-x.v);}
    fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
    fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
    fp operator+(const fp& x)const{return fp(*this)+=x;}
    fp operator-(const fp& x)const{return fp(*this)-=x;}
    fp operator*(const fp& x)const{return fp(*this)*=x;}
    fp operator/(const fp& x)const{return fp(*this)/=x;}
    bool operator==(const fp& x)const{return v==x.v;}
    bool operator!=(const fp& x)const{return v!=x.v;}
    friend istream& operator>>(istream& is,fp& x){return is>>x.v;}
    friend ostream& operator<<(ostream& os,const fp& x){return os<<x.v;}
}; using Fp=fp<>;
template<typename T>struct factorial {
    vector<T> Fact,Finv,Inv;
    factorial(int maxx){
        Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
        Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
        rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
        for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
    }
    T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
    T inv(int n){if(n<0)return 0; return Inv[n];}
    T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
    T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
    T nHr(int n,int r,bool inv=0){return nCr(n+r-1,r,inv);}
};

struct PrimeCount{
    const ll N; const ll M;
    vector<int> lo; vector<ll> hi;
    PrimeCount(const ll& _N):N(_N),M(int(sqrt(N))),lo(M+1),hi(M+1){
        rep(i,1,M+1)lo[i]=i-1,hi[i]=N/i-1;
        int cnt=0;
        for(int p=2;p<=M;p++){
            if(lo[p-1]==lo[p])continue;
            const ll n=N/p;
            const ll q=ll(p)*p;
            const int w=M/p;
            const ll L=min(M,N/q);
            for(int i=1;i<=w;i++)hi[i]-=hi[i*p]-cnt;
            const int t=min<int>(sqrt(n),L);
            for(int i=w+1;i<=t;i++)hi[i]-=lo[n/i]-cnt;
            for(int i=L,j=n/L;i>t;j++){
                int c=lo[j];
                while(j+1<=M and lo[j+1]==c)j++;
                c-=cnt;
                for(int e=max<int>(t,n/(j+1));i>e;i--)hi[i]-=c;
            }
            for(int i=M,j=M/p;j>=p;j--){
                const int c=lo[j]-cnt;
                for(int e=j*p;i>=e;i--)lo[i]-=c;
            }
            cnt++;
        }
    }
    ll pi(ll x){
        if(x<=M)return lo[x];
        else return hi[N/x];
    }
};

const int L=401010;
bitset<L> isp;

int main(){
    ll n,m;
    cin>>n>>m;

    PrimeCount pi(m);
    rep(p,2,L)isp[p]=1;
    vector<ll> ps;
    for(int p=2;p*p<=L;p++)if(isp[p]){
        for(int q=p*p;q<=L;q+=p)isp[q]=0;
    }
    rep(p,2,L)if(isp[p])ps.push_back(p);

    Fp pw[50],ret;
    rep(x,1,50)pw[x]=Fp(x).pow(n);
    auto dfs=[&](auto& f,ll x,int i,int e,Fp sigma)->void{ // x*(prime>=ps[i]) まで確定
        ret+=sigma*pw[e+2]; // x*ps[i]
        ret+=sigma*pw[e+1]*pw[2]*(pi.pi(m/x)-i-1); // x*(prime>ps[i])
        if(x*ps[i]*ps[i]<=m){
            f(f,x*ps[i],i,e+1,sigma);
        }
        rep(j,i+1,ps.size()){
            if(x*ps[j]*ps[j]>m)break;
            f(f,x*ps[j],j,1,sigma*pw[e+1]);
        }
    };
    ret+=1; // 1
    ret+=pw[2]*pi.pi(m); // only prime
    rep(i,0,ps.size()){
        if(ps[i]*ps[i]>m)break;
        dfs(dfs,ps[i],i,1,1);
    }
    cout<<ret<<'\n';
    return 0;
}
0