The 15th Chinese Northeast Collegiate H - Loneliness(思维,构造)

Posted issue是fw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The 15th Chinese Northeast Collegiate H - Loneliness(思维,构造)相关的知识,希望对你有一定的参考价值。

LINK

注意

左上角是 ( 1 , 1 ) (1,1) (1,1),右下角是 ( n , n ) (n,n) (n,n)

如果无脑走下去,会乘上一个 2 n − 1 2^{n-1} 2n1,早就爆了,所以需要构造 0 0 0

考虑 D R U L DRUL DRUL绕一圈可以让数字减 1 1 1, R D L U RDLU RDLU绕一圈可以让数字加 1 1 1

当原数字为偶数时, L U R D LURD LURD可以加 2 2 2,而 U L D R ULDR ULDR可以减 2 2 2

k < = 198 k<=198 k<=198

这样的话,当 k k k比较小的时候,可以先一直在原点为 0 0 0往下走到 ( n , 1 ) (n,1) (n,1),然后一直往右走到 ( n , n ) (n,n) (n,n)可以得到 198 198 198

不过没关系,我们可以靠自环 − 1 -1 1或者 − 2 -2 2来修正,所以 k < = 198 k<=198 k<=198是没问题的


k > 198 k>198 k>198

考虑先在原点构造一个 0 0 0,一直往下走到某个点 ( 1 , x ) (1,x) (1,x)

然后在 ( 1 , x ) (1,x) (1,x) ( 1 , n ) (1,n) (1,n)可以使用函数 f ( x ) = 2 x + 1 f(x)=2x+1 f(x)=2x+1(自环加 1 1 1再往下)和 f ( x ) = 2 x f(x)=2x f(x)=2x(直接往下走)

根据数字的二进制位某位是否有 1 1 1判断执行哪个函数

这样很容易就可以构造出 k − 198 k-198 k198

然后在 ( 1 , n ) (1,n) (1,n)一直往右边走到 ( n , n ) (n,n) (n,n)即可得到 k k k

#include <bits/stdc++.h>
using namespace std;
#define int long long
int t,n,k;
void add1(){ cout << "RDLU"; }
void add2(){ cout << "LURD"; }
void sub1(){ cout << "DRUL"; }
void sub2(){ cout << "ULDR"; }
void solve2()
{
	int sum = 0;
	k -= 198;
	for(int i=1;i<=40;i++)	cout << "D";
	for(int i=41,j=n-i;i<n;i++,j--)
	{
		if( k&(1ll<<j) )	add1();
		cout << "D";	sum *= 2;
	}
	for(int i=1;i<n;i++)	cout << "R";
}
void solve1()
{
	int sum = 0;
	for(int i=1;i<n;i++)	cout << "D";
	for(int i=1;i<n;i++)
	{
		cout << "R", sum += 2;
		while( sum>k )	sub2(), sum -= 2;
	}
}
signed main()
{
	int t; cin >> t >> n;
	while( t-- )
	{
		cin >> k; 
		if( k&1 ){ cout << -1 << '\\n'; continue; }
		sub1();
		if( k<=198 )	solve1();
		else	solve2();
		puts("");
	}
}

以上是关于The 15th Chinese Northeast Collegiate H - Loneliness(思维,构造)的主要内容,如果未能解决你的问题,请参考以下文章

The 15th Chinese Northeast Collegiate C. Vertex Deletion(树形dp)

The 15th Chinese Northeast L. k-th Smallest Common Substring(广义SAM,对节点字典序排序)

The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion (树形dp)

The 13th Chinese Northeast Contest H. Skyscraper(差分+树状数组)

The 13th Chinese Northeast Contest B. Balanced Diet(前缀和)

The 13th Chinese Northeast Contest C. Line-line Intersection(平面几何)