{题解}[jzoj3366]【NOI2012】随机数生成器

传送门

Analysis

数据范围中提示了我们
{题解}[jzoj3366]【NOI2012】随机数生成器_第1张图片
应用fermat小定理可以拿取不少(部分)
所以我们辛勤地跑去退O(1)式子,随即发现:
这™是一道矩阵乘法模板题。
既然是裸题,那就这样喽…
按照题目的式子,再套个快速幂,再套个黑科技 了事

Code

#include
#include
#include
#include 
using namespace std;
struct Matrix{
    long long n,m;
    long long value[4][4];
    Matrix()
    {
        memset(value,0,sizeof value);
    }
};
long long n;
long long Mo,A,C,Xo,N,G;
Matrix a,b;
long long mult(long long A,long long B)
{
    long long z = 0;
    if (B == 0) return z;
    z = mult(A,B >> 1);
    z = (z << 1) % Mo;
    if (B & 1) z = (z + A) % Mo;
    return z; 
}
Matrix Mul(Matrix a,Matrix b) 
{
    Matrix ans = Matrix();
    for (long long i = 1;i <= a.n;i ++)
        for (long long j = 1;j <= b.m;j ++) 
            for (long long k = 1;k <= a.m;k ++)
                ans.value[i][j] = (ans.value[i][j] + mult(a.value[i][k],b.value[k][j])) % Mo;
    ans.n = a.n,ans.m = b.m;
    return ans;
}
Matrix MatrQsm(Matrix a,long long e)
{
    if (e == 1) return a;
    Matrix tmp = MatrQsm(a,e >> 1);
    tmp = Mul(tmp,tmp);
    if (e & 1) tmp = Mul(tmp,a);
    return tmp;
}

int main()
{
    //freopen("D:/LiuYuanHao/3366.in","r",stdin);
    scanf("%lld%lld%lld%lld%lld%lld", &Mo, &A, &C, &Xo, &N, &G);
    b = Matrix();
    b.n = 2,b.m = 2;
    b.value[1][1] = A,b.value[2][1] = C,b.value[2][2] = 1;
    //友矩阵 
    a.n = 1,a.m = 2;
    a.value[1][1] = Xo,a.value[1][2] = 1;
    //初始矩阵 
    Matrix c = Matrix(); 
    c = MatrQsm(b,N); 
    c = Mul(a,c);
    //答案矩阵 
    printf("%lld", c.value[1][1] % G);
}

你可能感兴趣的:({题解}[jzoj3366]【NOI2012】随机数生成器)