欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodeForces - 385E Bear in the Field (矩阵快速幂)

程序员文章站 2022-03-16 18:44:52
...

CodeForces - 385E Bear in the Field (矩阵快速幂)
转移方程:
速度方程dx[i] dy[i]
//前一个位置的速度 + K(草莓数:(原始草莓:)sx[t-1] + sy[t-1]+(到t-1时刻增加的草莓) (t - 1))
dx[t] = dx[t-1] + sx[t-1] + sy[t-1] + t-1

dy[t] = dy[t-1] + sx[t-1] + sy[t-1] + t-1

位置方程sx[i] sy[i]
//前一个位置 + dx[t] (??)
sx[t] = sx[t-1] + dx[t-1] + sx[t-1] + sy[t-1] + t-1

sy[t] = sy[t-1] + dy[t-1] + sx[t-1] + sy[t-1] + t-1

CodeForces - 385E Bear in the Field (矩阵快速幂)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <ctime>
#define ll long long
using namespace std;

typedef vector<ll> vec;
typedef vector<vec> mat;
ll n, sx, sy, dx, dy, t;

mat mul(mat &A, mat &B) {
    mat C(A.size(), vec(B[0].size()));
    for (int i = 0; i < A.size(); i++) {
        for (int k = 0; k < B.size(); k++) {
            for (int j = 0; j < B[0].size(); j++) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % n;
                if(C[i][j] == 0) C[i][j] = n;
            }
        }
    }
    return C;
}

mat pow(mat A, ll n) {
    mat B(A.size(), vec(A.size()));
    for (int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while(n > 0) {
        if(n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

void solve() {
    mat A(6, vec(6));
    for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 6; j++) {
            if(i == j) A[i][j] = 1;
            else A[i][j] = 0;
        }
    }
    A[0][0] = A[1][1] = 2;
    A[0][1] = A[0][2] = A[0][4] = A[1][0] = A[1][3] = A[1][4] = A[2][0] = A[2][1] = A[2][2] = A[2][4] = 1;
    A[3][0] = A[3][1] = A[3][3] = A[3][4] = A[4][4] = A[4][5] = A[5][5] = 1;
    A = pow(A, t);
    mat B(6, vec(1));
    B[0][0] = sx, B[1][0] = sy, B[2][0] = dx, B[3][0] = dy, B[4][0] = 0, B[5][0] = 1;
    B = mul(A, B);
    if(B[0][0] < 0) B[0][0] += n;
    if(B[1][0] < 0) B[1][0] += n;
    printf("%lld %lld\n",B[0][0], B[1][0]);
}

int main() {
    scanf("%lld %lld %lld %lld %lld %lld", &n, &sx, &sy, &dx, &dy, &t);
    if(n == 1) {
        printf("1 1\n");
        return 0;
    }
    solve();
}
相关标签: 矩阵