首页 > 其他分享 >AtCoder Beginner Contest 331 G Collect Them All

AtCoder Beginner Contest 331 G Collect Them All

时间:2023-12-04 17:25:59浏览次数:36  
标签:AtCoder Them Beginner limits Contest int ll typedef sum

洛谷传送门

AtCoder 传送门

设数字 \(i\) 第一次拿到的时间为 \(t_i\),所求即为 \(E(\max\limits_{i = 1}^m t_i)\)。

施 min-max 容斥,有:

\[\begin{aligned}E(\max\limits_{i = 1}^m t_i) & = \sum\limits_{T \subseteq [1, m] \land T \ne \varnothing} (-1)^{|T| - 1} E(\min\limits_{j \in T} t_j) \\ & = \sum\limits_{T \subseteq [1, m] \land T \ne \varnothing} (-1)^{|T| - 1} \frac{n}{\sum\limits_{j \in T} a_j} \end{aligned} \]

设 \(\sum\limits_{j \in T} a_j = k\) 的集合 \(T\) 的容斥系数 \((-1)^{|T| - 1}\) 之和为 \(f_k\),那么:

\[ans = \sum\limits_{i = 1}^n \frac{n}{i} \times s_i \]

套路地,考虑生成函数 \(F(x) = -\prod\limits_{i = 1}^n (-x^{a_i} + 1)\),那么 \(f_i = [x^i] F(x)\)。而 \(F(x)\) 可以分治 NTT 求出。具体就是递归求 \(\prod\limits_{i = l}^r (-x^{a_i} + 1)\),然后得到 \([l, mid]\) 和 \([mid + 1, r]\) 的答案再合并,总时间复杂度就是 \(O(n \log^2 n)\)。

好像有先 exp 再求逆的 \(O(n \log n)\) 做法,但是我既不会 exp 也不会求逆,开摆。

code
// Problem: G - Collect Them All
// Contest: AtCoder - Daiwa Securities Co. Ltd. Programming Contest 2023(AtCoder Beginner Contest 331)
// URL: https://atcoder.jp/contests/abc331/tasks/abc331_g
// 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 = 1000100;
const ll mod = 998244353, G = 3;

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, a[maxn], f[maxn], inv[maxn], r[maxn];

typedef vector<ll> poly;

inline poly NTT(poly a, int op) {
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		if (i < r[i]) {
			swap(a[i], a[r[i]]);
		}
	}
	for (int k = 1; k < n; k <<= 1) {
		ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
		for (int i = 0; i < n; i += (k << 1)) {
			ll w = 1;
			for (int j = 0; j < k; ++j, w = w * wn % mod) {
				ll x = a[i + j], y = w * a[i + j + k] % mod;
				a[i + j] = (x + y) % mod;
				a[i + j + k] = (x - y + mod) % mod;
			}
		}
	}
	if (op == -1) {
		ll inv = qpow(n, mod - 2);
		for (int i = 0; i < n; ++i) {
			a[i] = a[i] * inv % mod;
		}
	}
	return a;
}

inline poly operator * (poly a, poly b) {
	a = NTT(a, 1);
	b = NTT(b, 1);
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		a[i] = a[i] * b[i] % mod;
	}
	a = NTT(a, -1);
	return a;
}

inline poly mul(poly a, poly b) {
	int n = (int)a.size() - 1, m = (int)b.size() - 1, k = 0;
	while ((1 << k) <= n + m + 1) {
		++k;
	}
	for (int i = 1; i < (1 << k); ++i) {
		r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
	}
	poly A(1 << k), B(1 << k);
	for (int i = 0; i <= n; ++i) {
		A[i] = a[i];
	}
	for (int i = 0; i <= m; ++i) {
		B[i] = b[i];
	}
	poly res = A * B;
	res.resize(n + m + 1);
	return res;
}

inline poly dfs(int l, int r) {
	if (l == r) {
		poly res(a[l] + 1);
		res[0] = 1;
		res[a[l]] = mod - 1;
		return res;
	}
	int mid = (l + r) >> 1;
	return mul(dfs(l, mid), dfs(mid + 1, r));
}

void solve() {
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%lld", &a[i]);
	}
	inv[1] = 1;
	for (int i = 2; i <= n; ++i) {
		inv[i] = (mod - mod / i) * inv[mod % i] % mod;
	}
	poly res = dfs(1, m);
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans = (ans + n * inv[i] % mod * res[i]) % mod;
	}
	ans = (mod - ans) % mod;
	printf("%lld\n", ans);
}

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

标签:AtCoder,Them,Beginner,limits,Contest,int,ll,typedef,sum
From: https://www.cnblogs.com/zltzlt-blog/p/17875444.html

相关文章

  • AtCoder Beginner Contest 331
    B-BuyOneCartonofMilk难度:⭐题目大意选择有三种套餐,6个鸡蛋S元,8个鸡蛋M元,12个鸡蛋L元;问如果要买至少N个鸡蛋,最少花费多少钱;解题思路一道入门级dp;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio(false......
  • AtCoder Beginner Contest 295
    B-Bombs题意:就是说有一种炸弹,能炸曼哈顿距离的障碍物,要你打印出炸完后的图模拟#include<bits/stdc++.h>usingnamespacestd;charmp[50][50];voidsolve(){ intn,m; cin>>n>>m; for(inti=1;i<=n;i++){ for(intj=1;j<=m;j++){ cin>>mp[i][j]; } } for......
  • AtCoder Beginner Contest 326
    B-326-likeNumbers题意:找到一个不小于n的数是326数,定义是思路:简单的模拟循环即可#include<bits/stdc++.h>usingnamespacestd;boolcheck(intx){ vector<int>a; while(x){ a.push_back(x%10); x/=10; } if(a[1]*a[2]==a[0])returntrue; elsereturnfalse;}......
  • AtCoder_abc326
    T12UP3DOWN简单的if判断,做题一分钟,翻译十分钟。。。代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){ intx,y;cin>>x>>y; if((x<=y&&y-x<=2)||(x>y&&x-y<=3)) cout<<"Yes"; elsecout<<"No&......
  • AtCoder_abc327
    T1ab循环从s[0]到s[n-2]判断有无ab相邻T2A^A两层循环枚举就可以了由于aa会增长的很快,所以当a=16时aa就已经大于$10^{18}$了,一定不会T就这么点数打表也能过T3NumberPlace就是数独的判断规则,h,l,g三个数组存储已有的数就好宫的判断我用了一个三维数组前两个维度表示宫的......
  • AtCoder_abc329
    AtCoder_abc329比赛链接A-SpreadA题链接题目大意输入一个字符串由大写字母组成的$S$,输出$S$并在每一个字符之间加上空格解题思路随便打打就能过.jpg代码//Problem:A-Spread//Contest:AtCoder-SkyInc,ProgrammingContest2023(AtCoderBeginnerContest329)//......
  • AtCoder_abc330
    AtCoder_abc330比赛链接A-CountingPassesA题链接题目大意给出$N$个数$a_1,a_2,a_3\cdots,a_N$,和一个正整数$L$。输出有几个$a_i\leL$.解题思路O(n)遍历一遍就好了代码//Problem:A-CountingPasses//Contest:AtCoder-TOYOTASYSTEMSProgrammingContest20......
  • AtCoder_abc331
    AtCoder_abc331(这次题真的真的真的好难)比赛链接A-Tomorrow题目链接题目大意有一个$M$个月,$D$天的日历,请输出$y年m月z日$的下一天。解题思路先让天数加一,如果超过了$D$就让月份加一,天数减$D$,然后月份同理代码//Problem:A-Tomorrow//Contest:AtCoder-DaiwaSec......
  • ALGO ARTIS Programming Contest 2023 Autumn(AtCoder Regular Contest 168)
    Preface先补一下这场ARC的博客,因为在来回合肥的路上一直在想这场的CD,所以有空后就先把这场补了A-<Inversion>不难发现对于一段连续的<,设其长度为\(x\),则它最少要贡献\(\frac{x(x+1)}{2}\)的答案而我们很容易构造一种方案刚好满足这个下界,只要让每段的结束比下一段的开头大......
  • Advent of Code 2023 solution [Mathematica/Python]
    Day1Part1(*读取文件*)lines=ReadList["E:\\ExplorerDownload\input.txt",String];(*计算校准值*)calibrationValues=ToExpression[StringJoin[#[[1]],#[[-1]]]]&/@(StringCases[#,DigitCharacter]&/@lines);(*打印总和*)Print......