为何UB
查看原帖
为何UB
549131
Eous楼主2024/12/16 16:03
#include<bits/extc++.h>
using namespace std;
const int mod = 10;
int n,k;
struct modint
{
    int val;
    modint(int x = 0):val(x % mod){};
    int &operator()(){return (val %= mod);}
    friend modint operator+(modint x,modint y){return ((x() + y()) % mod);}
    friend modint operator+=(modint &x,modint y){return x() = (x() + y()) % mod;}
    friend modint operator*(modint x,modint y){return (x() * y() % mod);}//
    friend modint operator*=(modint &x,modint y){return x() = (x() * y()) % mod;}//在这个operator里
}x;
struct mat1
{
    modint a[40][40];
    mat1(int x = 0)
    {
        memset(a,0,sizeof a);
        for (int i = 0; i < 40; i++)
            a[i][i] = x;
    }
    modint *operator[](int x){return a[x];}
    friend mat1 operator*(mat1 x,modint y)
    {
        for (int i = 1; i <= 40; i++)
            for (int j = 1; j <= 40; j++)
                x.a[i][j] *= y;//编译器说这里UB
        return x;
    }
    friend mat1 operator*(mat1 x,mat1 y)
    {
        mat1 ret;
        for (int i = 1; i <= 2; i++)
            for (int j = 1; j <= 2; j++)
                for (int k = 1; k <= 2; k++)
                    ret[i][j] += x[i][k] * y[k][j];
        return ret;
    }
}base;
struct mat2
{
    mat1 a[3];
    mat1 &operator[](int x){return a[x];}
    friend mat2 operator*(mat2 x,mat1 y)
    {
        mat2 ret;
        ret[1] = x[1] * (y[1][1] + y[2][1]);
        ret[2] = x[2] * (y[1][2] + y[2][2]);
        return ret;
    }
}origin;
template<typename type>
type binpow(type x,int y)
{
    type ret(1);
    while (y)
    {
        if (y & 1)
            ret = ret * x;
        x = x * x;
        y >>= 1;
    }
    return ret;
}
signed main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> origin[2][i][j]();
    base[1][1] = base[1][2] = base[2][2] = 1;
    base = binpow(base,k);
    origin = origin * base;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cout << origin[1][i][j]();
            if (j != n)
                cout << " ";
        }
        if (i != n)
            cout << endl;
    }
    return 0;
}
2024/12/16 16:03
加载中...