H. Loneliness

该博客探讨了H. Loneliness问题,指出奇数情况无解。文章提到,'DRUL'操作可使位置减少1,'RULD'操作减少2。对于偶数情况,虽然不指定终点,但可以通过特定操作转移起点。建议起始位置设为0以简化构造过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

H. Loneliness

首先,奇数一定无解。
其次,"DRUL"可以减1,"RULD"可以减2。

对于偶数的情况,如果不要求终点,那么构造起来就会比较简单,所以我们考虑移动通过上面两种操作转移起点。

为了使起点移动后方便构造,我们使初值为0。

#pragma GCC optimize("Ofast","inline")

#include<bits/stdc++.h>
using namespace std;

#define BEGIN signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define END return 0;}
#define endl '\n'

#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&-(x))

typedef long long ll;
typedef unsigned long long ull;

template<class T>void read(T& x)
{
    x=0;int f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    x*=f;
}//快读

template<class T>void write(T& x)
{
    int num=0;char c[40];
    while(x)c[++num]=(x%10)+48,x/=10;
    while(num)putchar(c[num--]);
}//快写

void solve()
{
    ll n;
    cin >> n;

    if (n & 1)
    {
        cout << -1 << endl;
        return;
    }

    int a[100] = {};
    int top = 0;
    while (n)
    {
        a[++top] = n % 2;
        n /= 2;
    }

    int sum1 = 0;
    for (int i = 1;i <= top;++i)
        sum1 += (a[i] == 1);
    int down = 100 - (top - 1);
    int left = 100 - (sum1 + 1);
    
    // 将初值改为0,并转移起点,保证构造后刚好到达点(100,100)
    cout << "DRUL";
    for (int i = 1;i <= down;++i)cout << 'D';
    for (int i = 1;i <= left;++i)
    {
        cout << "R";
        cout << "RULD";
    }
    
    //  开始构造
    vector<int>ve;
    for (int i = top;i >= 1;--i)
        if (a[i])ve.push_back(i - 1);
    ve.push_back(1);
    cout << 'R';
    for (int i = 0;i < ve.size() - 1;++i)
    {
        while (ve[i] != ve[i + 1])cout << 'D', --ve[i];
        if (i < ve.size() - 2)cout << 'R';
    }
    cout << endl;
}

BEGIN
#ifdef FSLSE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);

    //  手动修改栈内存
    int size=64<<20;  //  申请64MB栈空间

    //  32位Windows使用
    //  __asm__("movl %0, %%esp\n"::"r"((char*)malloc(size)+size));

    //  Linux以及64位Windows使用
    __asm__("movq %0, %%rsp\n"::"r"((char*)malloc(size)+size));

#endif

    int T, n;
    cin >> T >> n;
    while (T--){
        solve();
    }

#ifdef FSLSE
    exit(0);  //  手动修改栈内存时一定要有exit(0)
#endif
END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值