有趣的数列
1.为什么是卡特兰数
2.如何求组合数
应该是比较简单的。
way 1: 分解质因数
way 2: 求质数在 n! 中出现了多少次 (faster)
code 1:
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const int Maxn = 1e6 * 2;
LL n, p;
int bak[Maxn + 5];
int cnt, primes[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
rep (i, 2, Maxn) {
if (vis[i] == 0) {
vis[i] = 1;
primes[++cnt] = i;
}
rep (j, 1, cnt) {
if (primes[j] > Maxn / i) break;
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) break;
}
}
}
int main () {
// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.in", "r", stdin);
// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.out", "w", stdout);
Euler ();
read (n); read (p);
rep (i, 1, cnt) {
int x = primes[i];
for (int j = x; j <= n * 2; j += x) {
int tmp = j;
if (1 <= j && j <= n)
while (tmp % x == 0) {
bak[x]--;
tmp /= x;
}
else if (n + 2 <= j && j <= n * 2)
while (tmp % x == 0) {
bak[x]++;
tmp /= x;
}
}
}
LL res = 1;
rep (i, 1, cnt)
res = (res * (LL)pow (primes[i], bak[primes[i]])) % p;
write (res);
return 0;
}
code 2:
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const int Maxn = 1e6 * 2;
LL n, p;
int bak[Maxn + 5];
int cnt, primes[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
rep (i, 2, n * 2) {
if (vis[i] == 0) {
vis[i] = 1;
primes[++cnt] = i;
}
rep (j, 1, cnt) {
if (primes[j] > n * 2 / i) break;
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) break;
}
}
}
LL quick_pow (LL x, LL y) {
LL res = 1;
while (y) {
if (y & 1) res = (res * x) % p;
x = (x * x) % p; y >>= 1;
}
return res;
}
void Calc (LL x, int op) {
rep (i, 1, cnt) {
if (primes[i] > x) break;
for (LL j = primes[i]; j <= x; j *= primes[i])
bak[primes[i]] += op * (x / j);
}
}
int main () {
// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.in", "r", stdin);
// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.out", "w", stdout);
read (n); read (p);
Euler ();
Calc (n * 2, 1);
Calc (n + 1, -1);
Calc (n, -1);
LL res = 1;
rep (i, 1, cnt) {
if (primes[i] > n * 2) break;
res = (res * quick_pow (primes[i], bak[primes[i]])) % p;
}
write (res);
return 0;
}