intq_pow(int a, int k, int p) { int res = 1; while(k) { if(k & 1) res = (LL)res * a % p; a = (LL)a * a % p; k >>= 1; } return res; }
intC(int a, int b, int p) { if(b > a) return0; int res = 1; for (int i = 1, j = a; i <= b; i ++ ,j -- ) { res = (LL)res * j % p; res = (LL)res * q_pow(i, p - 2, p) % p; } return res; }
intlucas(LL a, LL b, int p) { if (a < p && b < p) returnC(a, b, p); elsereturn (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p; }
intmain() { int n; cin >> n; while(n -- ) { LL a, b; int p; cin >> a >> b >> p; cout << lucas(a, b, p) << endl; } return0; }
voidget_primes(int n) { for (int i = 2; i <= n; i ++ ) { if(!st[i]) primes[cnt ++ ] = i; for (int j = 0; primes[j] <= n / i; j ++ ) { st[primes[j] * i] = true; if(i % primes[j] == 0) break; } } }
intget(int n, int p) { int res = 0; while (n){ res += n / p; n /= p; } return res; }
vector<int> mul(vector<int> a, int b) { vector<int> c; int t = 0; for (int i = 0; i < a.size(); i ++ ) { t += a[i] * b; c.push_back(t % 10); t /= 10; } while(t) { c.push_back(t % 10); t /= 10; } return c; }
intmain() { int a, b; cin >> a >> b; get_primes(a); for (int i = 0; i < cnt; i ++ ) { int p = primes[i]; sum[i] = get(a, p) - get(a - b, p) - get(b, p); } vector<int> res; res.push_back(1); for (int i = 0; i < cnt; i ++ ) for (int j = 0; j < sum[i]; j ++ ) res = mul(res, primes[i]); for (int i = res.size() - 1; i >= 0; i -- ) printf("%d", res[i]); return0; }
python版本
利用定义,但是跑起来比c++略慢
1 2 3 4 5 6 7 8
a, b = map(int, input().split()) up = 1 down = 1 for i inrange(a - b + 1, a + 1): up *= i for i inrange(1, b + 1): down *= i print(up // down)
LL q_pow(LL a, LL k) { LL ret = 1; while(k) { if(k&1) ret = ret * a % mod; a = a * a % mod; k >>= 1; } return ret; }
intmain() { int n; cin >> n; LL ans = 1; // z for(int i = 2 * n; i > n; i -- ) ans = ans * i % mod; for(int i = n; i > 0; i -- ) ans = ans * q_pow(i, mod - 2) % mod; ans = ans * q_pow(n + 1, mod - 2) % mod; cout << ans << endl; return0; }
intgauss() { int c, r; // c代表列col, r代表行row 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; // 如果绝对值最大的是0,那么这一行全是0
for (int i = c; i < n + 1; i ++ ) swap(a[t][i], a[r][i]); // 第二步,把当前这一行换到最上面 for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // 第三步,把当前这一行的第一个数变成1,方程两边同时除以第一个系数 // 必须要从后往前,不然第一个数会直接变为1,然后不修改其他系数 for (int i = r + 1; i < n; i ++ ) if (fabs(a[i][c]) > eps) // 非零 for (int j = n; j >= c; j -- ) a[i][j] -= a[r][j] * a[i][c]; // 第四步,把第r+1行~n行的第c列元素都消除为0 // 用最上面一行和这行做差除去这一行 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[j][n] * a[i][j]; return0; }
intmain() { cin >> n; for (int i = 0; i < n; i ++ ) for (int j = 0; j < n + 1; j ++ ) cin >> a[i][j]; int t = gauss(); if (t == 0) { for (int i = 0; i < n; i ++ ) if(fabs(a[i][n]) < eps) puts("0.00"); elseprintf("%.2lf\n", a[i][n]); } elseif (t == 1) puts("Infinite group solutions"); elseputs("No solution"); return0; }
intgauss() { int c, r; for (c = 0, r = 0; c < n; c ++ ) { int t = r; for (int i = r; i < n; i ++ ) { if(a[i][c]) t = i; } if (!a[t][c]) continue;
for (int i = c; i <= n; i ++ ) swap(a[r][i], a[t][i]); for (int i = r + 1; i < n; i ++ ) if(a[i][c]) for (int j = n; j >= c; j -- ) a[i][j] ^= a[r][j]; r ++ ; } if(r < n) { for (int i = r; i < n; i ++ ) if(a[i][n]) 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; }
intmain() { cin >> n; for (int i = 0; i < n; i ++ ) { for (int j = 0; j < n + 1; j ++ ) { cin >> a[i][j]; } }
int t = gauss();
if(t == 0) { for (int i = 0; i < n; i ++ ) cout << a[i][n] << endl; } elseif(t == 1) puts("Multiple sets of solutions"); elseputs("No solution"); return0; }