萌新刚学OI,50pts求助
查看原帖
萌新刚学OI,50pts求助
576378
creation_hy楼主2022/10/23 23:06

看了眼题解,int已经全换longlong了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, last = 1;
struct Matrix
{
    ll r, c;
    ll a[5][5];
    inline Matrix()
    {
        memset(a, 0, sizeof(a));
    }
    inline Matrix(ll x)
    {
        r = c = x;
        memset(a, 0, sizeof(a));
        for (ll i = 1; i <= x; i++)
            a[i][i] = 1;
    }
    inline Matrix(ll x, ll y)
    {
        r = x;
        c = y;
        memset(a, 0, sizeof(a));
    }
    inline void init()
    {
        a[1][1] = last * 10 % m;
        a[1][2] = a[1][3] = a[2][3] = 0;
        a[2][1] = a[2][2] = a[3][1] = a[3][2] = a[3][3] = 1;
    }
    inline const Matrix operator*(const Matrix &x) const
    {
        Matrix res(r, x.c);
        for (ll i = 1; i <= res.r; i++)
            for (ll j = 1; j <= res.c; j++)
                for (ll k = 1; k <= c; k++)
                    res.a[i][j] = (res.a[i][j] + a[i][k] * x.a[k][j] % m) % m;
        return res;
    }
    inline const Matrix operator^(ll x) const
    {
        Matrix res(r), base = *this;
        while (x)
        {
            if (x & 1)
                res = res * base;
            base = base * base;
            x >>= 1;
        }
        return res;
    }
};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    Matrix b(3, 3), ans(3);
    for (ll i = 1; i <= (ll)log10(n); i++)
    {
        b.init();
        b = b ^ (last * 9);
        ans = ans * b;
        last = last * 10 % m;
    }
    b.init();
    b = b ^ (n - last + 1);
    ans = ans * b;
    cout << ans.a[3][1];
    return 0;
}
2022/10/23 23:06
加载中...