首页 > 其他分享 >[AGC061D] Almost Multiplication Table

[AGC061D] Almost Multiplication Table

时间:2023-04-18 12:11:06浏览次数:43  
标签:ch return Almost ll int AGC061D Multiplication inline check

人类智慧。

答案显然具有可二分性,考虑如何check。

我们使用调整法,不妨设 \(x_n<y_m\) (反着做同理),一开始我们令 \(x_i=1,y_i=+\infty\)。每次我们期望让 \(x\) 不断变大,\(y\) 不断变小,不断将它们调整到当前的上下界。具体的,每次令 \(x_i=\max \{x_i, \max \lceil {a_{i,j}-k\over y_j} \rceil\}, y_i = \min \{y_i, \min \lfloor {a_{i,j}+k\over x_j} \rfloor \}\)。为了保证单调性,还需令 \(x_i=\max\{x_i,x_{i-1}+1\}\),\(y_i=\min\{y_i,y_{i+1}-1\}\)

显然在任意时刻,都存在合法的 \(x\) 大于等于当前的 \(x\),最后一定会到一个合法解。如果当前 \(x_n>y_m\) 或 \(y_1 \le 0\) 就直接 break.

考虑它的复杂度,每个 \(x\) 至多会增加到根号值域,由于 \(n,m\) 很小,可以轻松跑过。

#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 15;
 
inline int read() {
	register int s = 0, f = 1; register char ch = getchar();
	while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
	while (isdigit(ch)) s = (s * 10) + (ch & 15), ch = getchar();
	return s * f; 
}
 
typedef long long ll;
 
const ll inf = 2e9;
 
ll a[N][N], x[N], y[N];
int n, m;
 
inline ll abs_(ll a) {
	return a < 0 ? -a : a;
}
 
inline bool chk(ll k) {
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			if (x[i] <= 0 || y[j] <= 0 || abs_(x[i] * y[j] - a[i][j]) > k) return 0;
	return 1;
}
 
inline bool check(ll k) {
	for (int i = 1; i <= n; ++i) x[i] = 1;
	for (int i = 1; i <= m; ++i) y[i] = inf;
	y[m + 1] = inf + 1;
	while (1) {
		for (int i = 1; i <= n; ++i) {
			ll r = -1;
			for (int j = 1; j <= m; ++j)
				r = max(r, (a[i][j] - k + y[j] - 1) / y[j]);
			x[i] = max(max(x[i], x[i - 1] + 1), r);
		}
		for (int i = m; i; --i) {
			ll r = inf;
			for (int j = 1; j <= n; ++j)
				r = min(r, (a[j][i] + k) / x[j]);
			y[i] = min(min(y[i], y[i + 1] - 1), r);
		} if (chk(k)) return 1;
		if (x[n] > y[m] || y[1] <= 0) break;
	}
	for (int i = 1; i <= n; ++i) x[i] = inf;
	for (int i = 1; i <= m; ++i) y[i] = 1;
	x[n + 1] = inf + 1;
	while (1) {
		for (int i = 1; i <= m; ++i) {
			ll r = -1;
			for (int j = 1; j <= n; ++j)
				r = max(r, (a[j][i] - k + x[j] - 1) / x[j]);
			y[i] = max(max(y[i], y[i - 1] + 1), r);
		}
		for (int i = n; i; --i) {
			ll r = inf;
			for (int j = 1; j <= m; ++j)
				r = min(r, (a[i][j] + k) / y[j]);
			x[i] = min(min(x[i], x[i + 1] - 1), r);
		} if (chk(k)) return 1;
		if (y[n] > x[m] || x[1] <= 0) break;
	}
	return 0;
}
 
int main() {
	n = read(); m = read();
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) a[i][j] = read();
	ll l = 0, r = 1e9, mid, res = r;
	while (l <= r) {
		mid = (l + r) >> 1;
		if (check(mid)) r = (res = mid) - 1;
		else l = mid + 1;
	} check(res);
	printf("%lld\n", res);
	for (int i = 1; i <= n; ++i) printf("%lld ", x[i]);
	puts("");
	for (int i = 1; i <= m; ++i) printf("%lld ", y[i]);
	return 0;
} 

标签:ch,return,Almost,ll,int,AGC061D,Multiplication,inline,check
From: https://www.cnblogs.com/wwlwakioi/p/17329128.html

相关文章

  • POJ - 1651 Multiplication Puzzle(区间dp)
    题目大意:给你N个数,每次可以选择一个数进行剔除(第一个和最后一个不能选择),选出该数后,sum+=该数左边的数*该数*该数右边的数问最小的sum是多少解题思路:用dp[i][j]表示[i,j]区间被剔除得只剩下i,j的最小sumdp[i][j]=dp[i][k]+dp[k][j]+num[i]*num[k]*num[j]#include......
  • C - Almost Sorted
    https://atcoder.jp/contests/arc132/tasks/arc132_c很难想到的动态规划,优化空间的思路非常巧妙用相对位置来转移f[i][j]表示i之前,放置数字的压缩情况为j,的所有方案数**f[i+1][(j|(1<<k))>>1]+=f[i][j]**k表示i放的数字的相对位置具体转移还是看代码#include<b......
  • CF888D Almost Identity Permutations 题解
    CF链接:AlmostIdentityPermutationsLuogu链接:AlmostIdentityPermutations${\scr\color{Aquamarine}{\text{Solution}}}$前言这好像是一道能用数学秒掉的题目但......
  • Creating double-precision integer multiplication with a quad-precision result fr
    Creatingdouble-precisionintegermultiplicationwithaquad-precisionresultfromsingle-precisionmultiplicationwithadouble-precisionresultRaymondC......
  • Codeforces CF255C Almost Arithmetical Progression
    链接难度:\(1500\)有一个序列\(b_{1\simn}\)。你需要从中选出一个长度最长的子序列\(p_{1\simk}\),使其满足\(p_1=p_3=...=p_{\lceil\frac{k}{2}\rceil-1},p_2=p_4=......
  • UVA 442 Matrix Chain Multiplication
    原题Vjudge题目大意模拟矩阵链乘的计算,如果出现错误就输出error,否则输出总共的乘法次数对于一个矩阵\(A(m\timesn),B(n\timesp)\)乘法次数为\(m\timesn\timesp......
  • poj1651 Multiplication Puzzle--区间dp
    原题链接:​​http://poj.org/problem?id=1651​​题意:给出N个数,每次从中抽出一个数(第一和最后一个不能抽),每次的得分为抽出的数与相邻两个数的乘积,直到只剩下首尾两个数为......
  • test cpu performance with matrix multiplication
    一需求:测试cpu计算性能二方法:1.使用一定规模方阵执行乘法运算,不需要保存结果。2.根据CPU核数开启线程执行乘法运算3.事先将线程执行任务放入线程对应的任务容器,然后开启线......
  • C. Almost All Multiples
    C.AlmostAllMultiplesGiventwointegers$n$and$x$,apermutation$^{\dagger}$$p$oflength$n$iscalledfunnyif$p_i$isamultipleof$i$forall$1\l......
  • 「题解」Codeforces 1730F Almost Sorted
    给定一个长度为\(n\)的置换\(p\),以及一个正整数\(k\).对于一个置换\(q\),要求对于所有满足\(1\leqi<j\leqn\)的\(i,j\),有以下不等式成立:\[p_{q_i}\leqp_{q_j}+......