1. 程式人生 > 實用技巧 >NOI Section 2 數論

NOI Section 2 數論

目錄

  • 錯拍問題
  • 同餘方程
  • 容斥原理
  • 康託展開
  • 生成函式
  • 組合數&CRT
  • 乘法逆元
  • min25
  • Min-Max容斥
  • 不等式證明
  • 質因數分解
  • 斐波那契數列
  • 多項式
  • 常係數齊次線性遞推
  • 高斯消元
  • 牛頓迭代
  • 插值

錯排問題

\(f(n)\)表示\(n\)個元素的錯排問題,有公式:

\[f(n)=(n-1)[f(n-1)+f(n-2)],n\ge 3 \]

特別地,\(f(1)=0,f(2)=1,f(3)=2\)

P1595 信封問題

problem

\(n\)個信不裝回原來信封的情況數。

code

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1e6+10;
const int maxm=5e5+10;
const int mod=1e9+7;
#define int long long
#define inf 0x3f3f3f3f3f
int read(){
	int a=0,op=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') op=-1;c=getchar();}
	while(c>='0'&&c<='9') a*=10,a+=c^48,c=getchar();
	return a*op;	
}
int f[maxn];
int n;
signed main(){
	n=read();
	f[1]=0,f[2]=1;
	for(int i=3;i<=n;i++) f[i]=(i-1)*(f[i-1]+f[i-2]);
	printf("%lld",f[n]);
	return 0;
}

同餘方程