Files
python/TangDou/AcWing/Math/ZhuHeJiShu/1317.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

79 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1010; // n+n=2n=> 500*2+10
int primes[N], cnt;
bool st[N];
int a[N], b[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
st[i * primes[j]] = true;
if (i % primes[j] == 0) break;
}
}
}
int get(int n, int p) { // n!中p的次数
int s = 0;
while (n) n /= p, s += n;
return s;
}
void mul(int a[], int b, int &len) {
int t = 0;
for (int i = 1; i <= len; i++) {
t += a[i] * b;
a[i] = t % 10;
t /= 10;
}
while (t) {
a[++len] = t % 10;
t /= 10;
}
//去前导0
while (len > 1 && !a[len]) len--;
}
int C(int a, int b, int c[]) {
int len = 1;
c[1] = 1;
for (int i = 0; i < cnt; i++) {
int p = primes[i];
int s = get(a, p) - get(b, p) - get(a - b, p);
while (s--) mul(c, p, len);
}
return len;
}
void sub(int a[], int b[], int &len) {
int t = 0;
for (int i = 1; i <= len; i++) {
t = a[i] - b[i] - t;
a[i] = (t + 10) % 10;
t < 0 ? t = 1 : t = 0;
}
while (len > 1 && !a[len]) len--;
}
int main() {
//加快读入
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
get_primes(2 * n);
int a1 = C(n + n, n, a);
int b1 = C(n + n, n - 1, b); // bl下面没有用到,原因是两数相减,我们知道a>b,按着a的长度来就行了
sub(a, b, a1);
for (int i = a1; i >= 1; i--) printf("%d", a[i]);
return 0;
}