题解:P17532 [JAG 2026 Summer Camp #1] Find Iotas

· · 题解

不难题。

我们考虑如何写 checker。

从小往大加数,设 f_{i,0} 为以第一个 i 结尾的 [1,2,3,\cdots,i] 子序列数量,f_{i,1} 为以第二个 i 结尾的 [1,2,3,\cdots,i] 子序列数量,转移是容易的,分讨两个 i 和两个 i-1 的相对位置即可。

令 x=dp_{i,0},y=dp_{i,1},那么我们插入 i 的两个位置能进行以下操作:

容易发现后四个基本没用,且前两个的操作顺序绝对是形如一串 2 操作接上一个 1。

也就是说,我们每次能用 k-1 次操作 x 变为 kx(k\ge 2),注意到这个 x 只能变为其倍数,所以我们每次肯定将 x 变为其 \frac Xx 的最小非 1 因子。

于是我们模拟上述过程即可,最后再把最后一次 1 操作取消掉就能让 x+y=X 了。假如操作次数还没有 n 那么一直用 4 操作。

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

int n,k;
vector<int> res;
void solve(){
    cin>>n>>k;
    if(k==0){
        if(n==1){
            cout<<"No"<<endl;
            return;
        }
        cout<<"Yes"<<endl;
        for(int i=n;i>=1;i--) cout<<i<<' '<<i<<' ';
        cout<<endl;
        return;
    }
    if(k==1){
        if(n==1){
            cout<<"No"<<endl;
            return;
        }
        cout<<"Yes"<<endl;
        cout<<n<<' ';
        for(int i=1;i<n;i++) cout<<i<<' ';
        cout<<n<<' ';
        for(int i=1;i<n;i++) cout<<i<<' ';
        cout<<endl;
        return;
    }
    res.clear();
    int x,y;x=y=1;
    res.push_back(1);
    while(k!=y){
        while(1){
            if(res.size()>n){
                cout<<"No"<<endl;
                return;
            }
            if(k%(x+y)==0){
                x+=y,y=x;
                if(k==y) break;
                res.push_back(1);
                break;
            }
            y+=x;res.push_back(2);
        }
    }
    cout<<"Yes"<<endl;
    int tot=0;
    vector<int> v1,v2,v3;
    for(int i:res){
        tot++;
        if(i==1){
            for(int i:v2) v3.push_back(i);
            for(int i:v2) v3.push_back(i);
            v2.clear();
            v2.push_back(tot);
        }
        else v2.push_back(tot);
    }
    for(int i:v2) v3.push_back(i);
    for(int i:v2) v3.push_back(i);
    while(tot<n){
        tot++;
        v1.push_back(tot);
        v3.push_back(tot);
    }
    for(int i:v1) cout<<i<<' ';
    for(int i:v3) cout<<i<<' ';
    cout<<endl;
}

signed main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int T;cin>>T;while(T--) solve();
    return 0;
}