题解:CF2157F Git Gud

· · 题解

考虑分块套分块。对于每一层分块均执行以下算法:从小到大遍历所有可能的块内位置,从大往小遍历所有块。最后将所有点调整到最外层的分块边界上后直接暴力,随便取一组外层块大小 10000 内层块大小 100 就已经可以通过了。

#include<bits/stdc++.h>
using namespace std;
int n;
const int B1 = 1e4;
const int B2 = 100;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    if(n==4){
        cout<<"4\n";
        cout<<"1 4\n";
        cout<<"3 1\n";
        cout<<"2 1\n";
        cout<<"3 1\n";
        return 0;
    }else{
        vector< pair<int,int> > ans;
        for(int i=1;i<B2;i++){
            vector<int> opt;
            for(int j=i;j<=n;j+=B2){
                opt.push_back(j);
            }
            reverse(opt.begin(),opt.end());
            for(int x:opt) ans.push_back({x,1});
        }
        //所有点都在 B2 的倍数上
        for(int i=1;i*B2<B1;i++){
            vector<int> opt;
            for(int j=i*B2;j<=n;j+=B1){
                opt.push_back(j);
            }
            reverse(opt.begin(),opt.end());
            for(int x:opt) ans.push_back({x,B2});
        }
        //所有点都在 B1 的倍数上
        for(int i=B1;i<n;i+=B1){
            ans.push_back({i,B1});
        }
        cout<<ans.size()<<"\n";
        for(pair<int,int> now:ans) cout<<now.first<<" "<<now.second<<"\n";
    }
    return 0;
}