//分解质因数是除以所有可以整除的因子,并累计个数。最后的数如果不是1,就说明是质数 voiddivide(int x){ for (int i = 2; i <= x / i; i ++ ) if (x % i == 0) { int s = 0; while (x % i == 0) x /= i, s ++ ; cout << i << ' ' << s << endl; } if (x > 1) cout << x << ' ' << 1 << endl; cout << endl; }
//一次性清除所有的约数 vector<int> get_divisors(int x) { vector<int> res; for (int i = 1; i <= x / i; i ++ ) if (x % i == 0) { res.push_back(i); if (i != x / i) res.push_back(x / i); } sort(res.begin(), res.end()); return res; }
//求单个较大的数的欧拉函数 intphi(int x) { int res = x; for (int i = 2; i <= x / i; i ++ ) if (x % i == 0) { res = res / i * (i - 1); while (x % i == 0) x /= i; } if (x > 1) res = res / x * (x - 1);
//给定正数a,b,m,求一个整数a*x与b模m同余,或给出无解,我们称之为线性同余方程 while(T--){ int a,b,m,x,y; cin >> a >> b >> m; int d = exgcd(a,m,x,y); if(b%d) puts("impossible"); else cout << (longlong)x*(b/d)%m << endl; //模m是为了求得最小整数解 }
// a[N][N]是增广矩阵 intgauss() { int c, r; for (c = 0, r = 0; c < n; c ++ ) { int t = r; for (int i = r; i < n; i ++ ) // 找到绝对值最大的行 if (fabs(a[i][c]) > fabs(a[t][c])) t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i <= n; i ++ ) swap(a[t][i], a[r][i]); // 将绝对值最大的行换到最顶端 for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // 将当前行的首位变成1 for (int i = r + 1; i < n; i ++ ) // 用当前行将下面所有的列消成0 if (fabs(a[i][c]) > eps) for (int j = n; j >= c; j -- ) a[i][j] -= a[r][j] * a[i][c];
r ++ ; }
if (r < n) { for (int i = r; i < n; i ++ ) if (fabs(a[i][n]) > eps) return2; // 无解 return1; // 有无穷多组解 }
for (int i = n - 1; i >= 0; i -- ) for (int j = i + 1; j < n; j ++ ) a[i][n] -= a[i][j] * a[j][n];
return0; // 有唯一解 }
快速幂:
1 2 3 4 5 6 7 8 9 10 11 12
//指数不断除2,底数不断平方 longlongquick_pow(longlong base, longlong power){ longlong res = 1; //此处等价于if(power%2==1) while (power) { if (power & 1) { res = res * base % MOD; } power >>= 1; base = (base * base) % MOD; } return res; }
//只求一个组合数可以直接求,时间复杂度为logN*N intmain(){ int n; cin >> n; int a = 2*n,b = n; ll res = 1; for(int i = 1,j = a; i <= b; i++,j--){ res = res * j % MOD; res = res * quick_pow(i,MOD-2)%MOD; } cout << res*quick_pow(n+1,MOD-2)%MOD << endl; }