跳到正文

个人记录

题解【画地为佬】

PBCWZCC约 11 分钟
原文
文章信息与版本

文章信息

发布
2019年11月4日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 11 分钟
获赞
49

版本

  1. v1当前

前排谢罪:当时OEIS的时候没有注意到能够A掉这题的,然后在比赛主页放言“无O(EIS)”做法,这些都是本人的疏忽,导致了比赛秩序的混乱,给大家带来不便,特此深表歉意。。。

比赛

题目传送门

灵感来源传送门

顺便stO 本校大佬Christopher_Yan Orz\mathsf{stO~\text{本校大佬}Christopher\_Yan~Orz}

10pts\large\bold{10pts}

m=3s+1, m∈N+ ,m = 3s+1,~m\in \bold{N_+}~,

s=⌊m−13⌋;s = \lfloor\frac{m-1}{3}\rfloor;

用这个式子做,单组数据时间复杂度O(1)\mathrm{O}(1)。

(妄想拿这个AC\mathrm{AC}?)

这个在m<12m<12时没有问题,但再往后就不对了。

比如,就在m=12m = 12 时:

m=3s+1m = 3s+1 对应的图形:

LuoguU92904_WA

出来的答案是33;

然而,最优的做法是:

LuoguU92904_AC

答案是44。

20pts\large\bold{20pts}

实际上,我们知道,为了使答案最小,在围新的地块时,我们要尽可能地少用围墙。也就是说,新建的地块要和尽量多的旧地块相邻。

举例,当m=24m=24时,我们从m=12m=12的基础上开始看:

LuoguU92904_step0

新的地块要和尽量多的旧地块相邻,因此我们不妨这样做:

LuoguU92904_step1

现在我们使用了1515条围墙;

新的地块要和尽量多的旧地块相邻(重要的事情说三遍),我们的下一步是:

LuoguU92904_step2

只再用22条围墙便可新建一个地块。

同理,我们这样用掉剩下的77条围墙:

LuoguU92904_effect

所以,当m=24m = 24时,s=9s = 9;

由此,我们可以轻松手推出m≤60m\leq60的所有答案,单组数据时间复杂度O(1)\mathrm{O}(1)。

40pts\large\bold{40pts}

不知道有没有O(m2)\mathrm{O}(m^2)做法。

60pts\large\bold{60pts}

(没想到比赛时没有人考虑这个,大家就跳过吧。。)

根据上面的理论,我们还能够发掘出这样的关系:

一张DAG\mathrm{DAG},地块的围建顺序在这张图上体现为拓扑序。

然而,想要尽可能地先占据需要围墙少的新地块,我们可以对这种图稍作处理:

这样沿着图跑,到一个节点让mm减去上面的数,如果到第xx个点时m<0m<0了,那么答案就是x−1x-1;

至于这个图的大小,我们不妨假设ss是个完全平方数,那么所有的小正方形地块可以拼成一个s×s\sqrt{s}\times\sqrt{s}的大正方形,此时mm满足m=2(s−1)2+2sm = 2(\sqrt{s}-1)^2+2\sqrt{s}。

可知⌈s ⌉=⌈2m+1−12⌉\lceil\sqrt{s}~\rceil=\lceil\frac{\sqrt{2m+1}-1}{2}\rceil,那么我们就开(2m+1−12)2(\frac{\sqrt{2m+1}-1}{2})^2的数组来存储。

附60pts\mathrm{60pts}代码:

(为阅读方便特意调整了代码,因为本人码风过于奇葩。。)

C++
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int MAXN = 1003; 
LL m, n, val[MAXN][MAXN]; 
struct Pg
{
	int n, m; 
	Pg(){}
	Pg(const int i, const int j)
	{
		n=i, m=j; 
	}
}; 
Pg to[MAXN][MAXN]; 
//int ind[MAXN][MAXN]; 
void init(void){
	int i, j, k; 
	val[1][1] = 4; 
	to[1][1].n = 1; 
	to[1][1].m = 2; 
	for(i = 2; i <= n; ++i)
	{
		val[i][i] = 2; 
		to[i][i] = Pg(1, i+1); 
//		ind[i][i] = 2; 
		for(j=2, k=i-2; j <= k; ++j)
		{
			val[i][j] = val[j][i] = 2; 
			to[i][j] = Pg(i, j+1); 
			to[j][i] = Pg(j+1, i); 
//			ind[i][j] = ind[j][i] = 2; 
		}
		val[i][i-1] = val[i-1][i] = 2; 
		to[i][i-1] = Pg(i, i); 
		to[i-1][i] = Pg(i, 1); 
//		ind[i][i-1] = ind[i-1][i] = 2; 
		val[i][1] = val[1][i] = 3; 
		to[i][1] = Pg(i, 2); 
		to[1][i] = Pg(2, i); 
//		ind[i][1] = ind[1][i] = 1; 
	}
	to[1][2] = Pg(2, 1); 
}
int search()
{
	Pg t; 
	int i, j, r, ans; 
	for(i=1, j=1, r=m, ans=-1; r>=0; ++ans)
	{
		r -= val[i][j]; 
		t = to[i][j]; 
		i = t.n; 
		j = t.m; 
	}
	return ans; 
}
int T; 
int main()
{
	double s; 
	scanf("%d", &T); 
	while(T--)
	{
		scanf("%lld", &m); 
		s = (sqrt(2.0*m + 1.0) + 1.0)/2.0; 
		n = (int)ceil(s); 
		init(); 
		printf("%d\n", search()); 
	}
	return 0; 
}

多组数据时,此做法的时间复杂度O(Tm)\mathrm{O}(Tm)。

100pts\large\bold{100pts}

60pts\mathrm{60pts}的思想不足以解决1e15\mathrm{1e15}级别的数据。

再回来看我们在做20pts\mathrm{20pts}时推出来的数列:

(为了围成下一个地块另外需要的围墙数)

4,3,3,2,3,2,3,2,2,3,2,2,3,2,2,2, ...\color{black}4,\color{red}3,\color{blue}3,\color{black}2,\color{red}3,\color{black}2,\color{blue}3,\color{black}2,\color{black}2,\color{red}3,\color{black}2,\color{black}2,\color{blue}3,\color{black}2,\color{black}2,\color{black}2,~...

此时关注红色33的下标,设第一个数字44的下标为11,那么红33的下标为:

2,5,10,17,...2,5,10,17,...

容易得知第xx个红33的下标为x2+1x^2+1;

同理,我们可以推出第yy个蓝33的下标为y2+y+1y^2+y+1;

那么分别解方程x2+1=s, y2+y+1=sx^2+1 = s, ~ y^2+y+1 = s,解得其正数解分别为x=s−1, y=4s−3 −12x = \sqrt{s-1}, ~ y = \frac{\sqrt{4s-3}~-1}{2},此时,我们有(⌊x⌋+⌊y⌋)(\lfloor x\rfloor+\lfloor y\rfloor)个,也就是(⌊s−1⌋+⌊4s−3 −12⌋)(\lfloor\sqrt{s-1}\rfloor + \lfloor\frac{\sqrt{4s-3}~-1}{2}\rfloor)个33;

如果这些小正方形地块恰能够拼成s×s\sqrt{s}\times \sqrt{s}的大正方形,则m=2(s−1)2+2sm = 2(\sqrt{s}-1)^2+2\sqrt{s};

在一般情况下,则有

m=2s+(⌊s−1⌋+⌊4s−3 −12⌋)+2   (1)m = 2s + (\lfloor\sqrt{s-1}\rfloor + \lfloor\frac{\sqrt{4s-3}~-1}{2}\rfloor) + 2~~~(1)

即2×(22\times(2的个数)+3×(3)+3\times(3的个数)+4×(4)+4\times(4的个数)=2×)=2\times总个数s+3s+3的个数+2×(4+2\times(4的个数))。

记(1)(1)式代入ss计算之后的结果为m′m'。ss不容易精确地求出,那我们可以在ss的大小的区间[1, (⌈2m+1−12⌉)2 ][1, ~ (\lceil\frac{\sqrt{2m+1}-1}{2}\rceil)^2 ~]上二分,求出使得m′<mm' < m且m−m′m - m'最小的ss,即为答案。

(叙述略麻烦,希望大家能理解)

100pts\mathrm{100pts}时间复杂度:O(Tlog⁡2m)\mathrm{O}(T\log_2{m})

(本来mm还可以更大的,但再大就炸精度了QwQ\mathfrak{QwQ})

Code\large\bold{Code}

(为阅读方便特意调整了代码,原因同上)

C++
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
LL m, n, N; 
double s; 
int ksum[10] = {0, 4, 7, 10, 12, 15, 17, 20, 22, 24}; 
LL calcsum(const LL i)
{
	if(i < 10) return ksum[i]; 
	LL ret = i*2; 
	double n1 = sqrt(i-1); 
	double n2 = (sqrt(4*(i-1)+ 1)- 1)/ 2; 
	n1 = floor(n1); n2 = floor(n2); 
	ret += (LL)(n1+n2) + 2; 
	return ret; 
}
LL search(const LL l, const LL r, const LL vl, const LL vr)
{
	if(l == r) return l; 
	LL mid = (l+r)/2;
	LL vmid = calcsum(mid); 
	if(m < vmid)
		return search(l, mid, vl, vmid); 
	else
		return search(mid+1, r, calcsum(mid+1), vr); 
}
LL ans; 
int T; 
int main()
{
	scanf("%d", &T); 
	while(T--)
	{
		scanf("%lld", &m); 
		s = (sqrt(2.0*m + 1.0) - 1.0)/ 2.0; 
		n = (int)ceil(s); 
		N = n*n + 1; 
		ans = search(1, N, calcsum(1), calcsum(N)) - 1; 
		printf("%lld\n", ans); 
	}
	return 0; 
}
原文
存档评论42 条抓取于