QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#874997#9923. Ma Meilleure Ennemieucup-team5243TL 1ms3712kbC++1711.0kb2025-01-28 23:28:372025-01-28 23:28:37

Judging History

你现在查看的是最新测评结果

  • [2025-01-28 23:28:37]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3712kb
  • [2025-01-28 23:28:37]
  • 提交

answer

#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
using namespace std;
#include <initializer_list>

namespace nachia{

bool IsPrime(unsigned long long x) noexcept {
    if(x <= 1) return false;
    if(x % 2 == 0) return x == 2;
    using u64 = unsigned long long;
    using u128 = __uint128_t;
    u64 d = x-1;
    int s = 0;
    int q = 63;
    while(!(d&1)){ d >>= 1; s++; }
    while(!(d >> q)) q--;
    u64 r = x; for(int t=0; t<6; t++) r*=2-r*x;
    u128 n2 = -(u128)x % x;
    auto red = [=](u128 t) noexcept -> u64 {
        u64 t2 = (t + (u128)((u64)t*-r)*x) >> 64;
        return (t2 >= x || t2 < (t >> 64)) ? t2-x : t2;
    };
    u64 one = red(n2);
    for(u64 base : { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }){
        if(base%x==0) continue;
        u64 a = base = red(base%x*n2);
        for(int e=q-1; e>=0; e--){ a = red((u128)a*a); if((d>>e)&1) a = red((u128)a*base); }
        if(a == one) continue;
        for(int t=1; t<s&&a!=x-one; t++) a = red((u128)a*a);
        if(a != x-one) return false;
    }
    return true;
}

} // namespace nachia

namespace nachia{

int Popcount(unsigned long long c) noexcept {
#ifdef __GNUC__
    return __builtin_popcountll(c);
#else
    c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3));
    c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5));
    c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17));
    c = (c * (~0ull/257)) >> 56;
    return c;
#endif
}

// please ensure x != 0
int MsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
    return 63 - __builtin_clzll(x);
#else
    using u64 = unsigned long long;
    int q = (x >> 32) ? 32 : 0;
    auto m = x >> q;
    constexpr u64 hi = 0x88888888;
    constexpr u64 mi = 0x11111111;
    m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35;
    m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 31;
    q += (m & 0xf) << 2;
    q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf;
    return q;
#endif
}

// please ensure x != 0
int LsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
    return __builtin_ctzll(x);
#else
    return MsbIndex(x & -x);
#endif
}

}


namespace nachia{

std::vector<std::pair<unsigned long long, int>> Factorize(unsigned long long x){
    if(x == 1) return {};
    if(IsPrime(x)) return {{x,1}};
    using u64 = unsigned long long;
    using u128 = __uint128_t;
    u64 X = x;
    std::vector<u64> p;
    for(u64 i=2; i<100; i+=1+i%2) if(x%i==0){ p.push_back(i); while(x%i==0) x/=i; }
    u64 r=1; u128 n2=1;
    auto updX = [&](){
        r = x; for(int t=0; t<6; t++) r*=2-r*x;
        n2 = -(u128)x % x;
    };
    auto red = [&](u128 t) noexcept -> u64 {
        u64 s = ((u128)x*((u64)t*r)) >> 64;
        u64 t2 = t >> 64;
        return t2-s + (t2 < s ? x : 0);
    };
    auto mult = [&](u64 a, u64 b) noexcept { return red((u128)red((u128)a*n2)*b); };
    auto gcd = [](u64 a, u64 b) noexcept {
        if(!a || !b) return a|b;
        int q = LsbIndex(a|b);
        b >>= LsbIndex(b);
        a >>= LsbIndex(a);
        while(a!=b){
            if(a<b){ b-=a; b>>=LsbIndex(b); }
            else{ a-=b; a>>=LsbIndex(a); }
        }
        return a<<q;
    };
    static u64 v = 7001;
    p.push_back(x);
    for(int pi=p.size()-1; pi<(int)p.size(); pi++) while(p[pi] != 1 && !IsPrime(p[pi])){
        x = p[pi]; updX();
        while(p[pi] == x){
            v^=v<<13; v^=v>>7; v^=v<<17; // Xorshift https://www.jstatsoft.org/article/download/v008i14/916
            u64 c = red(v); if(c == 0) continue;
            auto f = [=](u64 a) noexcept -> u64 { return red((u128)a*a+c); };
            u64 a=0, b=f(a);
            u64 buf = 1, sz = 1, nx = 10;
            while(true){
                while(nx != sz && a != b){
                    buf = mult(buf, a<=b?b-a:a-b); sz++;
                    a = f(a); b = f(f(b));
                }
                u64 g = gcd(buf, x);
                if(g != 1){
                    while(p[pi] % g == 0) p[pi] /= g;
                    p.push_back(g);
                    break;
                }
                if(a == b) break;
                nx = sz * 3 / 2;
            }
        }
    }
    std::vector<std::pair<u64, int>> res;
    for(u64 q : p) if(q != 1){
        int e=0; while(X%q == 0){ e++; X/=q; }
        if(e) res.push_back({ q, e });
    }
    return res;
}

unsigned long long Totient(unsigned long long x){
    auto F = Factorize(x);
    for(auto f : F) x -= x / f.first;
    return x;
}

} // namespace nachia


namespace nachia{

template<class Int> Int Gcd(Int a, Int b){
    if(a < 0) a = -a;
    if(b < 0) b = -b;
    if(!a || !b) return a + b;
    while(b){ a %= b; std::swap(a, b); }
    return a;
}

}

namespace nachia{

struct EnumerateDivisors{
    using u64 = unsigned long long;
    u64 raw;
    std::vector<u64> divord;
    std::vector<int> dims;
    std::vector<int> dimcum;
    std::vector<std::pair<u64, int>> I;
    std::vector<std::pair<u64, int>> pfs;
    EnumerateDivisors(
        std::vector<std::pair<unsigned long long, int>> pf
    )
        : pfs(std::move(pf))
    {
        raw = 1;
        int n = pfs.size();
        dims.resize(n);
        dimcum.assign(n+1, 1);
        divord = {1};
        for(int i=0; i<n; i++){
            dims[i] = pfs[i].second;
            dimcum[i+1] = dimcum[i] * (dims[i] + 1);
            int q = dimcum[i];
            for(int t=q; t<dimcum[i+1]; t++) divord.push_back(divord[t-q] * pfs[i].first);
            for(int t=0; t<pfs[i].second; t++) raw *= pfs[i].first;
        }
        I.resize(divord.size());
        for(int i=0; i<dimcum.back(); i++) I[i] = std::make_pair(divord[i], i);
        std::sort(I.begin(), I.end());
    }
    EnumerateDivisors(unsigned long long n)
        : EnumerateDivisors(Factorize(n)) {}
    int id(unsigned long long d) const {
        d = Gcd(d, raw);
        return std::lower_bound(I.begin(), I.end(), d, [](std::pair<u64, int> e, u64 v){ return e.first < v; })->second;
    }
    int numDivisors() const { return dimcum.back(); }
    unsigned long long divisor(int i){ return divord[i]; }
    template<class Elem>
    void Zeta(std::vector<Elem>& A) const {
        int Z = numDivisors();
        for(int d=0; d<(int)dims.size(); d++){
            int w = dims[d] * dimcum[d];
            int y = dimcum[d];
            for(int i=0; i<Z; i+=dimcum[d+1]){
                for(int j=0; j<w; j++) A[i+j+y] += A[i+j];
            }
        }
    }
    template<class Elem>
    void RevZeta(std::vector<Elem>& A) const {
        int Z = numDivisors();
        for(int d=0; d<(int)dims.size(); d++){
            int w = dims[d] * dimcum[d];
            int y = dimcum[d];
            for(int i=0; i<Z; i+=dimcum[d+1]){
                for(int j=w-1; j>=0; j--) A[i+j] += A[i+j+y];
            }
        }
    }
    template<class Elem>
    void Mobius(std::vector<Elem>& A) const {
        int Z = numDivisors();
        for(int d=0; d<(int)dims.size(); d++){
            int w = dims[d] * dimcum[d];
            int y = dimcum[d];
            for(int i=0; i<Z; i+=dimcum[d+1]){
                for(int j=w-1; j>=0; j--) A[i+j+y] -= A[i+j];
            }
        }
    }
    template<class Elem>
    void RevMobius(std::vector<Elem>& A) const {
        int Z = numDivisors();
        for(int d=0; d<(int)dims.size(); d++){
            int w = dims[d] * dimcum[d];
            int y = dimcum[d];
            for(int i=0; i<Z; i+=dimcum[d+1]){
                for(int j=0; j<w; j++) A[i+j] -= A[i+j+y];
            }
        }
    }
};

}

namespace nachia{

// ax + by = gcd(a,b)
// return ( x, - )
std::pair<long long, long long> ExtGcd(long long a, long long b){
    long long x = 1, y = 0;
    while(b){
        long long u = a / b;
        std::swap(a-=b*u, b);
        std::swap(x-=y*u, y);
    }
    return std::make_pair(x, a);
}

} // namespace nachia

namespace nachia{

template<unsigned int MOD>
struct StaticModint{
private:
    using u64 = unsigned long long;
    unsigned int x;
public:

    using my_type = StaticModint;
    template< class Elem >
    static Elem safe_mod(Elem x){
        if(x < 0){
            if(0 <= x+MOD) return x + MOD;
            return MOD - ((-(x+MOD)-1) % MOD + 1);
        }
        return x % MOD;
    }

    StaticModint() : x(0){}
    StaticModint(const my_type& a) : x(a.x){}
    StaticModint& operator=(const my_type&) = default;
    template< class Elem >
    StaticModint(Elem v) : x(safe_mod(v)){}
    unsigned int operator*() const { return x; }
    my_type& operator+=(const my_type& r) { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator+(const my_type& r) const { my_type res = *this; return res += r; }
    my_type& operator-=(const my_type& r) { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator-(const my_type& r) const { my_type res = *this; return res -= r; }
    my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
    my_type& operator*=(const my_type& r){ x = (u64)x * r.x % MOD; return *this; }
    my_type operator*(const my_type& r) const { my_type res = *this; return res *= r; }
    bool operator==(const my_type& r) const { return x == r.x; }
    my_type pow(unsigned long long i) const {
        my_type a = *this, res = 1;
        while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; }
        return res;
    }
    my_type inv() const { return my_type(ExtGcd(x, MOD).first); }
    unsigned int val() const { return x; }
    static constexpr unsigned int mod() { return MOD; }
    static my_type raw(unsigned int val) { auto res = my_type(); res.x = val; return res; }
    my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
    my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};

} // namespace nachia
using Modint = nachia::StaticModint<998244353>;

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    i64 N, M; cin >> N >> M;
    auto divs = nachia::EnumerateDivisors(N);
    i64 n = divs.numDivisors();
    vector<i64> D(n); rep(i,n) D[i] = divs.divisor(i);
    auto dims = divs.dimcum;
    i64 dimn = dims.size() - 1;
    vector<Modint> A(n);
    rep(i,n) A[i] = M / D[i];
    divs.RevMobius(A);
    vector<Modint> C(n); C[0] = -1;
    divs.Mobius(C);
    vector<Modint> dp(n);
    i64 maskx = 1 << dimn;
    vector<i64> offset(maskx), offsetv(maskx,1);
    rep(t,dimn) rep(i,1<<t) offset[i+(1<<t)] = offset[i] + dims[t];
    rep(t,dimn) rep(i,1<<t) offsetv[i+(1<<t)] = offsetv[i] * D[dims[t]];
    for(i64 i=n-1; i>=0; i--){
        i64 mask = 0;
        rep(t,dimn) if((i+dims[t]) % dims[t+1] >= dims[t]) mask |= 1ll << t;
        for(i64 j=mask; j>=1; j=(j-1)&mask){
            dp[i] += C[offset[j]] * dp[i+offset[j]].pow(offsetv[j]);
        }
        dp[i] += A[i];
    }
    cout << dp[0].val() << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

4 4

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

2338 1470

output:

18530141

result:

ok 1 number(s): "18530141"

Test #3:

score: -100
Time Limit Exceeded

input:

941958815880242160 945059392259579928

output:


result: