wushang12138 @ 2024-12-17 23:20:12
int hw(int ret,int N) { int sum = 0; int n = N ret; while(n /= N) { sum = sum N + (n % N); } if (sum == ret) return 0; else return 1; } int add(int M,int N) { int m = N M; int n = N M; int count = 0; int sum = 0; while (m /= N) { count++; } while (n /= N) { sum = sum * N + (n % N); } return sum; } int main() { int N, M; int i = 0; int j = 0; scanf("%d %d", &N, &M); int sum,STEP = 0; int ret = M; while (hw(ret,N)) { int a = add(ret,N); ret = ret + a; int b = hw(ret,N); STEP ++; if (STEP > 30) break; } if (STEP > 30) printf("Impossible!"); else printf("%d", STEP); return 0; }
int hw(int ret,int N)
{
int sum = 0;
int n = N * ret;
while(n /= N)
{
sum = sum * N + (n % N);
}
if (sum == ret)
return 0;
else
return 1;
}
int add(int M,int N)
{
int m = N * M;
int n = N * M;
int count = 0;
int sum = 0;
while (m /= N)
{
count++;
}
while (n /= N)
{
sum = sum * N + (n % N);
}
return sum;
}
int main()
{
int N, M;
int i = 0;
int j = 0;
scanf("%d %d", &N, &M);
int sum,STEP = 0;
int ret = M;
while (hw(ret,N))
{
int a = add(ret,N);
ret = ret + a;
int b = hw(ret,N);
STEP ++;
if (STEP > 30)
break;
}
if (STEP > 30)
printf("Impossible!");
else
printf("%d", STEP);
return 0;
}
by ycyjx @ 2024-12-21 15:02:17
@wushang12138 你这不是高精度(这题数据虽然不大,但有30次翻转)
by ycyjx @ 2024-12-21 15:03:01
只需要写一个高精度加法
by ycyjx @ 2024-12-21 15:04:06
思路你应该和我差不多,我就不加注释了
#include<bits/stdc++.h>
using namespace std;
const int N = 200;
struct bign{
int jz,len,d[N];
bign() { jz=10; len=1; memset(d,0,sizeof(d)); }
bign(string s){
len = s.size();
for (int i=0;i<len;i++)
d[i] = s[len-1-i]-(isdigit(s[len-1-i])?'0':('A'-10));
}
bool ishw(){
for (int i=0;i<len/2;i++)
if (d[i]!=d[len-1-i])
return false;
return true;
}
void fanzhuan(){
for (int i=0;i<len/2;i++)
swap(d[i],d[len-1-i]);
}
bign operator + (bign b){
bign c;
c.jz = jz;
c.len = max(len,b.len);
for (int i=0;i<c.len;i++){
c.d[i] += d[i] + b.d[i];
c.d[i+1] += c.d[i]/c.jz;
c.d[i] %= c.jz;
}
if (c.d[c.len]) c.len++;
return c;
}
friend istream& operator >>(istream &input,bign &x){
string s;
input >> s;
x = s;
return input;
}
};
int n,step;
bign a,b;
int main(){
cin >> n;
cin >> a;
a.jz = n;
while (!a.ishw()){
b = a;
b.fanzhuan();
a = a+b;
step++;
if (step>30) {
cout <<"Impossible!\n";
return 0;
}
}
cout << "STEP=" << step << '\n';
return 0;
}
by ycyjx @ 2024-12-21 15:04:46
(求关注)
by wushang12138 @ 2024-12-23 22:29:06
@ycyjx感谢