首页 > 其他分享 >AtCoder Regular Contest 112 F Die Siedler

AtCoder Regular Contest 112 F Die Siedler

时间:2023-04-27 13:22:09浏览次数:40  
标签:AtCoder Siedler ... int ll Contest long ans

洛谷传送门

AtCoder 传送门

感觉太人类智慧了。

设 \(A = (c_1,c_2,...,c_n)\) 表示当前每种牌的数量,\(f(A)\) 为状态 \(A\) 只进行换牌操作最终最少剩下几张牌。

\(f(A)\) 是可以贪心求出的,因为策略必然是能换则换

并且我们发现依次换 \(2,3,...,n,1\),最后 \(c_2 \sim c_n\) 都不会变,而 \(c_1 \gets c_1 - (2^n n! - 1)\)。

因为能换则换,所以不妨把 \(i \ge 2\) 的牌全部倒换成 \(1\) 号牌,这样我们得到了现在的 \(c_1 = X = \sum\limits_{i=1}^n c_i 2^{i-1}(i-1)!\)。

显然有 \(f((X,0,0,...,0)) = f(A)\),说明这样做不会影响最终答案。那我们就可以把一个状态映射到一个数 \(X\),对 \(X\) 去考虑就比对 \(A\) 去考虑容易多了。

设 \(P\) 为初始状态全部倒换成 \(1\) 号牌后得到的数,\(a_i\) 为第 \(i\) 套卡包倒换成 \(1\) 号牌后得到的数。设最终答案为 \(Q\),使用了 \(x_i\) 次第 \(i\) 套卡包,进行了 \(y\) 次依次换 \(2,3,...,n,1\) 的操作,那么有如下等式:

\[Q = P + \sum\limits_{i=1}^n x_i a_i - y(2^n n! - 1) \]

发现是一个不定方程的形式。

裴蜀定理:设 \(a,b\) 为不都为 \(0\) 的整数,则 \(ax + by = d\) 有解的充要条件是 \(d \mid \gcd(a,b)\)。

因为裴蜀定理可以推广到多个数,所以 \(Q\) 需要满足:

\[(Q - P) \mid \gcd(x_1 a_1, x_2 a_2, ..., x_n a_n, 2^n n! - 1) \]

记 \(d = \gcd(x_1 a_1, x_2 a_2, ..., x_n a_n, 2^n n! - 1)\),令 \(Q = kd + (P \bmod d), k \in \mathbb N\),那么问题就变成了求出 \(\min(f(kd + (P \bmod d)))\)。

我们有两个暴力做法。

  • 暴力枚举 \(k\) 并计算,时间复杂度 \(O(n \frac{2^n n! - 1}{d})\)。
  • 跑同余最短路,初始是 \(dis_u = 1, u = 2^{i-1} (i-1)!\),每次枚举要拿哪张牌,答案为 \(dis_{P \bmod d}\),时间复杂度 \(O(nd)\)。

考虑根号分治,发现复杂度其实和 \(2^n n! - 1\) 的最大的 \(\le \sqrt{2^n n! - 1}\) 的因数有关。打表可以发现,\(2^n n! - 1\) 的最大的 \(\le \sqrt{2^n n! - 1}\) 的因数为 \(1214827\),可过。

code
// Problem: F - Die Siedler
// Contest: AtCoder - AtCoder Regular Contest 112
// URL: https://atcoder.jp/contests/arc112/tasks/arc112_f
// Memory Limit: 1024 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

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

const int maxn = 20;
const int maxm = 1220000;

ll n, m, a[maxn], f[maxn], g[maxm];

inline ll calc() {
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += a[i] * f[i - 1];
	}
	return ans;
}

inline ll calc(ll x) {
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += (x % (2 * i));
		x /= (2 * i);
	}
	return ans;
}

void solve() {
	scanf("%lld%lld", &n, &m);
	f[0] = 1;
	for (int i = 1; i <= n; ++i) {
		f[i] = f[i - 1] * i * 2;
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
	}
	ll P = calc(), d = f[n] - 1;
	while (m--) {
		for (int i = 1; i <= n; ++i) {
			scanf("%lld", &a[i]);
		}
		d = __gcd(d, calc());
	}
	if (d <= 1214827) {
		queue<int> q;
		mems(g, -1);
		for (int i = 0; i < n; ++i) {
			g[f[i] % d] = 1;
			q.push(f[i] % d);
		}
		while (q.size()) {
			int u = q.front();
			q.pop();
			for (int i = 0; i < n; ++i) {
				int v = (u + f[i]) % d;
				if (g[v] == -1) {
					g[v] = g[u] + 1;
					q.push(v);
				}
			}
		}
		printf("%lld\n", g[P % d]);
	} else {
		ll ans = 9e18;
		for (ll x = (P % d ? P % d : d); x < f[n]; x += d) {
			ans = min(ans, calc(x));
		}
		printf("%lld\n", ans);
	}
}

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

到底怎么想到的啊

标签:AtCoder,Siedler,...,int,ll,Contest,long,ans
From: https://www.cnblogs.com/zltzlt-blog/p/17358643.html

相关文章

  • AtCoder Regular Contest 123 C 1, 2, 3 - Decomposition
    洛谷传送门AtCoder传送门从低位往高位考虑。设当前个位为\(k\),暴力搜索这一位向上进\(i\)位,设\(\left\lfloor\frac{n}{10}\right\rfloor-i\)的答案为\(t\)。若\(t>10i+k\)显然就不可行,因为就算个位全部填\(1\)也不能补齐;否则\(n\)的答案就是\(\max(t,\l......
  • AtCoder Regular Contest 120 F Wine Thief
    洛谷传送门AtCoder传送门Hint如果是一个环怎么做?Answer由于是一个环,因此环上每个点对最终答案造成的贡献都相同。设$f_{i,j}$为长度为$i$的序列选$j$个不相邻的点的方案数,则$f_{i,j}=\binom{i-j+1}{j}$。应该很好理解,考虑一个长度为$i-j+1$的链,链头、链尾和两......
  • AtCoder Regular Contest 125 E Snack
    洛谷传送门AtCoder传送门很棒的flow题,考虑建二分图。源点向每种零食连边权为\(a_i\)的边,每种零食向每个孩子连边权为\(b_j\)的边,每个孩子向汇点连边权为\(c_j\)的边,这个图的最大流就是答案。直接跑最大流肯定T,考虑最大流等价于求这个图的最小割,因此转而求最小割。......
  • AtCoder Regular Contest 126 D Pure Straight
    洛谷传送门AtCoder传送门很不错的状压。考虑先把最后作为答案的数聚到一起,再算它们的逆序对个数。设\(f_S\)为当前选的数集合为\(S\)的答案。有转移:选\(a_i\),答案加上之前选的比它大的数;不选\(a_i\),此时需要把左边的数或者右边的数往中间挪一个,答案加上左右两端的最......
  • SMU Spring 2023 Trial Contest Round 10
    SMUSpring2023TrialContestRound10 A-RemoveDuplicates#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=2e2+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedef......
  • AtCoder Regular Contest 115 F Migration
    洛谷传送门AtCoder传送门这种最大值最小化的题一般可以先考虑二分。设二分了一个\(mid\)。记\(A=(a_1,a_2,...,a_k)\)为表示每个棋子的位置的状态,如果\(A,B\)可以互相到达,就在它们之间连一条无向边。则要判断的是\(S=(s_1,s_2,...,s_k)\)和\(T=(t_1,t_2,...,t_k......
  • 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest D
    AlexhastwomagicmachinesAandB.MachineAwillgiveyou2x + 1coinsifyouinsertxcoinsinit,machineBwillgiveyou2x + 2.Alexhasnocoinsandwantstogetexactlyncoinsinordertobuyanewunicorn,buthecan’tfigureouthowtodoi......
  • 2014 Pacific Northwest Region Programming Contest—Division 2 Problem U — lim
    Incollegefootball,manydifferentsourcescreatealistoftheTop25teamsinthecountry.Sinceit’ssubjective,theselistsoftendiffer,butthey’reusuallyverysimilar.Yourjobistocomparetwooftheselists,anddeterminewheretheyaresimi......
  • XI Samara Regional Intercollegiate Programming Contest Problem J. Parallelogram
    Therearensticks,thei-thofwhichhaslengthai.Alexwantstoassemblefromthemasmanyparallelogramsaspossiblesimultaneously,witheachstickusedatmostinoneparallelogram.Whatmaximalnumberofparallelogramsisitpossibletoassembl......
  • XI Samara Regional Intercollegiate Programming Contest Problem E. Substring Re
    Twostringssandtofthesamelengtharegiven.Determinewhetheritispossibletomaketfromsusingexactlyonereverseofsomeitssubstring.InputThefirstlinecontainsthestrings,andthesecond—thestringt.Bothstringshavethesamel......