MLE,求大佬调一调
查看原帖
MLE,求大佬调一调
1342321
xunankai楼主2025/8/30 08:41
#include<bits/stdc++.h>
using namespace std;
#define int long long
const string rule="01101110";
string ns(string &s) {
    int n=s.size();
    string res(n,'0');
    for(int i=1;i<n-1;i++) {
        int x=(s[i-1]-'0')*4+(s[i]-'0')*2+(s[i+1]-'0');
        res[i]=rule[7-x];
    }
    return res;
}
int cnt(string &s){
    int cnt=0;
    for(char c:s) {
        if(c=='1') cnt++;
    }
    return cnt;
}
void trim(string &p) {
    int first=p.find('1');
    if(first==string::npos) {
        p="00";
        return;
    }
    int last=p.rfind('1');
    int left=max(0ll,first-2);
    int right=min((int)p.size(),last+3);
    p=p.substr(left,right-left);
    if(p.size()<2||p.substr(0,2)!="00") p="00"+p;
    if(p.size()<2||p.substr(p.size()-2,2)!="00") p=p+"00";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    string s;
    int n;
    cin>>s>>n;
    if(n==0){
        cout<<cnt(s);
        return 0;
    }
    string p="00"+s+"00";
    map<string,pair<int,int>> sn;
    int st=0;
    int os=cnt(p);
    while(st<n) {
        trim(p);
        if(sn.find(p)!=sn.end()) {
            auto pv=sn[p];
            int cl=st-pv.first;
            int co=os-pv.second;
            int cc=(n-st)/cl;
            int c=(n-st)%cl;
            os+=cc*co;
            st=n-c;
            sn.clear();
            if(c==0) break;
        }else{
            sn[p]={st,os};
        }
        if(st>=n) break;
        p=ns(p);
        os=cnt(p);
        st++;
    }
    cout<<os;
    return 0;
}
2025/8/30 08:41
加载中...