@TaoSama
2017-01-23T23:22:59.000000Z
字数 3640
阅读 1417
Learning-ACM
//__gcd(a, b)
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a, int b){
return a / gcd(a, b) * b;
}
//ax+by=gcd(a, b)
LL exgcd(LL a, LL b, LL& x, LL& y) {
LL d = a;
if(!b) x = 1, y = 0;
else {
d = exgcd(b, a % b, y, x);
y = (a / b) * x;
}
return d;
}
//求二元一次方程x最小非负解
LL solve(LL a, LL b, LL c) {
LL x, y, g = exgcd(a, b, x, y);
if(c % g) return -1;
x *= c / g;
x = (x % b + b) % (b / g);
return x;
}
//O(nloglogn)
bool notPrime[C];
vector<int> prime;
//int p[C / 10], ps;
for(int i = 2; i < C; ++i){
if(notPrime[i]) continue;
prime.push_back(i);
for(int j = i + i; j < C; j += i)
notPrime[j] = true;
}
// O(n)
bool notPrime[C];
vector<int> prime;
for(int i = 2; i < C; ++i){
if(!notPrime[i]) prime.push_back(i);
for(int j = 0; j < prime.size() && i * prime[j] < C; ++j){
notPrime[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
// O(sqrt(n))
void factorize(int n){
vector<int> factors;
for(int i = 2; i * i <= n; ++i) {
if(n % i == 0) {
factors.push_back(i);
while(n % i == 0) n /= i;
if(n == 1) break;
}
}
if(n != 1) factors.push_back(n);
return factors;
}
void factorize(int n){
vector<int> factors;
for(int i = 0; i < primes.size() && primes[i] * primes[i] <= n; ++i){
if(n % primes[i] == 0) {
factors.push_back(primes[i]);
while(n % primes[i] == 0) n /= primes[i];
if(n == 1) break;
}
}
if(n != 1) factors.push_back(n);
return factors;
}
void decomposite(int n){
vector<int> divisors;
for(int i = 2; i * i <= n; ++i){
divisors.push_back(i);
if(i * i != n) divisors.push_back(n / i);
}
return divisors;
}
vector<int> divisors[N];
void init(){
for(int i = 2; i < N; ++i){
for(int j = i + i; j < N; j += i)
divisors[j].push_back(i);
}
}
逆元详解,点击直达
a * x % m == 1 % m
//MOD必须是素数
[1, MOD) 模 MOD的逆元
inv[1] = 1;
for(int i = 2; i < MOD; ++i)
inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
int inv(int a, int m){
int x, y, g = exgcd(a, m, x, y);
if(g != 1) return -1;
return (a + m) % m;
}
//O(logn)
int quickPow(int x, int n){
if(n == 0) return 1;
if(n & 1) return x * quickPow(x, n - 1);
else {
int ret = quick(x, n / 2); // (x^(n/2))^2 = x^n
return ret * ret;
}
}
//O(logn)
int quickPow(int x, int n){
int ret = 1;
for(; n; n >>= 1){
if(n & 1) ret = ret * x;
x = x * x;
}
return ret;
}
int inv(int x, int m){
if(m is not a prime) return -1;
return quickPow(x, m - 2);
}
int fact[N], invFact[N];
void init(){
fact[0] = 1 = invFact[0] = 1;
for(int i = 1; i < N; ++i) {
fact[i] = fact[i - 1] * i % MOD;
invFact[i] = inv(fact[i], MOD);
}
}
//n <= MOD, m <= MOD, MOD不是很大
int C(int n, int m){
return fact[n] * invFact[m] % MOD * invFact[n - m] % MOD;
}
//m比较小
int C(int n, int m){
int ret = 1;
//C(n,m)=C(n,n-m)
m = min(m, n - m);
for(int i = 0; i < m; ++i){
ret *= (n - i);
ret /= (i + 1);
}
return ret;
}
int C(int n, int m){
int up = 1, dw = 1;
//C(n,m)=C(n,n-m)
m = min(m, n - m);
for(int i = 0; i < m; ++i){
up *= (n - i);
dw *= (i + 1);
}
dw = inv(dw, MOD);
return up * dw % MOD;
}
//C(n,m) = C(n-1,m)+C(n-1,m-1)
// 1
// 1 2 1
// 1 3 3 1
//n和m都不大
int C[N][N];
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
if(j == 0 || j == i) C[i][j] = 1;
else C[i][j] = C[i - 1][j] + C[i - 1][j - 1];