首页 > 其他分享 >CodeForces 1874E Jellyfish and Hack

CodeForces 1874E Jellyfish and Hack

时间:2024-03-12 23:04:01浏览次数:27  
标签:1874E frac limits sum CodeForces typedef maxn Hack ll

洛谷传送门

CF 传送门

显然 \(\text{fun}(P)_{\max} = \frac{|P|(|P| + 1)}{2}\)。

考虑大力 dp,设 \(f_{i, j, k}\) 为 \(|P| = i\),\(P_1 = j\),\(\text{fun}(P) = k\) 的排列 \(P\) 的个数。此时 \(|L| = j - 1, |R| = i - j\)。转移枚举 \(L_1, R_1, \text{fun}(L), \text{fun}(R)\),有:

\[f_{i, j, x + y + i} = \sum\limits_{k = 0}^i \sum\limits_{l = 0}^j \sum\limits_{x = 0}^{\frac{i(i - 1)}{2}} \sum\limits_{y = 0}^{\frac{(j - i)(j - i + 1)}{2}} f_{j - 1, k, x} \times f_{i - j, l, y} \times \binom{i - 1}{j - 1} \]

答案即 \(\sum\limits_{i = 1}^n \sum\limits_{j = m}^{\frac{n(n + 1)}{2}} f_{n, i, j}\)。

\(\binom{i - 1}{j - 1}\) 的系数是已知 \(L, R\) 内元素的相对大小关系,它们组成 \(P_{2 \sim n}\) 的方案数。

发现转移最后一维出现了类似卷积的形式,考虑用多项式刻画转移。即设 \(F_{i, j}(x) = \sum\limits_{k} f_{i, j, k} x^k\)。那么:

\[F_{i, j}(x) = x^i \sum\limits_{k = 0}^i \sum\limits_{l = 0}^j \binom{i - 1}{j - 1} F_{j - 1, k}(x) F_{i - j, l}(x) \]

令 \(G_i(x) = \sum\limits_{j = 1}^i F_{i, j}(x)\),那么:

\[G_i(x) = x^i \sum\limits_{j = 1}^{i - 1} \binom{i - 1}{j - 1} G_{j - 1}(x) G_{i - j}(x) \]

因为 \(\deg G_n(x) \le \frac{n(n + 1)}{2}\),所以求出 \(G_n(x)\) 的 \(\frac{n(n + 1)}{2} + 1\) 个点的点值,就能拉插推得 \(G_n(x)\) 每一项的系数。

考虑如何点值转系数:

\[f(x) = \sum\limits_{i = 1}^n y_i \prod\limits_{j \ne i} \frac{x - x_j}{x_i - x_j} \]

枚举 \(i\),那么 \(\prod\limits_{j \ne i} \frac{1}{x_i - x_j}\) 是可以预处理阶乘及其逆元后 \(O(1)\) 算的(因为 \(x_i = i\))。\(y_i\) 可以朴素 \(O(n^2)\) 计算。

考虑如何算 \(\prod\limits_{j \ne i} (x - x_j)\)。先算出 \(H(x) = \prod\limits_{i = 1}^n (x - x_i)\),然后那个东西相当于 \(\frac{H(x)}{x - x_i}\)。做大除法即可。

时间复杂度 \(O(n^4)\)。虽然枚举带了个 \(\frac{1}{4}\) 常数但是需要不停取模所以跑得很慢。

code
// Problem: E. Jellyfish and Hack
// Contest: Codeforces - Codeforces Round 901 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1874/E
// Memory Limit: 512 MB
// Time Limit: 8000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 210;
const int N = 40000;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n, m, g[maxn], fac[maxn * maxn], ifac[maxn * maxn], pw[maxn], inv[maxn * maxn];
ll F[maxn * maxn], G[maxn * maxn], H[maxn * maxn], res[maxn * maxn];

inline void fix(ll &x) {
	x = (x < 0 ? x + mod : x);
}

inline ll work(ll x) {
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = pw[i - 1] * x % mod;
	}
	g[0] = 1;
	for (int i = 1; i <= n; ++i) {
		ll s = 0;
		int k = 0;
		for (int j = 0; j <= (i - 1) / 2; ++j) {
			s += g[j] * g[i - 1 - j] * (1 + (j + j < i - 1));
			if ((++k) == 4) {
				s %= mod;
				k = 0;
			}
		}
		g[i] = s % mod * pw[i] % mod * inv[i] % mod;
	}
	return g[n] * fac[n] % mod;
}

void solve() {
	scanf("%lld%lld", &n, &m);
	fac[0] = 1;
	for (int i = 1; i <= N; ++i) {
		fac[i] = fac[i - 1] * i % mod;
	}
	ifac[N] = qpow(fac[N], mod - 2);
	for (int i = N - 1; ~i; --i) {
		ifac[i] = ifac[i + 1] * (i + 1) % mod;
	}
	inv[1] = 1;
	for (int i = 2; i <= N; ++i) {
		inv[i] = (mod - mod / i) * inv[mod % i] % mod;
	}
	F[0] = 1;
	for (int i = 1; i <= n * (n + 1) / 2 + 1; ++i) {
		for (int j = i; ~j; --j) {
			F[j] = (j ? F[j - 1] : 0) - F[j] * i;
			if (i & 1) {
				F[j] %= mod;
				fix(F[j]);
			}
		}
	}
	for (int i = 1; i <= n * (n + 1) / 2 + 1; ++i) {
		G[n * (n + 1) / 2 + 1] = F[n * (n + 1) / 2 + 1];
		for (int j = n * (n + 1) / 2; ~j; --j) {
			H[j] = G[j + 1];
			G[j] = (F[j] + H[j] * i) % mod;
		}
		ll p = work(i);
		p = p * ifac[i - 1] % mod;
		p = p * ifac[n * (n + 1) / 2 + 1 - i] % mod;
		if ((n * (n + 1) / 2 + 1 - i) & 1) {
			p = (mod - p) % mod;
		}
		bool fl = (i % 9 == 0);
		for (int j = 0; j <= n * (n + 1) / 2; ++j) {
			res[j] += H[j] * p;
			if (fl) {
				res[j] %= mod;
			}
		}
	}
	ll ans = 0;
	for (int i = m; i <= n * (n + 1) / 2; ++i) {
		ans = (ans + res[i]) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

由于本人被 CF 32 位机整破防了,所以代码有点卡常。

标签:1874E,frac,limits,sum,CodeForces,typedef,maxn,Hack,ll
From: https://www.cnblogs.com/zltzlt-blog/p/18069558

相关文章

  • Codeforces Round 933 (Div. 3)
    CodeforcesRound933(Div.3)AA-RudolfandtheTicket暴力查看代码voidsolve(){intn,m,k;cin>>n>>m>>k;vector<int>b(n),c(m);for(inti=0;i<n;++i)cin>>b[i];for(inti=0;i<m;++i)cin>>c[i];......
  • Codeforces Round 933 (Div. 3)
    CodeforcesRound933(Div.3)A-RudolfandtheTicket解题思路:暴力。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingpiii=pair<ll,pair<ll,ll>&......
  • [HackMyVm] Quick
    kali:192.168.56.104主机发现arp-scan-l#arp-scan-lInterface:eth0,type:EN10MB,MAC:00:0c:29:d2:e0:49,IPv4:192.168.56.104Startingarp-scan1.10.0with256hosts(https://github.com/royhills/arp-scan)192.168.56.10a:00:27:00:00:05(Unkno......
  • Codeforces Round 933 (Div. 3)
    A.RudolfandtheTicket#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;usingldb=longdouble;#defineinti64usingvi=vector<int>;usingpii=pair<int,int>;constintinf=1e9;co......
  • tryhackme-Retro(复古的)
    题目没有给太多的描述,但是根据硬币,复古的得知这是一个像素,复古,FC游戏的爱好者,之前游戏厅里的游戏信息收集首先对靶机进行端口扫描通过扫描得知一共开放80和3389这两个端口访问80端口发现是IIS的默认页面使用gobuster进行目录扫描扫描到/retro目录,访问该页面这里我访问......
  • Codeforces Round 933 (Div. 3) A-F
    CodeforcesRound933(Div.3)A.RudolfandtheTicket题意:有两个数组\(b_n\)和\(c_m\),两个数组中分别取一个数相加是否小于k,有几种方法使\(b_f\)+\(c_s\)<=k思路:暴力枚举所有方案,时间复杂度O(nm)代码:#include<bits/stdc++.h>usingnamespacestd;constintMAXN=1e......
  • TheWay2Hack
    TheWay2Hackercoding阶段一打基础。主要涉及两个方面,一个是代码质量和设计,代表课程是cs61a。预计时间为一个月(因为已经过去一个月了)。另一个是步入下一阶段的先导课,是为了进入更底层视角的铺垫,csapp和NandToTetris。每周一个lab,一共7个lab,预计时间为两个半月;另一个是NandToTe......
  • hackthebox sandworm medium writeup
    Thisisthewriteupforthemediummachine'onlyrforyou'.Topiccoveredinthisarticleare: LFI,commnadinjection,neo4jcipherinjection,maliciouspythonpackagesandcodeexecutionviapipdownload.ShellasuserSubdomainenumeration:ffuf......
  • tryhackme-Anthem(国歌)
    根据题目描述,这是一个让我们练习的简单Windows机器信息收集首先对靶机进行端口扫描加入-Pn参数是因为Windows默认开启防火墙拒绝icmpping数据包根据开放端口80和3389猜测到后续可能会远程连接靶机接着访问80端口进行信息收集根据title和网页标题,可以看出网站的域名为Anth......