【CodeForces - 1034B】Little C Loves 3 II

版权声明:本文为博主原创文章……懂吗?要尊重别人的劳动成果呐 https://blog.csdn.net/Tiw_Air_Op1721/article/details/82811681


@题目描述 - English@

time limit per test: 1 second
memory limit per test: 256 megabytes

Little C loves number «3» very much. He loves all things about it.
Now he is playing a game on a chessboard of size
n×m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (xi,yi) and (xj,yj) is defined as |xi−xj|+|yi−yj|.

He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place.

Input
A single line contains two integers n and m (1≤n,m≤10^9) — the number of rows and the number of columns of the chessboard.

Output
Print one integer — the maximum number of chessmen Little C can place.

Examples

input
2 2
output
0

input
3 3
output
8

Note
In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0.
In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).

扫描二维码关注公众号,回复: 4104771 查看本文章

@中文题意@

n*m的矩阵,当两个点(x1, y1)与(x2, y2)曼哈顿距离为3时可以将两个点匹配。每个点只能够与一个点匹配。求最多能可以匹配多少个点。
n,m <= 10^9

@分析@

【这种题也只能够手算小数据来找规律……】
首先弄清楚一个上界:对于任意的 n m n*m 矩阵,如果n与m都为奇数,则答案最大为nm-1;否则答案最大为nm。
不妨假设n <= m。
当 n = 1 的时候,我们只能横着进行匹配。我们手算几组数据,可以用如下图的方法可以把 1 6 1*6 所有点都匹配。
1*6
再手算几组,我们发现这样一个规律:对于任意一个 1 m 1*m ,如果m%6<=3,则能匹配的点数为m-m%6;否则能匹配的点数为m-6+m%6。
【具体的证明应该可以用归纳法证明,请容许我偷一下懒qwq】

当 n = 2 的时候。 2 2 2*2 显然无解, 2 3 2*3 的矩阵中间那一列的元素没有任何元素能跟它们匹配,所以最大匹配点数也只能为4。 2 4 2*4 2 5 2*5 可以采用下列所示的方法全部匹配完。
2*42*5
2 6 2*6 矩阵可以把它拆成两个 1 6 1*6 的矩阵,每个矩阵内部可以全部匹配完。
然后!学长们就是被 2 7 2*7 的矩阵卡掉了。实际上 2 7 2*7 不能构造出全部匹配的情况的。如图:两个蓝色块只能与两个紫色块匹配,所以紫色块只能与蓝色块匹配。同理,两个红色块只能与两个棕色块匹配,所以棕色块只能与红色块匹配。然后有一个块既要和蓝色块匹配又要和棕色块匹配,所以不可能~
2*7(1)2*7(2)
所以 2 7 2*7 最多只能配对12个点。
然后,最关键的来了。对于一个 2 m 2*m (m>=7),如果m为偶数,我们可以把m拆成若干个4与6的和,即将原矩阵拆成若干个 2 4 2*4 矩阵与 2 6 2*6 矩阵。对于这些矩阵我们可以把所有点匹配完,所以我们自然也就可以匹配完 2 m 2*m 的所有点;反之,如果m为奇数,我们可以先将 2 m 2*m 分成 2 5 2*5 2 ( m 5 ) 2*(m-5) 两个部分, 2 5 2*5 可以匹配完。又因m-5是个偶数,所以奇数也可以得到相似的结论。
即: 2 m m &gt; = 7 2*m(m&gt;= 7) 的矩阵,答案总可以达到上界 2 m 2*m

下一步,当n = 3的时候。
3 3 3*3 3 4 3*4 3 5 3*5 的构造如下:
3*33*43*5
3 6 3*6 就可以分成3个 1 6 1*6 拼出,就不画图了。
于是,对于一个 3 m 3*m 的矩阵,一样地,如果m是偶数,就将m拆成若干个4与6的和;否则就拆成(m-3)与3。所以, 3 m m &gt; = 3 3*m(m&gt;=3) 的答案也总是可以达到上界

对于n = 4,我们已知 4 2 4*2 4 3 4*3 可以构造出来。 4 4 4*4 可以拆成两个 4 2 4*2 。类似的推理, 4 m m &gt; = 4 4*m(m&gt;=4) 的答案总可以达到上界。

对于n = 5,我们已知 5 2 5*2 5 3 5*3 5 4 5*4 可以构造出来。因此 5 m m &gt; = 5 5*m(m&gt;=5) 的答案总可以达到上界。

对于n = 6,我们已知 6 1 6*1 可以构造出来。因此 6 m m &gt; = 6 6*m(m&gt;=6) 的答案总可以达到上界。

然后,对于n>6。如果n为偶数,可以把n拆成若干个4与6的和;否则如果m为偶数,可以把n拆成若干个4与6的和;否则,可以把n拆成(n-3)与3,然后重复上面的推理。因此 n m n &gt; 6 , m &gt; n n*m(n&gt;6, m&gt;n) 的答案总可以达到上界。

至此,本题就Over了。

@代码@

图片可能看起来有些太鲜艳了……但是我也不知道什么更好的方法可以表达“将两个点匹配起来”qwq……
如果眼睛瞎了,请不要打我qwq
一样地,如果有任何的疑问或者bug,可以留言在下面告诉我,我会尽力解答你们的疑惑的qwq!

#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	if( n > m ) swap(n, m);
	if( n == 1 ) {
		if( m % 6 == 0 )
			printf("%d\n", m);
		else if( m % 6 <= 3 )
			printf("%d\n", m-(m%6));
		else printf("%d\n", m-(6-m%6));
	}
	else if( n == 2 && m == 2 )
		printf("%d\n", 0);
	else if( n == 2 && m == 3 )
		printf("%d\n", 4);
	else if( n == 2 && m == 7 )
		printf("%d\n", 12);
	else {
		if( n % 2 == 1 && m % 2 == 1 )
			printf("%I64d\n", 1LL*n*m-1);
		else printf("%I64d\n", 1LL*n*m);
	}
}

@END@

就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。

猜你喜欢

转载自blog.csdn.net/Tiw_Air_Op1721/article/details/82811681