首页 > 其他分享 >ACM板子大公开!

ACM板子大公开!

时间:2023-04-01 18:23:11浏览次数:228  
标签:infact 公开 int res ll ACM 板子 fact mod

目前只有非常少的一部分,正在逐渐完善中...

数学

求组合数

ll fact[N], infact[N];

ll qmi(ll a, ll k, ll p){
    ll res = 1;
    while(k){
        if(k & 1)
            res = (ll)res * a % p;
        a = (ll)a * a % p;
        k >>= 1;
    }
    return res;
}

ll C (int a, int b) {
    if (a < b)  return 0;
    return fact[a] * infact[b] % mod * infact[a - b] % mod;
}

void init () {
    fact[0] = infact[0] = 1;
    for(int i = 1; i < N; i++){
        fact[i] = (ll)fact[i-1] * i % mod;
        infact[i] = (ll)infact[i-1] * qmi(i, mod - 2,mod) % mod;
    }
}

线性筛法求Mobius函数

void init (int x) { //线性筛法求mobius函数
    mobius[1] = 1;
    int cnt = 0;
    for (int i = 2; i <= x; i++) {
        if (!vis[i])    prime[++cnt] = i, mobius[i] = -1;
        for (int j = 1; i * prime[j] <= x; j++) {
            int t = i * prime[j];
            vis[t] = true;
            if (i % prime[j] == 0) {
                mobius[t] = 0;
                break;
            }
            mobius[t] = mobius[i] * -1;
        }
    }
    for (int i = 1; i <= x; i++)    sum[i] = sum[i-1] + mobius[i];
}

字符串

Z函数(扩展KMP)

vector<int> ExKMP (string s) { //从0开始
    int n = s.size ();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; i++) {
        if (i <= r && z[i-l] < r - i + 1)   z[i] = z[i-l];
        else {
            z[i] = max (0, r - i + 1);
            while (i + z[i] < n && s[z[i]] == s[i + z[i]])  z[i] ++; //暴力匹配
        }
        if (i + z[i] > r)   l = i, r = i + z[i] - 1; //更新l,r
    }
    return z;
}

标签:infact,公开,int,res,ll,ACM,板子,fact,mod
From: https://www.cnblogs.com/CTing/p/17274321.html

相关文章