蒟蒻求助
查看原帖
蒟蒻求助
1283045
NOI109_bjh楼主2025/1/26 13:28

样例过不了。。。

#include<bits/stdc++.h>
#define int long long
#define ci const int

using namespace std;

ci e=310;
int n,ans=-1;
char a[e],b[e],c[e];

bool f(){
    int len=strlen(a+1);
    for(int i=1;i<=len/2;i++)
        if(a[i]!=a[len-i+1])return 0;
    return 1;
}

void h(){
    int len=strlen(a+1);
    for(int i=1;i<=len;i++)
        b[len-i+1]=a[i];
    return;
}

void add(){
    int len=strlen(a+1);
    for(int i=len;i>0;i--){
        c[i+1]=a[i]+b[i]-'0';
        if(c[i]>='0'+n)
            c[i+1]=c[i+1]-n,c[i]++;
    }
    return;
}

void dfs(int cou){
    if(cou>30)return;

    if(cou<=30&&f()){
        ans=cou;
        return;
    }

    h();
    add();

    for(int i=1,j=1;c[j]!='\0';i++,j++){
        if(i==1&&c[i]=='0')j++;
        a[i]=c[i];
    }
    
    dfs(cou+1);
    return;
}

signed main(){
    ios::sync_with_stdio(0),
    cin.tie(0),cout.tie(0);

    cin>>n>>a+1;

    dfs(0);

    if(ans==-1)cout<<"Impossible!";
    else cout<<"STEP="<<ans;
    
    return 0;
}
2025/1/26 13:28
加载中...