# T1

## Code

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])
#define inline __inline__ __attribute__((always_inline))
LL x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}

const int Max_n = 2005, mod = 1e9 + 7, inv = (mod + 1) / 2;
int n, ans, tot, t;
int f[Max_n], q[Max_n], g[Max_n];
char s[Max_n][Max_n];

namespace Input {
void main() {
for (int i = n; i >= 1; i--) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= n; j++) tot += s[i][j] == '.';
reverse(s[i] + 1, s[i] + n + 1);
}
}
}  // namespace Input

namespace Solve {
void DP() {
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= n; j++) {
(q[j] = s[j + 1][i - 1] != 'W' ? f[j] : 0) %= mod;
}
for (int j = 1; j <= n; j++) (q[j] += q[j - 1]) %= mod;
for (int j = 0; j <= n; j++) {
g[j] = j > 0 && s[j][i] == '.' ? (LL)q[j - 1] * inv % mod * inv % mod : 0;
(g[j] += f[j] % mod) %= mod;
}
swap(g, f);
}
for (int i = 0; i < n; i++) f[i] = s[i + 1][n] != 'W' ? f[i] : 0;
for (int i = 1; i < n; i++) (f[i] += f[i - 1]) %= mod;
}
void main() {
t = 1;
while (tot--) t = 2 * t % mod;
for (int i = 1; i <= n; i++) f[i] = s[i][1] == '.' ? (LL)inv * inv % mod : 0;
DP();
(ans += (LL)f[n] * t % mod * 2 % mod) %= mod;
(ans += (LL)f[n - 1] * t % mod) %= mod;
f[0] = 1;
for (int i = 1; i <= n; i++) f[i] = 0;
DP();
(ans += (LL)f[n] * t % mod) %= mod;
(ans += (LL)f[n - 1] * t % mod * inv % mod) %= mod;
cout << ans << endl;
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input::main();
Solve::main();
}


# T2

## Sol

$f(t) = t!-\sum_{j > 0} g(j)*f(t-j)*C^{j}_{t}$

$g(t)=\sum_{j > 0, x|j} C_{t-1}^{j-1}*(j-1)! * g(t - j)$

## Code

#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
typedef unsigned long long uLL;
#define go(G, x, i, v) \
for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])
#define inline __inline__ __attribute__((always_inline))
LL x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}

const int Max_n = 7.5e3 + 5;
int n, mod, ans;
int C[Max_n][Max_n], fac[Max_n];

namespace FastMod {
uLL b, m;
void init(int x) { b = x, m = uLL((__int128(1) << 64) / b); }
uLL Mod(uLL a) {
uLL q = (uLL)((__int128(m) * a) >> 64);
uLL r = a - q * b;  // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
}  // namespace FastMod
using FastMod::Mod;

void M(int &x) { x = x >= mod - 1 ? x - mod + 1 : x; }

namespace Input {
}  // namespace Input

namespace Init {
void main() {
FastMod::init(mod - 1);
for (int i = 0; i <= n; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) M(C[i][j] = C[i - 1][j - 1] + C[i - 1][j]);
}
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = Mod((uLL)fac[i - 1] * i);
}
}  // namespace Init

namespace Solve {
int f[Max_n], g[Max_n];
bool vis[Max_n];
int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = (LL)a * a % mod)
if (b & 1) res = (LL)res * a % mod;
return res;
}
int DP(int x) {
g[0] = f[0] = 1;
for (int i = x; i <= n; i += x) {
g[i] = 0;
for (int j = x; j <= i; j += x)
M(g[i] += Mod(Mod((uLL)C[i - 1][j - 1] * fac[j - 1]) * g[i - j]));
}
for (int i = n % x; i <= n; i += x) {
f[i] = fac[i];
for (int j = x; j <= i; j += x)
M(f[i] += mod - 1 - Mod(Mod((uLL)C[i][j] * g[j]) * f[i - j]));
}
return Mod(fac[n] - f[n] + mod - 1);
}
void main() {
ans = 1;
for (int i = 2; i <= n; i++)
if (!vis[i]) {
for (int j = i; j <= n; j += i) vis[j] = 1;
for (int j = i; j <= n; j *= i) ans = (LL)ans * ksm(i, DP(j)) % mod;
}
cout << ans << endl;
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
freopen("B.in", "r", stdin);
freopen("B.out", "w", stdout);
#endif
Input::main();
Init::main();
Solve::main();
}


# T3

## Sol

$f(T)=K-\sum_{i \in S} A_i - 1 + C_i + K - n$

$f(T)=K - (n-x) - (K-n+1) * y$

## Code

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])
#define inline __inline__ __attribute__((always_inline))
LL x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}

const int Max_n = 1e5 + 5, mod = 1e9 + 7;
struct graph {
int hd[Max_n];
int cntr, nx[Max_n << 1], to[Max_n << 1];
void addr(int u, int v) {
cntr++;
nx[cntr] = hd[u], to[cntr] = v;
hd[u] = cntr;
}
};
struct path {
int u, v, len;
};

int n;
int fac[Max_n], ifac[Max_n];
int deg[Max_n], bf[Max_n], nx[Max_n];
graph G;
int cnt, f[Max_n], x[Max_n], y[Max_n];
path K[Max_n];
void remove(int x) {
nx[bf[x]] = nx[x], bf[nx[x]] = bf[x];
}

namespace Input {
void main() {
for (int i = 1; i < n; i++) {
deg[u]++, deg[v]++;
}
}
}  // namespace Input

namespace Init {
int ksm(int a, int b = mod - 2) {
int res = 1;
for (; b; b >>= 1, a = (LL)a * a % mod)
if (b & 1) res = (LL)res * a % mod;
return res;
}
void build(int x, int fa, int Fa, int dep) {
if (deg[x] != 2) {
if (x != nx[0]) K[++cnt] = (path){Fa, x, dep + 1};
Fa = x, dep = 0;
}
go(G, x, i, v) if (v != fa) build(v, x, Fa, dep + 1);
}
void main() {
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = (LL)fac[i - 1] * i % mod;
ifac[n] = ksm(fac[n]);
for (int i = n; i ; i--) ifac[i - 1] = (LL)ifac[i] * i % mod;
int L = 0;
for (int i = 1; i <= n; i++)
if (deg[i] != 2) {
bf[i] = L, nx[L] = i, L = i;
f[i] = i, x[i] = 1, y[i] = deg[i];
}
build(nx[0], 0, 0, 0);
}
}  // namespace Init

namespace Solve {
bool cmp(path a, path b) {
return a.len < b.len;
}
int ans[Max_n];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int u, int v, int len) {
u = find(u), v = find(v);
if (u == v) return;
f[v] = f[u], x[u] += x[v] + len - 2, y[u] += y[v] - 2;
remove(v);
}
void main() {
sort(K + 1, K + cnt + 1, cmp);
int pos = 1;
ans[n] = fac[n];
for (int k = n - 1; k; k--) {
ans[k] = fac[k];
while (pos <= n && n - K[pos].len > k) merge(K[pos].u, K[pos].v, K[pos].len), pos++;
for (int i = nx[0]; i; i = nx[i]) {
ans[k] = (LL)ans[k] * ifac[k - (n - x[find(i)]) - (k - n + 1) * y[find(i)]] % mod;
}
}
for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
freopen("C.in", "r", stdin);
freopen("C.out", "w", stdout);
#endif
Input::main();
Init::main();
Solve::main();
}

posted @ 2020-04-29 12:50  洛水·锦依卫  阅读(284)  评论(0编辑  收藏  举报