zoukankan      html  css  js  c++  java
  • BZOJ 2875: [Noi2012]随机数生成器( 矩阵快速幂 )

    矩阵快速幂...+快速乘就OK了 

    --------------------------------------------------------------------------------------

    #include<bits/stdc++.h>
     
    using namespace std;
     
    typedef long long ll;
     
    ll MOD, a, c, x, n, g;
     
    ll MUL(ll a, ll b) {
    ll ans = 0;
    for(; b; b >>= 1) {
    if(b & 1) ans += a;
    if(ans >= MOD) ans -= MOD;
    a <<= 1;
    if(a >= MOD) a -= MOD;
    }
    return ans;
    }
     
    struct matrix {
    ll x[2][2];
    matrix() {
    memset(x, 0, sizeof x);
    }
    inline void unit() {
    x[0][0] = x[1][1] = 1;
    x[0][1] = x[1][0] = 0;
    }
    matrix operator * (const matrix &o) {
    matrix ans;
    for(int i = 0; i < 2; i++)
       for(int j = 0; j < 2; j++)
           for(int k = 0; k < 2; k++)
               (ans.x[i][j] += MUL(x[i][k], o.x[k][j])) %= MOD;
    return ans;
    }
    matrix operator = (const matrix &o) {
    memcpy(x, o.x, sizeof x);
    return *this;
    }
    matrix operator ^ (ll k) {
    matrix ans, p = *this; ans.unit();
    for(; k; k >>= 1) {
    if(k & 1) ans = ans * p;
    p = p * p;
    }
    return ans;
    }
    } Q;
     
    int main() {
    cin >> MOD >> a >> c >> x >> n >> g;
    Q.x[0][0] = a; Q.x[0][1] = 0; Q.x[1][0] = c; Q.x[1][1] = 1;
    matrix ans = Q ^ n;
    cout << (MUL(x, ans.x[0][0]) + ans.x[1][0]) % MOD % g << " ";
    return 0;
    }

    --------------------------------------------------------------------------------------

    2875: [Noi2012]随机数生成器

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 1289  Solved: 731
    [Submit][Status][Discuss]

    Description

    Input

    包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数。

    Output

    输出一个数,即Xn mod g

    Sample Input


    11 8 7 1 5 3


    Sample Output

    2

    HINT

    Source

  • 相关阅读:
    python获取公网ip,本地ip及所在国家城市等相关信息收藏
    Tkinter的下拉列表Combobox
    pyinstaller打包pyqt文件(转)
    通过pyqt5实现俄罗斯方块游戏例子
    pygame游戏开发入门例子
    python界面Tkinter编程(tkMessageBox对话框使用)
    python tkinter-菜单栏
    python tkinter-容器、子窗体
    HUNNU--湖师大--11409--Skill
    [置顶] 博客搬迁到新地址。
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4725747.html
Copyright © 2011-2022 走看看