数论公式笔记

 组合数取模  1 - 1e18

#include <iostream>
#include <string.h>
#include <stdio.h>
 
using namespace std;
typedef long long LL;
 
LL n,m,p;
 
LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1)
        {
            ans = ans * a % p;
            b--;
        }
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
 
LL C(LL n, LL m)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a * quick_mod(b, p-2) % p) % p;
    }
    return ans;
}
 
LL Lucas(LL n, LL m)
{
    if(m == 0) return 1;
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d", &n, &m, &p);
        printf("%I64d\n", Lucas(n,m));
    }
    return 0;
}

斯特林公式

void init(){//组合数递推,第一类斯特林数
    c[0][0]=1;
    stir[0][0]=1;

    for(int i=1;i<=2000;i++)
    {
        c[i][0]=c[i][i]=1;
        stir[i][0]=0;
        stir[i][i]=1;
        for(int j=1;j<i;j++)
        {
            c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;   
            stir[i][j]=(stir[i-1][j-1]+stir[i-1][j]*(i-1))%mod;
        }
    }
}

第二类斯特林数:

 

第二类斯特林就是把(n-1)变成m

 错排公式

 D(n)=(n-1)[D(n-1)+D(n-2)];   D(1)=0; D(2)=1

卡特兰数公式

 快速傅里叶变换模板 待更

#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn = 1e6 + 1000;
const double PI = acos(-1.0);

struct Complex{
	double r, i;
	Complex(){ r = 0, i = 0; }
	Complex(double real,double imag): r(real), i(imag){}
}F[maxn], G[maxn];
Complex operator + (Complex a,Complex b){ return Complex( a.r + b.r , a.i + b.i ); }
Complex operator - (Complex a,Complex b){ return Complex( a.r - b.r , a.i - b.i ); }
Complex operator * (Complex a,Complex b){ return Complex( a.r * b.r - a.i * b.i, a.i * b.r + b.i * a.r ); }

int rev[maxn], len, lim = 1;

void FFT(Complex* a,int opt)
{
	for(int i = 0; i < lim; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
	for(int dep = 1; dep <= log2(lim); ++dep)
	{
		int m = 1 << dep;
		Complex wn = Complex(cos(2.0 * PI / m), opt * sin(2.0 * PI / m));
		for(int k = 0; k < lim; k += m)
		{
			Complex w = Complex(1, 0);
			for(int j = 0; j < m / 2; j++)
			{
				Complex t = w * a[k + j + m / 2];
				Complex u = a[k + j];
				a[k + j] = u + t;
				a[k + j + m / 2] = u - t;
				w = w * wn;
			}
		}
	}
	if(opt == -1)for(int i = 0;i < lim; ++i) a[i].r /= lim;
}
signed main()
{
	int n,m;
	scanf("%lld %lld", &n, &m);
	for(int i = 0; i <= n; i++)	scanf("%lf", &F[i].r);
	for(int i = 0; i <= m; i++)	scanf("%lf", &G[i].r);
	while(lim <= n+m)	lim <<= 1,len++;
	for(int i = 0; i < lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
	FFT(F,1); FFT(G,1);
	for(int i = 0; i <= lim; i++)	F[i] = F[i] * G[i];
	FFT(F,-1);
	for(int i = 0; i <= n+m; i++)
		printf("%lld ",(int)(F[i].r + 0.5));  
	return 0;
}
#include <cstdio>
#include <cmath>
#include <algorithm>

const int MAXN = 262144 + 1;
const double PI = std::acos(-1.0);

struct Complex {
    double r, i;

    Complex(double r = 0, double i = 0) : r(r), i(i) {}

    Complex conj() const { return Complex(r, -i); }

    Complex operator+(const Complex &rhs) const { return Complex(r + rhs.r, i + rhs.i); }
    Complex operator-(const Complex &rhs) const { return Complex(r - rhs.r, i - rhs.i); }
    Complex operator*(const Complex &rhs) const { return Complex(r * rhs.r - i * rhs.i, r * rhs.i + i * rhs.r); }
    Complex operator/(double rhs) const { return Complex(r / rhs, i / rhs); }
};

class FFT {
private:
    static const int N = 262144;

    Complex omega[N + 1], omegaInv[N + 1];

    void init() {
        for (int i = 0; i < N; i++) {
            omega[i] = Complex(std::cos(2 * PI / N * i), std::sin(2 * PI / N * i));
            omegaInv[i] = omega[i].conj();
        }
    }

    void reverse(Complex *a, int n) {
        for (int i = 0, j = 0; i < n; i++) {
            if (i < j) std::swap(a[i], a[j]);
            for (int l = n >> 1; (j ^= l) < l; l >>= 1) {}
        }
    }

    void transform(Complex *a, int n, Complex *omega) {
        reverse(a, n);

        for (int l = 2; l <= n; l <<= 1) {
            int hl = l >> 1;
            for (Complex *x = a; x != a + n; x += l) {
                for (int i = 0; i < hl; i++) {
                    Complex t = omega[N / l * i] * x[i + hl];
                    x[i + hl] = x[i] - t;
                    x[i] = x[i] + t;
                }
            }
        }
    }

public:
    FFT() { init(); }

    int extend(int n) {
        int res = 1;
        while (res < n) res <<= 1;
        return res;
    }

    void dft(Complex *a, int n) {
        transform(a, n, omega);
    }

    void idft(Complex *a, int n) {
        transform(a, n, omegaInv);
        for (int i = 0; i < n; i++) a[i] = a[i] / n;
    }
} fft;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    static Complex a[MAXN], b[MAXN];
    for (int i = 0; i <= n; i++) scanf("%lf", &a[i].r);
    for (int i = 0; i <= m; i++) scanf("%lf", &b[i].r);

    int N = fft.extend(n + m + 1);

    fft.dft(a, N);
    fft.dft(b, N);
    for (int i = 0; i < N; i++) a[i] = a[i] * b[i];
    fft.idft(a, N);

    for (int i = 0; i < n + m + 1; i++)
        printf("%.0lf%c", a[i].r + 0.001, " \n"[i == n + m]);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值