30 pts TLE 求助
查看原帖
30 pts TLE 求助
688783
SilverLi楼主2023/10/27 19:22
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
// 高精模板
struct __int
{
    string s;
    int sz, f;
    __int()
    {
        s = "0", sz = 1, f = 1;
    }
}t0, t1, t2, t10, mod;
__int empty()
{
    __int x;
    x.s = "", x.sz = 0;
    return x;
}
__int tval(string str)
{
    __int x;
    if (str[0] == '-')
        x.f = -1, str = str.substr(1);
    x.s = str, x.sz = str.size();
    return x;
}
__int tval(int x)
{
    __int ans;
    if (x == 0)
        return ans;
    ans = empty();
    if (x < 0)
        ans.f = -1, x = -x;
    while (x)
        ans.s = char(x % 10 + '0') + ans.s, ans.sz++, x /= 10;
    return ans;
}
__int abs(__int x)
{
    x.f = 1;
    return x;
}
__int opp(__int x)
{
    x.f = -x.f;
    return x;
}
istream& operator>>(istream& cin_, __int& x)
{
    x = empty();
    char ch = getchar();
    while (ch < '0' || ch>'9')
        x.f = (ch == '-' ? -1 : 1), ch = getchar();
    while (ch >= '0' && ch <= '9')
        x.s += ch, x.sz++, ch = getchar();
    return cin_;
}
ostream& operator<<(ostream& cout_, __int x)
{
    if (x.f == -1)
        putchar('-');
    for (int i = 0; i < x.sz; i++)
        putchar(x.s[i]);
    return cout_;
}
bool operator<(__int x, __int y)
{
    if (y.f == -1 && x.f == -1)
        return y < x;
    if (x.f == 1 && y.f == -1)
        return false;
    if (x.f == -1 && y.f == 1)
        return true;
    return x.sz < y.sz || (x.sz == y.sz && x.s < y.s);
}
bool operator>(__int x, __int y)
{
    return y < x;
}
bool operator>=(__int x, __int y)
{
    return !(y < x);
}
bool operator<=(__int x, __int y)
{
    return !(x > y);
}
bool operator==(__int x, __int y)
{
    return (x >= y) && (y <= x);
}
bool operator!=(__int x, __int y)
{
    return !(x == y);
}
__int operator+(__int x, __int y)
{
    if (x.f == -1 && y.f == -1)
    {
        x.f = y.f = 1;
        return opp(x + y);
    }
    if (x.f == -1 && y.f == 1)
        return y + x;
    string a = x.s, b = y.s, res = "";
    int sa = x.sz, sb = y.sz;
    if (x.f == 1 && y.f == -1)
    {
        if (x < abs(y))
        {
            x.f = -1, y.f = 1;
            __int ans = y + x;
            ans.f = -1;
            return ans;
        }
        for (int i = sb; i < sa; i++)
            b = '0' + b;
        int j = 0;
        for (int i = sa - 1; i >= 0; i--)
        {
            int t = a[i] - b[i] - j;
            j = (t < 0), t = (t + 10) % 10;
            res = char(t + '0') + res;
        }
        while (res.size() > 1 && res[0] == '0')
            res = res.substr(1);
        __int ans;
        ans.s = res, ans.sz = res.size(), ans.f = 1;
        return ans;
    }
    if (sa > sb)
        swap(a, b), swap(sa, sb);
    __int ans;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for (int i = sa; i < sb; i++)
        a += '0';
    int j = 0;
    for (int i = 0; i < sb; i++)
    {
        int t = a[i] + b[i] - 2 * '0' + j;
        j = t / 10, t %= 10, res += char(t + '0');
    }
    if (j)
        res += char(j + '0');
    reverse(res.begin(), res.end());
    ans.s = res, ans.sz = res.size();
    return ans;
}
void operator+=(__int& x, __int y)
{
    x = x + y;
}
__int operator-(__int x, __int y)
{
    y.f = -y.f;
    return x + y;
}
void operator-=(__int& x, __int y)
{
    x = x - y;
}
__int operator*(__int x, __int y)
{
    if (x.f != y.f)
    {
        x.f = y.f;
        return opp(x * y);
    }
    string a = x.s, b = y.s;
    int sa = x.sz, sb = y.sz;
    if (a == "0" || b == "0")
        return tval(0);
    __int ans;
    for (int i = sb - 1; i >= 0; i--)
    {
        string m;
        int in = 0;
        for (int j = sa - 1; j >= 0; j--)
        {
            int t = (a[j] - '0') * (b[i] - '0') + in;
            in = t / 10, t %= 10;
            char c = t + '0', l = in + '0';
            m = c + m;
            if (j == 0 && l > '0') m = l + m;
        }
        for (int j = 1; j <= sb - 1 - i; j++)
            m += '0';
        ans += tval(m);
    }
    return ans;
}
void operator*=(__int& x, __int y)
{
    x = x * y;
}

__int operator/(__int x, __int y)
{
    if (x.f != y.f)
    {
        x.f = y.f;
        return opp(x / y);
    }
    x.f = y.f = 1;
    if (y > x)
        return tval(0);
    int cur = 0;
    __int tmp = empty(), ans = empty();
    tmp.s = x.s[0], tmp.sz++;
    while (cur < x.sz)
    {
        int res = 9;
        while (y * tval(res) > tmp)res--;
        ans.s += char(res + '0'), ans.sz++;
        tmp -= y * tval(res);
        if (tmp.s == "0")
            tmp.s = "", tmp.sz = 0;
        tmp.s += x.s[++cur], tmp.sz++;
    }
    while (ans.sz > 1 && ans.s[0] == '0')
        ans.s = ans.s.substr(1), ans.sz--;
    return ans;
}
void operator/=(__int& x, __int y)
{
    x = x / y;
}
__int operator%(__int x, __int y)
{
    return x - ((x / y) * y);
}
void operator%=(__int& x, __int y)
{
    x = x % y;
}
void operator++(__int& x)
{
    x = x + tval(1);
}
void operator--(__int& x)
{
    x = x - tval(1);
}
__int pow(__int a, __int b)
{
    __int res = tval(1);
    while (b > t0)
        res = res * (b % t2 == t1 ? a : tval(1)) % mod, a = a * a % mod, b /= t2;
    return res;
}
__int pow(__int a, int b)
{
    __int res = tval(1);
    while (b)
        res = res * (b & 1 ? a : tval(1)) % mod, a = a * a % mod, b >>= 1;
    return res;
}
__int fac(int n)
{
    __int res = tval(1);
    for (int i = 1; i <= n; i++)
        res *= tval(i);
    return res;
}
// 高精模板 __int
constexpr int N = 1e2 + 5;
int k;
__int n, s, ans;
signed main() {
    t0.s = "0"; t1.s = "1"; t2.s = "2";
    t10.s = "10"; t10.sz = 2;
    cin >> n >> k;
    // if (n.s == "1232135") {
    //     cout << -1;
    //     return 0;
    // }
    ans.s = "1";
    __int m;
    m.s = "10"; m.sz = 2;
    s = n % pow(m, k);
    __int tmp = m, now, each, last;
    for (int i = 1; i <= k; ++i) {
        mod.sz = i + 1;
        string t = "";
        for (int j = 1; j <= i; ++j)
            t += '0';
        mod.s = '1' + t;
        now = s % mod;
        last = now;
        each = pow(now, ans) % mod;
        now *= each;
        now %= mod;
        __int cnt;
        cnt.s = "1";
        // cerr << "CNT ";
        while (last != now) {
            now *= each;
            now %= mod;
            ++cnt;
            if (cnt > t10)   break;
            // cerr << cnt << ' ';
        }
        if (cnt > t10) {
            cout << -1;
            return 0;
        }
        // cerr << '\n';
        // cerr << i << ' ' << mod << ' ' << last << ' ' << each << ' ' << cnt << '\n';
        ans *= cnt;
    }
    cout << ans;
    return 0;
}
2023/10/27 19:22
加载中...