111

咲ら  •  1个月前


#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

typedef long long ll;
const ll MOD = 99999999;


vector<vector<ll>> multiply(const vector<vector<ll>>& a, const vector<vector<ll>>& b) {
    int n = a.size();
    int m = b[0].size();
    int p = b.size();
    vector<vector<ll>> res(n, vector<ll>(m, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int k = 0; k < p; ++k) {
                res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % MOD;
            }
        }
    }
    return res;
}

vector<vector<ll>> matrix_pow(vector<vector<ll>> a, ll power) {
    int n = a.size();
    vector<vector<ll>> res(n, vector<ll>(n, 0));
    for (int i = 0; i < n; ++i) {
        res[i][i] = 1;
    }
    while (power > 0) {
        if (power % 2 == 1) {
            res = multiply(res, a);
        }
        a = multiply(a, a);
        power /= 2;
    }
    return res;
}

int main() {
    ll n;
    cin >> n;
    
    if (n == 1) {
        cout << 2 << endl << 3 << endl;
        return 0;
    } else if (n == 2) {
        cout << 1 << endl << 4 << endl;
        return 0;
    } else if (n == 3) {
        cout << 6 << endl << 5 << endl;
        return 0;
    }
    vector<vector<ll>> transform(8, vector<ll>(8, 0));
    transform[0][1] = 1; 
    transform[0][4] = 2; 
    transform[0][6] = 1; 
    
    transform[1][0] = 1; 
    transform[1][4] = 3; 
    transform[1][5] = 2; 
    transform[1][7] = 1; 
    
    transform[2][0] = 1; 
    transform[3][1] = 1;
    transform[4][2] = 1; 
    transform[5][3] = 1; 
    transform[6][6] = 1; 
    transform[7][7] = 1; 
    
    ll power = n - 3;
    vector<vector<ll>> mat_pow = matrix_pow(transform, power);
    vector<ll> initial = {6, 5, 1, 4, 2, 3, 5, 3};
    vector<ll> res(8, 0);
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            res[i] = (res[i] + mat_pow[i][j] * initial[j]) % MOD;
        }
    }
    
    cout << res[0] << endl << res[1] << endl;
    
    return 0;
}

include

include

include

using namespace std;

typedef long long ll; const ll MOD = 99999999;

vector<vector> multiply(const vector<vector>& a, const vector<vector>& b) {

int n = a.size();
int m = b[0].size();
int p = b.size();
vector<vector<ll>> res(n, vector<ll>(m, 0));
for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        for (int k = 0; k < p; ++k) {
            res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % MOD;
        }
    }
}
return res;

}

vector<vector> matrix_pow(vector<vector> a, ll power) {

int n = a.size();
vector<vector<ll>> res(n, vector<ll>(n, 0));
for (int i = 0; i < n; ++i) {
    res[i][i] = 1;
}
while (power > 0) {
    if (power % 2 == 1) {
        res = multiply(res, a);
    }
    a = multiply(a, a);
    power /= 2;
}
return res;

}

int main() {

ll n;
cin >> n;

if (n == 1) {
    cout << 2 << endl << 3 << endl;
    return 0;
} else if (n == 2) {
    cout << 1 << endl << 4 << endl;
    return 0;
} else if (n == 3) {
    cout << 6 << endl << 5 << endl;
    return 0;
}
vector<vector<ll>> transform(8, vector<ll>(8, 0));
transform[0][1] = 1; 
transform[0][4] = 2; 
transform[0][6] = 1; 

transform[1][0] = 1; 
transform[1][4] = 3; 
transform[1][5] = 2; 
transform[1][7] = 1; 

transform[2][0] = 1; 
transform[3][1] = 1;
transform[4][2] = 1; 
transform[5][3] = 1; 
transform[6][6] = 1; 
transform[7][7] = 1; 

ll power = n - 3;
vector<vector<ll>> mat_pow = matrix_pow(transform, power);
vector<ll> initial = {6, 5, 1, 4, 2, 3, 5, 3};
vector<ll> res(8, 0);
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        res[i] = (res[i] + mat_pow[i][j] * initial[j]) % MOD;
    }
}

cout << res[0] << endl << res[1] << endl;

return 0;

}



评论:

请先登录,才能进行评论