跳到正文

个人记录

卡常记录(GOODTEK)

fkxr约 30 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月9日
篇幅
约 30 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

(侵权请联系删除)

我们有如下分块代码需要卡常:

code
C++
#define mod 998244353
int sum[345][345];
int a[100005],id[100005],tag[100005];
void Main() {
	int n;in>>n;tag[0]=1;
	int len=sqrt(n);
	int t=pow(n,/*块长*/);
	for(int i=1;i<=t;i++)
		for(int j=0;j<i;j++)
			sum[i][j]=1;
	for(int i=1;i<=n;i++){
		in>>a[i];id[i]=i/len,tag[i]=1;
		for(int j=1;j<=t;j++)
			sum[j][i%j]*=a[i],
			sum[j][i%j]%=mod;
	}
	int q;in>>q;
	for(;q--;){
		int l,r,x;in>>l>>r>>x;
		int q=id[l],w=id[r];
		if(q==w)
			for(int i=l;i<=r;i++)a[i]=a[i]*x%mod;
		else{
			for(int i=l;id[i]==q;i++)a[i]=a[i]*x%mod;
			for(int i=r;id[i]==w;i--)a[i]=a[i]*x%mod;
			for(int i=q+1;i<w;i++)tag[i]=tag[i]*x%mod;
		}
		for(int i=1;i<=t;i++)
			for(int j=0;j<i;j++)
				sum[i][j]=sum[i][j]*power(x,(r-j+i)/i-(l-j-1+i)/i,mod);
		int s,k;in>>s>>k;
		if(k>t){
			int ans=1;
			for(int i=s;i<=n;i+=k)ans=ans*tag[id[i]]%mod*a[i]%mod;
			out<<ans<<"\n";
		}else out<<sum[k][s%k]<<"\n";
	}
}

记块长为 BB,n,mn,m 同阶则该代码的时间复杂度为

O(nn+n2B+nB2log⁡n)O(n\sqrt{n}+\frac{n^2}{B}+nB^2\log n)

考虑光速幂消掉 3030 行快速幂带来的 log⁡n\log n,则时间复杂度变为:

O(nn+n2B+nB2+nn)=O(nn+n2B+nB2)O(n\sqrt{n}+\frac{n^2}{B}+nB^2+n\sqrt{n})=O(n\sqrt{n}+\frac{n^2}{B}+nB^2)

正常算的话 B=n13B=n^{\frac{1}{3}} 有最优时间复杂度

O(nn+n2n13+n×n132)=O(nn+n53+n53)=O(n53)O(n\sqrt{n}+\frac{n^2}{n^{\frac{1}{3}}}+n\times{n^{\frac{1}{3}}}^2)=O(n\sqrt{n}+n^{\frac{5}{3}}+n^{\frac{5}{3}})=O(n^{\frac{5}{3}})

虽然是理论最优,但还远远不能通过此题,更无法实践爆标。因为代码中除法和取模太多,本地实测最坏情况下跑了 3s3s,评测姬 1.5s1.5s。

考虑出题人造数据的方式:如果步长=rand()%n\text{步长}=\text{rand()}\%n 的话,只有不到 1300\frac{1}{300} 的概率要查根分的表,观察大样例可得:每次的步长不是几千几万就是一百以下,说明 make.cpp\text{make.cpp} 大概是:

Python
import random
if random.randint(0,1):
    print(random.randint(1,值1))
else:
    print(random.randint(1,100000))

所以处理 n>值1n>\text{值1} 的表大概率是在浪费,将块长取到 B=n0.25B=n^{0.25},可得时间复杂度是:

O(nn+nP(B≤k)nk+nP(k<B)n+n46)O(n\sqrt{n}+nP(B\le k)\frac{n}{k}+nP(k<B)\sqrt{n}+n^{\frac{4}{6}})

又 P(B≤k)P(B\le k) 非常小,所以时间复杂度趋近于

O(nn)O(n\sqrt{n})

实际上比 std 优很多,比 std 跑下来快一倍,成功实践爆标。

如何卡这个代码:

  • 专门卡:x=random.randint(sqrt(sqrt(n)),sqrt(n))
  • 一起卡掉其他这类性质的代码:x=random.randint(1,sqrt{n})**2+随机扰动

实际表现:

std with fastio(提交记录)

std with fastio
C++
#include <bits/stdc++.h>
#define endl "\n"
typedef long long i64;
using namespace std;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#define __int128 __int128_t
#endif

#define ds(x) (x=='\r'||x=='\n'||x==' ')
#define MAX 20
namespace fastIO {//吾常于「oi-wiki」习,自悟「快读快写」之术。
	template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; }
	template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); }
	struct FIstream {
		inline FIstream operator>>(int& a) { r(a); return{}; }
		inline FIstream operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; }
		inline FIstream operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; }
		template<typename T>inline FIstream operator<<(T& a) { r(a); return{}; }
		inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } }
	}in;
	struct FOstream {
		inline FOstream operator<<(const int a) { w(a); return{}; }
		inline FOstream operator<<(const char ch) { pc(ch); return{}; }
		inline FOstream operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; }
		template<typename T>inline FOstream operator>>(const T a) { w(a); return{}; }
	}out;
}using fastIO::in; using fastIO::out;
#undef ds
#define eout cerr

const int Mod = 998244353;
int Pow(int a, int b){
    int res = 1;
    for( ; b ; b >>= 1, a = 1LL * a * a % Mod) if(b & 1) res = 1LL * res * a % Mod;
    return res;
}

const int Maxn = 1e5 + 10;
const int MaxB = 400;
int N, Q, B, P[Maxn];

int pur[MaxB][MaxB], Dif[MaxB][MaxB], All[MaxB];
int Cnt[MaxB][MaxB];

int L[Maxn], R[Maxn], Bel[Maxn];
int Prod[Maxn], Sig[Maxn];

void update(int l, int r, int d){
    int cnt = 0, k = 1;
    int invd = Pow(d, Mod - 2);
    for(int i = B;i >= 1;i--){
        if((r - l + 1) / i > cnt){
            k = 1LL * k * Pow(d, (r - l + 1) / i - cnt) % Mod;
            cnt = (r - l + 1) / i;
        }
        All[i] = 1LL * All[i] * k % Mod;
        int x = (l - 1) % i, y = (r - 1) % i;
        if((l - 1) % i == r % i) continue;
        if(d == 0){
            Cnt[i][x] ++;
            Cnt[i][y + 1] --;
        }
        else{
            Dif[i][x] = 1LL * Dif[i][x] * d % Mod;
            Dif[i][y + 1] = 1LL * Dif[i][y + 1] * invd % Mod;
        }
        if(x > y){
            if(d == 0) Cnt[i][0] ++;
            else Dif[i][0] = 1LL * Dif[i][0] * d % Mod;
        }
    }

    if(Bel[l] == Bel[r]){
        for(int i = l;i <= r;i++) Sig[i] = 1LL * Sig[i] * d % Mod;
    }
    else{
        for(int i = l;i <= R[l];i++) Sig[i] = 1LL * Sig[i] * d % Mod;
        for(int i = L[r];i <= r;i++) Sig[i] = 1LL * Sig[i] * d % Mod;
        for(int i = Bel[l] + 1;i < Bel[r];i++) Prod[i] = 1LL * Prod[i] * d % Mod;
    }
}

int query(int s, int k){
    if(k <= B){
        int res = 1LL * pur[k][(s - 1) % k] * All[k] % Mod;
        int _ = 0;
        for(int i = 0;i <= (s - 1) % k;i++){
            _ += Cnt[k][i];
            res = 1LL * res * Dif[k][i] % Mod;
        }
        return res * (_ == 0);
    }

    int res = 1;
    for(int i = s;i <= N;i += k) res = 1LL * res * Sig[i] % Mod * Prod[Bel[i]] % Mod * P[i] % Mod;
    return res;
}

signed main(){
    /*freopen("goodtek.in", "r", stdin);
    freopen("goodtek.out", "w", stdout);*/

    in << N;
    for(int i = 1;i <= N;i++) in << P[i];

    B = std::sqrt(N);
    for(int i = 0;i <= B;i++){
        All[i] = 1;
        for(int j = 0;j <= B;j++){
            pur[i][j] = Dif[i][j] = 1;
        }
    }
    for(int i = 1;i <= B;i++){
        for(int j = 1;j <= N;j++){
            pur[i][(j - 1) % i] = 1LL * pur[i][(j - 1) % i] * P[j] % Mod;
        }
    }

    for(int i = 1;i <= N;i++){
        L[i] = (i - 1) / B * B + 1;
        R[i] = std::min(L[i] + B - 1, N);
        Bel[i] = (i - 1) / B + 1;
        Prod[i] = Sig[i] = 1;
    }

    in << Q;
    while(Q --){
        int l, r, p, s, k;
        in<<l << r << p << s << k;
        update(l, r, p);
        out>>query(s, k);
        pc('\n');
    }
    return 0;
}

my sol(提交记录)

my sol
C++
// @author       fkxr(luogu uid = 995934)
// https://hydro.ac/d/Fkxr/p/IO
// CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/
//#define luogu
#ifndef luogu
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#else
#include <bits/stdc++.h>
#endif
#ifndef db
#define db 0
#endif
#define endl cerr<<"------------------I Love the Olympiad in Informatics------------------\n";
#define int long long
#define _CRT_SECURE_NO_WARNINGS 1
#define _4x _4x
#define all(x) x.begin(),x.end()
//#define BIT BIT
//#define ST ST
//#define dsu dsu
using namespace std;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#define __int128 __int128_t
#endif

#define ds(x) (x=='\r'||x=='\n'||x==' ')
#define MAX 20
namespace fastIO {//吾常于「oi-wiki」习,自悟「快读快写」之术。
	template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; }
	template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); }
	struct FIstream {
		inline FIstream operator>>(int& a) { r(a); return{}; }
		inline FIstream operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; }
		inline FIstream operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; }
		template<typename T>inline FIstream operator<<(T& a) { r(a); return{}; }
		inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } }
	}in;
	struct FOstream {
		inline FOstream operator<<(const int a) { w(a); return{}; }
		inline FOstream operator<<(const char ch) { pc(ch); return{}; }
		inline FOstream operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; }
		template<typename T>inline FOstream operator>>(const T a) { w(a); return{}; }
	}out;
}using fastIO::in; using fastIO::out;
#undef ds
#define eout cerr

namespace Maths {//吾常赴「MO」之赛,自悟「数学」之术。
	const bool __is_P[] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1 };
	inline bool IP1(const int a) { if (a <= 29)return __is_P[a]; if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0)return 0; for (int i = 6;; i += 6) { if (((i + 1) * (i + 1)) > a)return 1; if (a % (i + 1) == 0)return 0; if (((i + 5) * (i + 5)) > a)return 1; if (a % (i + 5) == 0)return 0; } }
#define times(a,b,m) (c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m,c<m?c:m+c)
	inline int power(int a, int b, const int mod = -1) { unsigned long long c; int ans = 1; if (mod == -1) { for (; b;) { if (b & 1)ans *= a; b >>= 1; a *= a; }return ans; }for (; b;) { if (b & 1)ans = times(ans, a, mod); b >>= 1; a = times(a, a, mod); }return ans; }
	const int Suk[] = { 2,325,9375,28178,450775,9780504,1795265022 };
	inline bool chk(const int n, int a, int b, int x) { if (x >= n) return 1; unsigned long long c; int v = power(x, a, n); if (v == 1)return 1; int j = 1; while (j <= b) { if (v == n - 1)break; v = times(v, v, n); j++; }if (j > b)return 0; return 1; }
	inline bool IP(int n) { if (n < 3 || n % 2 == 0)return n == 2; if (n <= 1e6) { return IP1(n); } else { int a = n - 1, b = 0; while (a % 2 == 0)a >>= 1, b++; for (int k : Suk)if (!chk(n, a, b, k))return 0; return 1; } }
#undef times
} using Maths::power;
using Maths::IP;
namespace exs {
#ifdef _4x
	int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
#else
	int dx[] = { 1,0,-1,-1,1,1,0,-1 }, dy[] = { 1,1,1,0,0,-1,-1,-1 };
#endif
	template<typename T, typename T1, typename T2>inline bool bt(T l, T2 x, T1 r) { return l <= x && x <= r; }
	inline bool emc(const int& a, const int& b) { return a > b; }
#define popcti(val) ((int)__builtin_popcount(val))
#define popct(val) ((int)__builtin_popcountll(val))
#ifndef _MSC_VER
	void Freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
#endif

#ifdef BIT//吾常读「《算法竞赛进阶指南》」之书,自悟「树状数组」之术。
	struct bit {
		vector<int> c0, c1; int n;
		inline void Add(vector<int>& c, int p, int v) { for (; p <= n; p += p & -p) c[p] += v; }
		inline int Sum(const vector<int>& c, int p) { int t = 0; for (; p; p -= p & -p) t += c[p]; return t; }
		inline int sum(int l, int r) { assert(n != 0); return Sum(c0, r) * r - Sum(c1, r) - Sum(c0, l - 1) * (l - 1) + Sum(c1, l - 1); }
		inline void add(int l, int r, int v) { assert(n != 0); Add(c0, l, v); Add(c0, r + 1, -v); Add(c1, l, (l - 1) * v); Add(c1, r + 1, -r * v); }
		inline void init(const vector<int>& c) { n = c.size() - 1; c0.resize(n + 2); c1.resize(n + 2); int last = 0; for (int i = 1; i <= n; i++) { last = c[i] - last; Add(c0, i, last); Add(c1, i, last * (i - 1)); last = c[i]; } }
	};
#endif

#ifdef ST//吾常读「《算法竞赛进阶指南》」之书,自悟「ST表」之术。
	struct st {
		vector<int> lg; vector<vector<int>> f;
		inline void init(const vector<int>& c) { int len = c.size() - 1; lg.resize(len + 2); for (int i = 2; i <= len; i++) lg[i] = lg[i >> 1] + 1; f.resize(18, vector<int>(len + 2)); for (int i = 1; i <= len; i++) f[0][i] = c[i]; for (int j = 1; (1 << j) <= len; j++) for (int i = 1; i + (1 << j) - 1 <= len; i++) f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]); }
		inline int q(int l, int r) { int j = lg[r - l + 1]; return max(f[j][l], f[j][r - (1 << j) + 1]); }
	};
#endif

#ifdef dsu//0x 吾常读「《算法竞赛进阶指南》」之书,自悟「并查集」之术。
	struct dsu {
		vector<int>fa;
		dsu(int n):fa(n){iota(all(fa),0);}
		inline int find(int x){return fa[x]==x?x:fa[x]=fa[fa[x]]=fa[fa[fa[x]]]=find(fa[fa[fa[x]]]);}
		inline void unite(int x,int y){fa[find(x)]=find(y);}
		//x->y
	};
#endif
}using namespace exs;
#define mod 998244353
int sum[345][345];
int a[100005];
int id[100005];
int tag[100005];
int pw[345][2];
void Main() {
	int n;in>>n;
	int len=sqrt(n);
	int t=pow(n,0.25);
	for(int i=1;i<=t;i++)
		for(int j=0;j<i;j++)
			sum[i][j]=1;
	tag[0]=1;
	for(int i=1;i<=n;i++){
		in>>a[i];id[i]=i/len;
		tag[i]=1;
		for(int j=1;j<=t;j++)
			sum[j][i%j]*=a[i],
			sum[j][i%j]%=mod;
	}
	int q;in>>q;
	for(;q--;){
		int l,r,x;
		in>>l>>r>>x;
		int q=id[l],w=id[r];
		if(q==w)
			for(int i=l;i<=r;i++)a[i]=a[i]*x%mod;
		else{
			for(int i=l;id[i]==q;i++)a[i]=a[i]*x%mod;
			for(int i=r;id[i]==w;i--)a[i]=a[i]*x%mod;
			for(int i=q+1;i<w;i++)tag[i]=tag[i]*x%mod;
		}
		pw[0][0]=pw[0][1]=1;
		int tmp=sqrt(r-l+1);
		for(int i=1;i<=tmp+2;i++)
			pw[i][0]=pw[i-1][0]*x%mod;
		for(int i=1;i<=tmp+2;i++)
			pw[i][1]=pw[i-1][1]*pw[tmp][0]%mod;
		for(int i=1;i<=t;i++)
			for(int j=0;j<i;j++){
				int cnt=(r-j+i)/i-(l-j-1+i)/i;
				sum[i][j]=sum[i][j]*pw[cnt/tmp][1]%mod*pw[cnt%tmp][0]%mod;
			}
		int s,k;in>>s>>k;
		if(k>t){
			int ans=1;
			for(int i=s;i<=n;i+=k)
				ans=ans*tag[id[i]]%mod*a[i]%mod;
			out<<ans<<"\n";
			continue;
		}
		out<<sum[k][s%k]<<"\n";
	}
}
signed main() {
	Main();
	return 0;
}

不仅用时和空间都优于 std,而且思路也明显简单于 sol,下面放一段 sol 时间复杂度的证明:

这时,我们需要思考多出来的 log⁡N\log N 怎么处理,从何而来。

其实很显然,就是做 pcp ^ c 贡献时的快速幂,但是看起来这个 log⁡N\log N 不可避免,但是还能优化。

就是,当 kk 从大往小遍历时, cc 只增不减,所以可以只在原先基础上再接着乘,这个优化是一个很有力度的优化。

在 B=NB = \sqrt N 时,这个过程的计算量大约在 2N2\sqrt N 左右。

因此可以做到 O(NN)O(N\sqrt N) 。

关于该过程的计算量计算需要用到一个 Stirling’s approximation ,即

N!≈2πN(Ne)NN! \approx \sqrt {2\pi N} (\dfrac Ne) ^ N

这个过程的计算量可以约为这个式子

∑k=1Nlog⁡(Nk−Nk+1)=∑k=1Nlog⁡(Nk(k+1))≈(∑k=1Nlog⁡N)−2(∑k=1Nlog⁡k)=Nlog⁡N−2log⁡(N!)≈Nlog⁡N−2log⁡(2πN(Ne)N)≈Nlog⁡N−2log⁡(N(Ne)N)=Nlog⁡N−12log⁡N−2Nlog⁡(Ne)≈Nlog⁡N−2Nlog⁡(N)+2N=2N\begin{aligned} &\sum_{k = 1}^{\sqrt N} \log(\dfrac Nk - \dfrac N{k + 1}) \\ =& \sum_{k = 1}^{\sqrt N} \log(\dfrac N{k(k + 1)}) \\ \approx& \Big(\sum_{k = 1}^{\sqrt N} \log N\Big) - 2\Big(\sum_{k = 1}^{\sqrt N} \log k\Big) \\ =& \sqrt N \log N - 2\log (\sqrt N !) \\ \approx& \sqrt N \log N - 2\log \big(\sqrt {2\pi \sqrt N} (\dfrac {\sqrt N}e) ^ {\sqrt N} \big) \\ \approx& \sqrt N \log N - 2\log \big(\sqrt {\sqrt N} (\dfrac {\sqrt N}e) ^ {\sqrt N} \big) \\ =& \sqrt N \log N - \dfrac 12\log N - 2\sqrt N\log (\dfrac {\sqrt N}e) \\ \approx& \sqrt N \log N - 2\sqrt N\log ({\sqrt N}) + 2\sqrt N \\ =& 2\sqrt N \end{aligned}

所以复杂度可以保证。

最后的最后:昨天一连两场 CSP 赛前信心赛给我打的没信心了,这套题前两题的确好,我考得也不错,感谢今天的出题人!祝大家 RP++ 喵。

打模拟赛

教练精心准备了 CSPS 前的信心赛。

心态 +3

原文