跳到正文

题解

此时此刻的光辉-加强版 题解

西江月·定风波约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年1月13日
收录
2026年1月13日
更新
2026年11月9日
篇幅
约 14 分钟

版本

  1. v3当前
  2. v2
  3. v1

极其变态的一个题。。。

最早是放在 1010 月中旬杭州市科技节桐庐分赛区选拔赛的,但是很不幸场上这个题被放在了最后导致所有人都来不及写,最高分 44 分。。。但是有学弟会了 7272 分。

后面学弟补题补了一晚上没能卡过我的加强版,但是因为极限数据跑太慢了我不是很满意,就大卡了一下空间,后面卡了几次时间卡进了 44 秒钟。已经造了两种形式的极限数据,应该不太有其他卡法了。不知道有没有比较特殊的人类智慧。

目前好像没看到有大佬拿神秘优化卡过去。

我的代码没有用特别快的输入输出,并且大量运用了 vector,所以要吃波动才能过。其实质因数表完全可以内存池实现,估计会快出不少,但是太懒了还没写(

人类智慧的优化有时候会很有用。如果 ai=ai−1a_i=a_{i-1} 就直接继承前一个数的分解,否则以一定概率记录当前 aia_i 的分解,然后分解质因数的时候查一下当前值是否被记录,被记录就直接继承前面算过的分解。这样可以有效处理极限数据。

然后就是逆元的问题,可以多预处理点逆元,查的时候 O(1)O(1) 查询,可以有效提升速度。

剩下就是基础的卡常。调块长、调筛的质数个数、调 Pollard-Rho 步长和 Miller-Rabin 判的质数个数、输入输出优化、规避类型转换、规避取模、多用常量。

类似这样子,吃评测波动可以跑的相当优秀。

下面是一个实现,最慢点 3.843.84 秒,总用时 6.176.17 秒,可以不用卡波动,稳稳通过:

(Code by Milky_Cat,小奶猫怎么这么强 orz。)

C++
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
typedef unsigned long long ll;
using namespace std;
using namespace __gnu_pbds;
#define BF_SIZE 200000
    bool IOerr=0;
    inline char nc(){
        static char buf[BF_SIZE],*p1=buf+BF_SIZE,*pend=buf+BF_SIZE;
        if(p1==pend){
            p1=buf;
            pend=buf+fread(buf,1,BF_SIZE,stdin);
            if(pend==p1){IOerr=1;return -1;}
        }
        return *p1++;
    }
    inline bool bla(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
    inline void read(ll &x){
        char ch;
        while(bla(ch=nc()));
        if(IOerr){return;}
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    inline void read(int &x){
        char ch;
        while(bla(ch=nc()));
        if(IOerr){return;}
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    #undef BF_SIZE
static char F[200];
inline void write(int x){
    int tmp=x;
    int cnt=0;
    while(tmp>0)
    {
        F[cnt++]=tmp%10+'0';
        tmp/=10;
    }
    while(cnt>0)putchar(F[--cnt]);
}
const ll p[] = {0, 2, 3, 5, 7, 11};
constexpr int mod = 998244853;
vector<pair<ll, int> > vec[100005];
gp_hash_table<ll, int> mp;
gp_hash_table<ll, bool> calced;
gp_hash_table<ll, vector<pair<ll, int> > > mmp;
int n, m;
ll res;
struct que{
    int l, r, id;
}qu[100005];
int ans[100005]; 
int inv[300005], fac[300005];
int qz[100005][10], lsh[1005], pnum;
ll a[100005];
vector<int> pri;
mt19937 eng(time(0));
inline ll rnd(const ll l, const ll r){
    return l + eng() % (r - l + 1);
}
inline ll mul(ll x, ll k, const ll m){
    return (__int128)x * k % m;
}
inline ll qpow(ll x, ll k, const ll m){
    ll ans = 1;
    while(k){
        if (k & 1)
            ans = mul(ans, x, m);
        x = mul(x, x, m);
        k >>= 1;
    }
    return ans;
}
inline bool Miller_Rabin(const ll n, const ll a){
    ll vp = n - 1, r = 0, pan;
    while (!(vp & 1))
        r++, vp >>= 1;
    pan = qpow(a, vp, n);
    if (pan == 1)
        return 1;
    for (int i = 0; i < r; i++){
        if (pan == n - 1)
            return 1;
        pan = mul(pan, pan, n);
    }
    return 0;
}
inline bool isp(const ll n){
    if (n < 2)
        return 0;
    for (int i = 1; i <= 3; i++){
        if (n == p[i])
            return 1;
        if (n % p[i] == 0 || !Miller_Rabin(n, p[i]))
            return 0;
    }
    return 1;
}
inline ll nxt(const ll x, const ll c, const ll n){
    return (mul(x, x, n) + c);
}
inline ll Pollard_Rho(const ll n){
    if (n == 4)
        return 2;
    ll x, y, c = rnd(3, n - 1), res = 1;
    int lim = 32;
    x = y = rnd(3, n - 1);
    x = nxt(x, c, n) % n;
    y = nxt(x, c, n) % n;
    while (x != y){
        ll bans = 1;
        for (int i = 1; i <= lim; i++){
            if (!mul(bans, max(x, y) - min(x, y), n))
                break;
            bans = mul(bans, max(x, y) - min(x, y), n);
            x = nxt(x, c, n);
            y = nxt(nxt(y, c, n), c, n);
				 x %= n, y %= n;
        }
        res = gcd(bans, n);
        if (res != 1)
            return res;
    }
    return n;
}
inline void gene(const ll x, const int id){
    for (auto &tmp : vec[id]){
        if (tmp.first == x){
            tmp.second++;
            return;
        }
    }
    vec[id].push_back({x, 1});
}
void dfs(const ll n, const int id){
    ll a = n, b;
    while (a == n)
        a = Pollard_Rho(a);
    b = n / a;
    if (isp(a)) gene(a, id);
    else dfs(a, id);
    if (isp(b)) gene(b, id);
    else dfs(b, id);
}
inline void add(const int i){
    for (auto tmp : vec[i]){
        ll x = tmp.first;
        int cnt = tmp.second;
        res = res * inv[mp[x] + 1] % mod;
        mp[x] += cnt;
        res = res * (mp[x] + 1) % mod;
    }
}
inline void del(const int i){
    for (auto tmp : vec[i]){
        ll x = tmp.first;
        int cnt = tmp.second;
        res = res * inv[mp[x] + 1] % mod;
        mp[x] -= cnt;
        res = res * (mp[x] + 1) % mod;
    }
}
inline void gpri(){
    pri.emplace_back(2);
    lsh[2] = 0;
    pnum = 1;
    for (int i = 3; i <= 29; i += 2)
        if (isp(i)){
            pri.emplace_back(i);
            lsh[i] = pnum++;
        }
}
signed main(){
    read(n);read(m);
    fac[0] = 1;
    for (int i = 1; i <= 3 * n; i++)
        fac[i] = 1ll * i * fac[i - 1] % mod;
    int ifac = qpow(fac[3 * n], mod - 2, mod);
    for (int i = 3 * n; i; i--){
        inv[i] = 1ll * fac[i - 1] * ifac % mod;
        ifac = 1ll * ifac * i % mod;
    }
    for (int i = 1; i <= n; i++)
        read(a[i]);
    gpri();
    for (int i = 1; i <= n; i++){
        for (int j = 0; j < 10; j++)
            qz[i][j] = qz[i - 1][j];
        for (auto x : pri){
            while (a[i] % x == 0){
                a[i] /= x;
                qz[i][lsh[x]]++;
            }
        }
    }
    for (int i = 1; i <= n; i++){
        if (a[i] == 1)
            continue;
        if (a[i] == a[i - 1]){
            vec[i] = vec[i - 1];
            continue;
        }
        if (isp(a[i])){
            vec[i].emplace_back(make_pair(a[i], 1));
            continue;
        }
        if (calced.find(a[i]) != calced.end()){
        	vec[i] = mmp[a[i]];
        	continue;
		}
        if (eng() % 3 == 0){
            dfs(a[i], i);
            continue;
        }
        dfs(a[i], i);
        calced[a[i]] = 1;
        mmp[a[i]] = vec[i];
    }
    for (int i = 1; i <= m; i++){
        read(qu[i].l);read(qu[i].r);
        qu[i].id = i;
    }
    int kc = 310;
    sort(qu + 1, qu + 1 + m, [kc](que a, que b){
        if (a.l / kc != b.l / kc) return a.l < b.l;
        else return (a.l / kc) & 1 ? a.r < b.r : a.r > b.r;
    });
    int l = 1, r = 0;
    res = 1;
    for (int i = 1; i <= m; i++){
        while (l > qu[i].l)
            add(--l);
        while (r < qu[i].r)
            add(++r);
        while (l < qu[i].l)
            del(l++);
        while (r > qu[i].r)
            del(r--);
        ll tmp = res;
        for (int j = 0; j < 10; j++)
            tmp = tmp * (qz[r][j] - qz[l - 1][j] + 1) % mod;
        ans[qu[i].id] = tmp;
    }
    for (int i = 1; i <= m; i++){
        write(ans[i]);
        putchar('\n');
    }
    return 0;
}
原文
存档评论4 条抓取于