25分求助

P1015 [NOIP1999 普及组] 回文数

Marrelia @ 2022-12-22 09:14:18

只有#1 AC

#include <iostream>
using namespace std;

const int N = 309;
int a[N], b[N], n, len;
string s1;
int step = 1;

void add();
void reserve();
bool pd();

int main() {
    scanf("%d", &n);
    cin >> s1;
    for (int i = 0; i < s1.length(); ++i)
        if (s1[i] <= '9' && s1[i] >= '0') {
            a[i] = s1[i] - '0';
        }
        else {
            a[i] = s1[i] - 'A' + 10;
        }
    len = s1.length();
    /*for (int i = 0; i < len; ++i) {
        cout << a[i] << endl;
    }
    cout << len << endl;*/
    while (!pd()) {
        reserve();
        add();
        ++step;
    }
    if (step <= 30) {
        printf("STEP=%d\n", step);
    }
    else {
        printf("Impossible!\n");
    }
    return 0;
}

void add() {
    /*for (int i = 0; i < len; ++i) {
        cout << a[i];
    }
    cout << endl;
    for (int i = 0; i < len; ++i) {
        cout << b[i];
    }
    cout << endl << endl << step << " " << len << endl << endl << endl;*/
    for (int i = 0; i < len; ++i) {
        a[i] = a[i] + b[i];
    }
    for (int i = 0; i < len; ++i) {
        if (a[i] >= n) {
            a[i + 1]++, a[i] -= n;
            //        cout << a[i];
        }
    }
    if (a[len + 1] > 0) {
        ++len;
    }
}

void reserve() {
    for (int i = 0; i < len; ++i) {
        b[i] = a[len - i - 1];
    }
}

bool pd() {
    for (int p1 = 0, p2 = len - 1; p1 < p2; ++p1, --p2)
        if (a[p1] != a[p2]) {
            return false;
        }
    return true;
}

by __Luna__ @ 2022-12-22 14:09:48

试试这个:

#include<iostream>
#include<sstream>
#include<cmath>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
class very_long
{
    bool sign;
    int len;
    short* value;
public:
    very_long()
    {
        sign = 1;
        value = new short[13];
        len = 13;
        value[12] = 1;
        value[11] = 1;
        value[10] = 4;
        value[9] = 5;
        value[8] = 1;
        value[7] = 4;
        value[6] = 1;
        value[5] = 9;
        value[4] = 1;
        value[3] = 9;
        value[2] = 8;
        value[1] = 1;
        value[0] = 0;
    }
    very_long(const very_long& a)
    {
        sign = a.sign;
        len = a.len;
        if (len <= 0)value = { 0 };
        else {
            value = new short[len];
            for (int i = 0; i != len; i++)
            {
                value[i] = a.value[i];
            }
        }
    }
    very_long(string a)
    {
        string b = a;
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        if (b == string("inf") || b == string("Inf") || b == string("infinity") || b == string("Infinity"))
        {
            len = -1;
            value = { 0 };
            return;
        }
        if (b == string("nan") || b == string("NaN"))
        {
            len = -2;
            value = { 0 };
            return;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(const char a[])
    {
        string b = a;
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        if (b == string("inf") || b == string("Inf") || b == string("infinity") || b == string("Infinity"))
        {
            len = -1;
            value = { 0 };
            return;
        }
        if (b == string("nan") || b == string("NaN"))
        {
            len = -2;
            value = { 0 };
            return;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(unsigned int a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(unsigned long a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(unsigned long long a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(unsigned short a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(int a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(long a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(long long a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(short a)
    {
        string b = to_string(a);
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(float a)
    {
        double ccc = 1.0, ddd = 0.0;
        if (double(a) == double(ccc / ddd))
        {
            sign = 1;
            len = -1;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ccc / ddd)))
        {
            sign = 0;
            len = -1;
            value = { 0 };
            return;
        }
        if (double(a) == double(ddd / ddd))
        {
            sign = 1;
            len = -2;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ddd / ddd)))
        {
            sign = 0;
            len = -2;
            value = { 0 };
            return;
        }
        string b = to_string(a);
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(double a)
    {
        double ccc = 1.0, ddd = 0.0;
        if (double(a) == double(ccc / ddd))
        {
            sign = 1;
            len = -1;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ccc / ddd)))
        {
            sign = 0;
            len = -1;
            value = { 0 };
            return;
        }
        if (double(a) == double(ddd / ddd))
        {
            sign = 1;
            len = -2;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ddd / ddd)))
        {
            sign = 0;
            len = -2;
            value = { 0 };
            return;
        }
        string b = to_string(a);
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long(long double a)
    {
        double ccc = 1.0, ddd = 0.0;
        if (double(a) == double(ccc / ddd))
        {
            sign = 1;
            len = -1;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ccc / ddd)))
        {
            sign = 0;
            len = -1;
            value = { 0 };
            return;
        }
        if (double(a) == double(ddd / ddd))
        {
            sign = 1;
            len = -2;
            value = { 0 };
            return;
        }
        else if (double(a) == double(-(ddd / ddd)))
        {
            sign = 0;
            len = -2;
            value = { 0 };
            return;
        }
        string b = to_string(a);
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            sign = 0;
        }
        else
        {
            sign = 1;
        }
        len = b.size();
        value = new short[b.size()];
        for (int i = 0; i != len; i++)
        {
            value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
    }
    very_long& operator = (very_long a)
    {
        sign = a.sign;
        len = a.len;
        delete[]value;
        if (len <= 0)value = { 0 };
        else
        {
            value = new short[len];
            for (int i = 0; i != len; i++)
            {
                value[i] = a.value[i];
            }
        }
        return *this;
    }
    friend istream& operator>>(istream& in, very_long& a)
    {
        string b;
        in >> b;
        if (b.find(".") != -1)
            b = b.substr(0, b.find("."));
        if (b.substr(0, 1) == "-")
        {
            b = b.substr(1, b.size() - 1);
            a.sign = 0;
        }
        else
        {
            a.sign = 1;
        }
        if (b == string("inf") || b == string("Inf") || b == string("infinity") || b == string("Infinity"))
        {
            a.len = -1;
            a.value = { 0 };
            return in;
        }
        if (b == string("nan") || b == string("NaN"))
        {
            a.len = -2;
            a.value = { 0 };
            return in;
        }
        a.len = b.size();
        delete[]a.value;
        a.value = new short[b.size()];
        for (int i = 0; i != a.len; i++)
        {
            a.value[i] = short(b.substr(b.size() - 1, 1).c_str()[0] - '0');
            b = b.substr(0, b.size() - 1);
        }
        return in;
    }
    friend ostream& operator<<(ostream& out, const very_long& a)
    {
        if (a.len == -2)
        {
            out << "nan";
            return out;
        }
        out << ((a.sign == 0) ? "-" : "");
        if (a.len == -1)
        {
            out << "inf";
            return out;
        }
        for (int i = a.len - 1; i != -1; i--)
        {
            out << a.value[i];
        }
        return out;
    }
    ~very_long()
    {
        delete[]value;
    }
    operator const char* () const
    {
        string b;
        const char* a;
        if (len == -2)
        {
            b += "nan";
            a = b.c_str();
            return a;
        }
        b += ((sign == 0) ? "-" : "");
        if (len == -1)
        {
            b += "inf";
            a = b.c_str();
            return a;
        }
        for (int i = len - 1; i != -1; i--)
        {
            b += '0' + char(value[i]);
        }
        a = b.c_str();
        return a;
    }
    friend string to_string(very_long a)
    {
        string b;
        if (a.len == -2)
        {
            b += "nan";
            return b;
        }

        b += ((a.sign == 0) ? "-" : "");
        if (a.len == -1)
        {
            b += "inf";
            return b;
        }
        for (int i = a.len - 1; i != -1; i--)
        {
            b += '0' + char(a.value[i]);
        }
        return b;
    }
    operator char* () const
    {
        string b;
        if (len == -2)
        {
            b += "nan";
            char* a = new char[b.size() + 1];
            for (int i = 0; i != b.size() + 1; i++)
                a[i] = b.c_str()[i];
            return a;
        }
        b += ((sign == 0) ? "-" : "");
        if (len == -1)
        {
            b += "inf";
            char* a = new char[b.size() + 1];
            for (int i = 0; i != b.size() + 1; i++)
                a[i] = b.c_str()[i];
            return a;
        }
        for (int i = len - 1; i != -1; i--)
        {
            b += '0' + char(value[i]);
        }
        char* a = new char[b.size() + 1];
        for (int i = 0; i != b.size() + 1; i++)
            a[i] = b.c_str()[i];
        return a;
    }
    friend very_long operator +(const very_long& a, const very_long& b)
    {
        very_long c;
        if (a.len == -2)
        {
            return a;
        }
        if (b.len == -2)
        {
            return b;
        }
        if (a.len == -1 && b.len == -1 && a.sign != b.sign)
        {
            return very_long("nan");
        }
        if (a.len == -1)
        {
            return a;
        }
        if (b.len == -1)
        {
            return b;
        }
        if (a.sign == b.sign)
        {
            c.sign = a.sign;
            c.len = a.len > b.len ? a.len + 1 : b.len + 1;
            delete[]c.value;
            c.value = new short[c.len];
            int o = 0;
            for (int i = 0; i != c.len; i++)
            {
                c.value[i] = ((i >= a.len ? 0 : a.value[i]) + (i >= b.len ? 0 : b.value[i]) + o) % 10;
                o = ((i >= a.len ? 0 : a.value[i]) + (i >= b.len ? 0 : b.value[i]) + o) / 10;
            }
        }
        else
        {
            very_long aa, bb;
            aa = a;
            bb = b;
            if (a.len < b.len)
            {
                aa = b;
                bb = a;
            }
            else if (a.len == b.len)
            {
                for (int i = a.len - 1; i != -1; i--)
                {
                    if (a.value[i] < b.value[i])
                    {
                        aa = b;
                        bb = a;
                        break;
                    }
                    if (a.value[i] > b.value[i])
                    {
                        break;
                    }
                }
            }
            c = aa;
            int o = 0;
            for (int i = 0; i != c.len; i++)
            {
                c.value[i] -= (i >= bb.len ? 0 : bb.value[i]) + o;
                o = 0;
                if (c.value[i] < 0)
                {
                    c.value[i] += 10;
                    o = 1;
                }
            }
        }
        while (c.value[c.len - 1] == 0 && c.len != 1)
        {
            c.len = c.len - 1;
        }
        if (c.len == 1 && c.value[0] == 0)
        {
            c.sign = 1;
        }
        return c;
    }
    friend very_long operator +(const very_long& a, const int& b)
    {
        return a + very_long(b);
    }
    friend very_long operator +(const int& a, const very_long& b)
    {
        return very_long(a) + b;
    }
    friend very_long operator- (const very_long& a)
    {
        very_long b = a;
        b.sign = (b.sign + 1) % 2;
        return b;
    }
    friend very_long operator -(const very_long& a, const very_long& b)
    {
        very_long aa = a, bb = b;
        return aa + (-bb);
    }
    friend very_long operator -(const very_long& a, const int& b)
    {
        return a - very_long(b);
    }
    friend very_long operator -(const int& a, const very_long& b)
    {
        return very_long(a) - b;
    }
    friend bool operator >(const very_long& a, const very_long& b)
    {
        if (a.len == -2)
        {
            return false;
        }
        if (b.len == -2)
        {
            return false;
        }
        if (a.len == -1 && b.len == -1 && a.sign == b.sign)
            return false;
        if (a.len == -1)
        {
            if (a.sign == 1)
                return true;
            else
                return false;
        }
        if (b.len == -1)
        {
            if (b.sign == 0)
                return true;
            else
                return false;
        }
        if (a.sign == 0 && b.sign == 1)
            return false;
        if (a.sign == 1 && b.sign == 0)
            return true;
        if (a.sign == 0 && b.sign == 0)
        {
            very_long aa = a, bb = b;
            return (-aa) < (-bb);
        }
        if (a.len > b.len)
        {
            return true;
        }
        else if (a.len == b.len)
        {
            for (int i = a.len - 1; i != -1; i--)
            {
                if (a.value[i] > b.value[i])
                {
                    return true;
                }
                if (a.value[i] < b.value[i])
                {
                    return false;
                }
            }
        }
        return false;
    }
    friend bool operator >(const int& a, const very_long& b)
    {
        return very_long(a) > b;
    }
    friend bool operator >(const very_long& a, const int& b)
    {
        return  a > very_long(b);
    }
    friend bool operator <(const very_long& a, const very_long& b)
    {
        if (a.len == -2)
        {
            return false;
        }
        if (b.len == -2)
        {
            return false;
        }
        if (a.len == -1 && b.len == -1 && a.sign == b.sign)
            return false;
        if (a.len == -1)
        {
            if (a.sign == 0)
                return true;
            else
                return false;
        }
        if (b.len == -1)
        {
            if (b.sign == 1)
                return true;
            else
                return false;
        }
        if (a.sign == 0 && b.sign == 1)
            return true;
        if (a.sign == 1 && b.sign == 0)
            return false;
        if (a.sign == 0 && b.sign == 0)
        {
            very_long aa = a, bb = b;
            return (-aa) > (-bb);
        }
        if (a.len < b.len)
        {
            return true;
        }
        else if (a.len == b.len)
        {
            for (int i = a.len - 1; i != -1; i--)
            {
                if (a.value[i] < b.value[i])
                {
                    return true;
                }
                if (a.value[i] > b.value[i])
                {
                    return false;
                }
            }
        }
        return false;
    }
    friend bool operator <(const int& a, const very_long& b)
    {
        return very_long(a) < b;
    }
    friend bool operator <(const very_long& a, const int& b)
    {
        return  a < very_long(b);
    }
    friend bool operator ==(const very_long& a, const very_long& b)
    {
        if (a.len == -2)
        {
            return false;
        }
        if (b.len == -2)
        {
            return false;
        }
        if (a<b || a>b)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    friend very_long operator ==(const very_long& a, const int& b)
    {
        return a == very_long(b);
    }
    friend very_long operator ==(const int& a, const very_long& b)
    {
        return very_long(a) == b;
    }
    friend bool operator !=(const very_long& a, const very_long& b)
    {
        if (a.len == -2)
        {
            return true;
        }
        if (b.len == -2)
        {
            return true;
        }
        if (a<b || a>b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    friend very_long operator !=(const very_long& a, const int& b)
    {
        return a != very_long(b);
    }
    friend very_long operator !=(const int& a, const very_long& b)
    {
        return very_long(a) != b;
    }
    friend bool operator <=(const very_long& a, const very_long& b)
    {
        if (a < b || a == b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    friend very_long operator <=(const very_long& a, const int& b)
    {
        return a <= very_long(b);
    }
    friend very_long operator <=(const int& a, const very_long& b)
    {
        return very_long(a) <= b;
    }
    friend bool operator >=(const very_long& a, const very_long& b)
    {
        if (a > b || a == b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    friend very_long operator >=(const very_long& a, const int& b)
    {
        return a >= very_long(b);
    }
    friend very_long operator >=(const int& a, const very_long& b)
    {
        return very_long(a) >= b;
    }
    very_long& operator+=(very_long a)
    {
        *this = *this + a;
        return *this;
    }
    very_long& operator-=(very_long a)
    {
        *this = *this - a;
        return *this;
    }
    friend very_long operator *(const very_long& a, const very_long& b)
    {
        very_long c;
        if (a.len == -2)
        {
            return a;
        }
        if (b.len == -2)
        {
            return b;
        }
        if (a.sign == b.sign)
        {
            c.sign = 1;
        }
        else
        {
            c.sign = 0;
        }
        if (a.len == -1 || b.len == -1)
        {
            c.len = -1;
            delete[]c.value;
            c.value = { 0 };
            if (a == very_long(0) || b == very_long(0))
                c = 0;
            return c;
        }
        c.len = a.len + b.len;
        delete[]c.value;
        c.value = new short[c.len];
        for (int i = 0; i != c.len; i++)
        {
            c.value[i] = 0;
        }
        for (int i = 0; i != a.len; i++)
        {
            for (int j = 0; j != b.len; j++)
            {
                c.value[i + j] += a.value[i] * b.value[j];
                c.value[i + j + 1] += c.value[i + j] / 10;
                c.value[i + j] %= 10;
            }
        }
        while (c.value[c.len - 1] == 0 && c.len != 1)
        {
            c.len = c.len - 1;
        }
        if (c.len == 1 && c.value[0] == 0)
        {
            c.sign = 1;
        }
        return c;
    }
    very_long& operator*=(very_long a)
    {
        *this = *this * a;
        return *this;
    }
    friend very_long operator /(const very_long& a, const very_long& b)
    {
        very_long c, aa = a, bb = b, cc;
        if (a.len == -2)
        {
            return a;
        }
        if (b.len == -2)
        {
            return b;
        }
        if (b == very_long(0))
        {
            if (a == very_long(0))return very_long("nan");
            int x = 1, y = 0;
            cout << x / y;
        }
        if (a.len == -1)
        {
            if (b > very_long(0))
                return a;
            else
                return -a;
        }
        if (b.len == -1)
        {
            return very_long(0);
        }
        if (a == b)
        {
            c.sign = 1;
            c.len = 1;
            delete[]c.value;
            c.value = new short[1];
            c.value[0] = 1;
            return c;
        }
        if (a.sign == b.sign)
        {
            c.sign = 1;
        }
        else
        {
            c.sign = 0;
        }
        aa.sign = 1;
        bb.sign = 1;
        if (aa < bb)
        {
            c.sign = 1;
            c.len = 1;
            delete[]c.value;
            c.value = new short[1];
            c.value[0] = 0;
            return c;
        }
        delete[]c.value;
        c.value = new short[aa.len - bb.len + 1];
        c.len = aa.len - bb.len + 1;
        for (int i = 0; i != c.len; i++)
        {
            c.value[i] = 0;
        }
        for (int i = aa.len - bb.len, k = aa.len; i != -1; i--)
        {
            delete[]cc.value;
            cc.len = k;
            cc.value = new short[cc.len];
            for (int j = 0; j != cc.len; j++)
            {
                cc.value[j] = 0;
            }
            for (int j = 0; j != bb.len; j++)
            {
                cc.value[j + i] = bb.value[j];
            }
            while (aa >= cc)
            {
                aa -= cc;
                c.value[i]++;
            }
            k--;
        }
        while (c.value[c.len - 1] == 0 && c.len != 1)
        {
            c.len = c.len - 1;
        }
        if (c.len == 1 && c.value[0] == 0)
        {
            c.sign = 1;
        }
        return c;
    }
    friend very_long operator %(const very_long& a, const very_long& b)
    {
        very_long c, aa;
        if (a.len == -2)
        {
            return a;
        }
        if (b.len == -2)
        {
            return b;
        }
        aa = a;
        c = (a / b) * b;
        c = aa - c;
        return c;
    }
    very_long& operator%=(very_long a)
    {
        *this = *this % a;
        return *this;
    }
    very_long& operator/=(very_long a)
    {
        *this = *this / a;
        return *this;
    }
    very_long& operator++()
    {
        *this += 1;
        return *this;
    }
    very_long& operator--()
    {
        *this -= 1;
        return *this;
    }
    very_long operator++(int)
    {
        very_long c = *this;
        *this += 1;
        return c;
    }
    very_long operator--(int)
    {
        very_long c = *this;
        *this -= 1;
        return c;
    }
};
const very_long Inf = "inf", NaN = "nan";
map<string, very_long>htod;
map<very_long, string>dtoh;
very_long plus_(very_long a, very_long b, very_long c)
{
    very_long d = 0, jin = 0, j = 16;
    for (very_long i, k; (a != very_long(0) || b != very_long(0)) || jin != very_long(0); j *= 16)
    {
        i = a % 16 + b % 16;
        k = (jin + i);
        d += (k % c) * (j / 16);
        jin = k / c;
        a /= 16;
        b /= 16;
    }
    return d;
}
very_long fromh(string a)
{
    very_long b = 0, j = 1;
    for (int i = a.size() - 1; i >= 0; i--, j *= 16)
    {
        b += htod[a.substr(i, 1)] * j;
    }
    return b;
}
string toh(very_long a)
{
    string b = "";
    for (; a != very_long(0); a /= 16)
    {
        b = dtoh[a % 16] + b;
    }
    return b;
}
int main()
{
    htod["0"] = 0;
    htod["1"] = 1;
    htod["2"] = 2;
    htod["3"] = 3;
    htod["4"] = 4;
    htod["5"] = 5;
    htod["6"] = 6;
    htod["7"] = 7;
    htod["8"] = 8;
    htod["9"] = 9;
    htod["A"] = 10;
    htod["B"] = 11;
    htod["C"] = 12;
    htod["D"] = 13;
    htod["E"] = 14;
    htod["F"] = 15;
    dtoh[0] = "0";
    dtoh[1] = "1";
    dtoh[2] = "2";
    dtoh[3] = "3";
    dtoh[4] = "4";
    dtoh[5] = "5";
    dtoh[6] = "6";
    dtoh[7] = "7";
    dtoh[8] = "8";
    dtoh[9] = "9";
    dtoh[10] = "A";
    dtoh[11] = "B";
    dtoh[12] = "C";
    dtoh[13] = "D";
    dtoh[14] = "E";
    dtoh[15] = "F";
    string a, b;
    very_long c;
    cin >> c >> a;
    for (int i = 0; i <= 30; i++)
    {
        b = a;
        reverse(b.begin(), b.end());
        if (b == a)
        {
            cout << "STEP=" << i;
            return 0;
        }
        a = toh(plus_(fromh(a), fromh(b), c));
    }
    cout << "Impossible!";
    return 0;
}

(p.s. 看着有点长?这是因为我套用了一个自己之前写的高精度类。主要是懒得写高精)


by zzz99947937 @ 2023-01-17 17:49:32

@GodForever 您可真刑


by www101 @ 2023-03-10 15:55:01

666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666


|