跳到正文

题解

题解:P13996 【MX-X19-T5】「FeOI Round 4.5」天使のクローバー

DuskDawn约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年9月7日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 4 分钟
题目
P13996

版本

  1. v1当前

这题不难啊,为什么卡掉了那么多人

首先这个东西一看就非常的有规律赏心悦目,再结合数据范围基本确定就是打表或者推式子之类的了。

于是先写一段代码生成 fx(s)f^x(s):

C++
#include <bits/stdc++.h>
using namespace std;
int x,n=2,s[101]={0,1};
vector<int> t;
int main(){
    cin>>x;
    for (int i=1;i<=x;i++){
        t.clear();
        for (int j=0;j<n;j++){
            if (s[j]==0)t.push_back(j%2);
            else t.push_back(j%2),t.push_back(j%2),t.push_back(j%2);
        }
        n=0;
        for (int v:t)s[n++]=v;
    }
    for (int v:t)cout<<v<<" ";
}

当 x=3x=3 到 55 时的输出:

代码
x=0:0 1
x=1:0 1 1 1
x=2:[0 1 1 1 0 0 0 1 1 1]

x=3:[0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1]
x=4:[0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1] 0 1 1 1 [0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1]
x=5:[0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1] 0 1 1 1 [0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1] [0 1 1 1 0 0 0 1 1 1] [0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1] 0 1 1 1 [0 1 1 1 0 0 0 1 1 1] 0 1 [0 1 1 1 0 0 0 1 1 1]

发现没?fx(s)=fx−1(s)+fx−3(s)+fx−1(s)f^x(s)=f^{x-1}(s)+f^{x-3}(s)+f^{x-1}(s)。加号表示序列拼接。

到这一步这道题就很简单了,直接分治就行。

首先预处理出所有的 fx(s)f^x(s) 的长度。由于 r≤1018r \le 10^{18},当 fx(s)f^x(s) 的长度大于 101810^{18} 时就停止。

每次询问先判断 rr 是否出界,然后递归将问题规模 xx 缩小到 33,直接输出 srs_r。缩小 xx 时要修改 rr 在新的 fx(s)f^x(s) 的位置。

记 nn 为 fx(s)f^x(s) 的长度,时间复杂度仅 O(qlog⁡n)O(q \log n)。代码极短。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int f[64]={2,4,10};
int a[]={0,1,1,1,0,0,0,1,1,1};
int q,x,r;
int solve(int x,int r){
    if (r<11)return a[r];
    //计算r在新的序列下对应的位置
    //以当前序列为[0 1 1 1 0 0 0 1 1 1][0 1][0 1 1 1 0 0 0 1 1 1]举例
    if (r<f[x-1])return solve(x-1,r);//r在第一个[0 1 1 1 0 0 0 1 1 1]部分
    else if (f[x-1]<=r&&r<f[x-1]+f[x-3])return solve(x-3,r-f[x-1]);//r在中间的[0 1]部分
    return solve(x-1,r-f[x-1]-f[x-3]);//r在第二个[0 1 1 1 0 0 0 1 1 1]部分
}
signed main(){
    for (int i=3;f[i-1]<=1e18;i++)f[i]=f[i-1]*2+f[i-3];
    cin>>q;
    while (q--){
        cin>>x>>r;x=min(x,52ll);//当问题规模大于52时直接砍到52即可
        if (r>=f[x])cout<<"-1\n";
        else cout<<solve(x,r)<<endl;
    }
    return 0;
}
原文