首页 > 其他分享 >AtCoder Grand Contest 051 D C4

AtCoder Grand Contest 051 D C4

时间:2024-01-15 10:57:56浏览次数:29  
标签:AtCoder typedef ifac Contest res ll C4 欧拉 mod

洛谷传送门

AtCoder 传送门

下文的点 \(1, 2, 3, 4\) 对应原题面中的 \(S, T, U, V\)。

直接对无向图欧拉回路计数不太好做。考虑给边定向。枚举有 \(i\) 条边是从 \(1\) 到 \(2\) 的。那么 \(2 \to 1\) 有 \(a - i\) 条边。由于这个图必须满足每个点的入度等于出度,设 \(j\) 条 \(2 \to 3\) 的边,\(b - j\) 条 \(3 \to 2\) 的边,那么我们有 \(a - i + j = i + b - j\)。解得 \(j = i + \frac{b - a}{2}\)。同理有 \(k = j + \frac{c - b}{2}\) 条 \(3 \to 4\) 的边,\(l = k + \frac{d - c}{2}\) 条 \(4 \to 1\) 的边。

这样我们就将题目的无向图转化成了有向图。在这个图上做欧拉回路计数。可以考虑 BEST 定理,有向欧拉图的本质不同欧拉回路数量(循环同构视为本质相同,每条边互相区分)为:

\[T \prod\limits_{i = 1}^n (out_i - 1)! \]

其中 \(T\) 为图的外向生成树个数(注意到有向欧拉图以每个点为根的外向生成树个数相等),\(out_i\) 为 \(i\) 点的出度。

这题要求欧拉回路从 \(1\) 出发和结束。每一条欧拉回路 \(1\) 都出现了 \(out_1\) 次,把循环同构的加上,所以答案乘上 \(out_1\)。注意这题每条同方向的边互不区分,所以答案乘上 \(\frac{1}{i! (a - i)! j! (b - j)! k! (c - k)! l! (d - l)!}\) 即可。

时间复杂度 \(O(a + b + c + d)\)。

注意判一些无解的情况,比如 \(i, j, k, l\) 不在范围内。

code
// Problem: D - C4
// Contest: AtCoder - AtCoder Grand Contest 051 (Good Bye rng_58 Day 2)
// URL: https://atcoder.jp/contests/agc051/tasks/agc051_d
// Memory Limit: 1024 MB
// Time Limit: 2000 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 = 2000100;
const int N = 2000000;
const ll mod = 998244353;

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 A, B, C, D, fac[maxn], ifac[maxn], a[9][9];

inline ll det() {
	ll ans = 1;
	for (int i = 1; i <= 3; ++i) {
		for (int j = i; j <= 3; ++j) {
			if (a[j][i]) {
				if (i != j) {
					swap(a[i], a[j]);
					ans = (mod - ans) % mod;
				}
				break;
			}
		}
		if (!a[i][i]) {
			return 0;
		}
		ans = ans * a[i][i] % mod;
		ll t = qpow(a[i][i], mod - 2);
		for (int j = i; j <= 3; ++j) {
			a[i][j] = a[i][j] * t % mod;
		}
		for (int j = 1; j <= 3; ++j) {
			if (i == j) {
				continue;
			}
			ll t = (mod - a[j][i]) % mod;
			for (int k = i + 1; k <= 3; ++k) {
				a[j][k] = (a[j][k] + t * a[i][k]) % mod;
			}
		}
	}
	return ans;
}

void solve() {
	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;
	}
	scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
	if (((B - A) & 1) || ((C - B) & 1) || ((D - C) & 1) || ((A - D) & 1)) {
		puts("0");
		return;
	}
	ll ans = 0;
	for (int i = 0; i <= A; ++i) {
		int j = i + (B - A) / 2;
		int k = j + (C - B) / 2;
		int l = k + (D - C) / 2;
		if (A - i + l != i + D - l || j < 0 || j > B || k < 0 || k > C || l < 0 || l > D || i + D - l == 0 || j + A - i == 0 || k + B - j == 0 || l + C - k == 0) {
			continue;
		}
		mems(a, 0);
		a[1][1] = l + A - i;
		a[2][2] = i + B - j;
		a[3][3] = j + C - k;
		a[4][4] = k + D - l;
		a[1][2] = (mod - i) % mod;
		a[2][1] = (mod - A + i) % mod;
		a[2][3] = (mod - j) % mod;
		a[3][2] = (mod - B + j) % mod;
		a[3][4] = (mod - k) % mod;
		a[4][3] = (mod - C + k) % mod;
		a[4][1] = (mod - l) % mod;
		a[1][4] = (mod - D + l) % mod;
		ll res = det() * fac[i + D - l] % mod * fac[j + A - i - 1] % mod * fac[k + B - j - 1] % mod * fac[l + C - k - 1] % mod;
		res = res * ifac[i] % mod * ifac[A - i] % mod * ifac[j] % mod * ifac[B - j] % mod * ifac[k] % mod * ifac[C - k] % mod * ifac[l] % mod * ifac[D - l] % mod;
		ans = (ans + res) % mod;
	}
	printf("%lld\n", ans);
}

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

标签:AtCoder,typedef,ifac,Contest,res,ll,C4,欧拉,mod
From: https://www.cnblogs.com/zltzlt-blog/p/17964930

相关文章

  • AtCoder Beginner Contest 336
    AtCoderBeginnerContest336A-LongLoong#include<bits/stdc++.h>#defineendl'\n'//#defineintlonglongusingnamespacestd;voidsolve(){ intx; cin>>x; cout<<"L"; while(x--)cout<<"o&q......
  • 2021-2022 ACM-ICPC Latin American Regional Programming Contest
    Preface唉最近天天前期犯病,读错题占用大量机时还红温,纯在靠队友兜底H板题但刚开始因为没打印自己的KM板子就写个了MCMF上去,然后直接TLE飞,后面找了个别人的板子抄上去才过,I题一个傻逼题题意读错爆WA两发最后1h把L题扔给队友然后跑去看ECF滚榜直播了,只能说从此清北电的格局打开了......
  • AtCoder World Tour 2022 B The Greatest Two
    原题面:https://atcoder.jp/contests/wtf22-day2/tasks/wtf22_day2_b题面翻译:一个长度为\(n\)的排列\(p\),每次可以把一个长\(k\)区间的最大与次大值交换,问操作任意次数后可以得到的排列数量对\(998244353\)取模。这题被我搬到了一场多校联考中。在搬到的题面中,我加入了......
  • 2021-2022 ICPC Northwestern European Regional Programming Contest (NWERC 2021)
    Preface和昨天刚好相反,前期极度崩盘2h2题而且一堆银铜牌题不会但好在后面稳扎稳打慢慢追回来了一点,最后超高罚时8题收场这场一边打一边看ECF的实况,最后看到同校的Wifi暴打全场,实在是ORZA.AccessDenied签到,首先暴力问出长度然后从前往后一位一位确定即可注意实现的时候不......
  • asp.net mvc4 controller构造函数
    asp.netmvc4controller构造函数ASP.NETMVC4中的Controller类有多种构造函数可供使用。以下是常见的两种构造函数示例:默认构造函数(无参):publicclassMyController:Controller{publicMyController(){}//这里为空的构造函数表示没有任何初始化操作}......
  • AtCoder Beginner Contest 335 G Discrete Logarithm Problems
    洛谷传送门AtCoder传送门考虑若我们对于每个\(a_i\)求出来了使得\(g^{b_i}\equiva_i\pmodP\)的\(b_i\)(其中\(g\)为\(P\)的原根),那么\(a_i^k\equiva_j\pmodP\)等价于\(kb_i\equivb_j\pmod{P-1}\),有解的充要条件是\(\gcd(b_i,P-1)\midb_j\)。显然......
  • mysql发生连接异常Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    【问题背景】应用部署再ecs或者云上报错 Cause:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communicationslinkfailure用的是 数据库连接池(Druid) 背景信息 使用Druid作为数据库连接池时,在数据库宕机后再次恢复,应用无法获取数据库连接或获取的连接为失......
  • AtCoder Beginner Contest 335 (Sponsored by Mynavi)
    AtCoderBeginnerContest335(SponsoredbyMynavi)A-2023代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondvoidsolve(){strings;cin>>s;......
  • AtCoder Beginner Contest 335 总结
    ABC335总结A.202<s>3</s>翻译给你一个由小写英文字母和数字组成的字符串\(S\)。\(S\)保证以2023结尾。将\(S\)的最后一个字符改为4,并打印修改后的字符串。分析两种做法:直接把最后一个字符改为4,然后输出。输出前\(n\)个字符后输出4。code#include<bits/stdc......
  • MCP3461RT-E/NC 16位ADC用于便携式仪器仪表,XMC4800-F100F1024AA适合工业连接、控制(MCU
    1、MCP3461RT-E/NCICADC16BITSIGMA-DELTA20UQFNMCP3461器件是16位三角积分模数转换器(ADC),具有高达153.6kSPS的可编程数据速率。它们提供集成功能,如内部基准电压源、内部振荡器、温度传感器和烧毁传感器检测,以减少系统元件数量和总解决方案成本。MCP3461ADC采用超小型3mmx3......