Red_Cow @ 2023-11-17 18:22:35
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
char s1[100];
int a1[100]={0},a2[100]={0};
int N,i,len,ans=0,flag=1;
scanf("%d",&N);
getchar();
gets(s1);
len=strlen(s1);
for(i=0;i<len;i++)//将M的每一位放进数组
{
if((s1[i]>='a'&&s1[i]<='f')||(s1[i]>='A'&&s1[i]<='F')) a1[i]=s1[i]-'a'+10;
else a1[i]=s1[i]-'0';
}
while(ans<=30)
{
flag=1;
for(i=0;i<len;i++)//判断是否回文
{
a2[i]=a1[len-1-i];
if(a1[i]!=a1[len-1-i]) flag=0;
}
if(flag==0)//相加得到新数组
{
ans++;
for(i=0;i<len;i++)
{
a1[i]=a1[i]+a2[i];
if(a1[i]>=N)
{
a1[i]-=N;
a1[i+1]++;
if(i==len-1) len++;
}
}
}
else break;
}
if(ans>30) printf("Impossible");
else printf("STEP=%d",ans);
return 0;
}
by Red_Cow @ 2023-11-21 19:06:26
数据#2本地也是对的啊
by zrl123456 @ 2023-12-18 21:49:48
#include <bits/stdc++.h>
using namespace std;
int n,s[150],a[2][150],len,k;
char ch;
int main(){
cin>>n;
while(cin>>ch){
if(ch>='0'&&ch<='9') s[++k]=ch-48;
else s[++k]=ch-55;
}
for(int i=k;i>=1;i--) a[0][i]=s[k+1-i];
len=k;
for(int i=1;i<=30;i++){
memset(a[i&1],0,sizeof(a[i&1]));
for(int j=len;j>=1;j--){
a[i&1][j]+=a[(i-1)&1][j]+a[(i-1)&1][len+1-j];
a[i&1][j-1]+=a[i&1][j]/n;
a[i&1][j]%=n;
}
bool b=true;
if(a[i&1][0]){
for(int j=len;j>=0;j--)
a[i&1][j+1]=a[i&1][j];
len++;
a[i&1][0]=0;
}
for(int j=1;j<=(len+1)/2;j++)
if(a[i&1][j]!=a[i&1][len+1-j])
b=false;
if(b){
cout<<"STEP="<<i;
return 0;
}
}
cout<<"Impossible!";
return 0;
}