• ZOJ 2671 Cryptography(线段树+求区间矩阵乘积)


    题意:已知n个矩阵(下标从1开始),求下标x~y区间矩阵的乘积。最多m次询问,n ( 1 <= n <= 30,000) and m ( 1 <= m <= 30,000)。

    分析:

    1、矩阵初始化为单位矩阵,因为要做乘积,E*A=A。

    2、因为输出矩阵的所有值范围在0~r-1,所以要对r取余。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    typedef long long ll;
    typedef unsigned long long llu;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
    const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e4;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 30000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    struct Matrix{
        int ma[2][2];
        Matrix(){
            ma[0][0] = ma[1][1] = 1;
            ma[0][1] = ma[1][0] = 0;
        }
    }num[MAXN << 2];
    int r;
    Matrix mul(Matrix a, Matrix b){
        Matrix ans;
        for(int i = 0; i < 2; ++i){
            for(int j = 0; j < 2; ++j){
                ans.ma[i][j] = 0;
                for(int k = 0; k < 2; ++k){
                    (ans.ma[i][j] += (a.ma[i][k] * b.ma[k][j]) % r) %= r;
                }
            }
        }
        return ans;
    }
    void add_up(int id){
        num[id] = mul(num[id << 1], num[id << 1 | 1]);
    }
    void add(int l, int r, int cur, Matrix x, int id){
        if(l == r){
            num[id] = x;
            return;
        }
        int mid = (l + r) >> 1;
        if(cur <= mid){
            add(l, mid, cur, x, id << 1);
        }
        else add(mid + 1, r, cur, x, id << 1 | 1);
        add_up(id);
    }
    Matrix query(int L, int R, int l, int r, int id){
        if(l >= L && r <= R){
            return num[id];
        }
        int mid = (l + r) >> 1;
        Matrix ans;
        if(L <= mid){
            ans = mul(ans, query(L, R, l, mid, id << 1));
        }
        if(R >= mid + 1){
            ans = mul(ans, query(L, R, mid + 1, r, id << 1 | 1));
        }
        return ans;
    }
    int main(){
        int n, m;
        bool flag = true;
        while(scanf("%d%d%d", &r, &n, &m) == 3){
            if(flag) flag = false;
            else printf("\n");
            for(int i = 1; i <= n; ++i){
                Matrix tmp;
                for(int j = 0; j < 2; ++j){
                    for(int k = 0; k < 2; ++k){
                        scanf("%d", &tmp.ma[j][k]);
                    }
                }
                add(1, n, i, tmp, 1);
            }
            while(m--){
                int x, y;
                scanf("%d%d", &x, &y);
                Matrix t = query(x, y, 1, n, 1);
                printf("%d %d\n", t.ma[0][0], t.ma[0][1]);
                printf("%d %d\n", t.ma[1][0], t.ma[1][1]);
                if(m) printf("\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    初探并行线程的三种写法
    Revit2017二次开发-打印机配置
    test错误记录
    自行车出租系统错误集2
    错误记录-spring+mybatis
    Windows(64位IIS)未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
    sqlDeveloper连接oracle
    Apriori算法-java
    Apriori算法-位运算-C语言
    Apriori算法-数组-C语言
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6358462.html
Copyright © 2020-2023  润新知