首页 > 其他分享 >hdu 1575(矩阵快速幂)

hdu 1575(矩阵快速幂)

时间:2023-06-28 23:33:48浏览次数:60  
标签:hdu Mat temp int res 矩阵 ++ ori 1575


题解:矩阵快速幂模板题。

#include <stdio.h>
#include <string.h>
const int N = 10;
struct Mat {
    int g[N][N];
}res, ori;
int n, k;

Mat multiply(Mat x, Mat y) {
    Mat temp;
    memset(temp.g, 0, sizeof(temp.g));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            for (int l = 0; l < n; l++)
                temp.g[i][j] = (temp.g[i][j] + x.g[i][l] * y.g[l][j]) % 9973;
    return temp;
}

void calc(int m) {
    while (m) {
        if (m & 1)
            res = multiply(res, ori);
        m >>= 1;
        ori = multiply(ori, ori);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        memset(res.g, 0, sizeof(res.g));
        memset(ori.g, 0, sizeof(ori.g));
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++) {
            res.g[i][i] = 1;
            for (int j = 0; j < n; j++)
                scanf("%d", &ori.g[i][j]);
        }
        calc(k);
        int ans = 0;
        for (int i = 0; i < n; i++)
            ans = (ans + res.g[i][i]) % 9973;
        printf("%d\n", ans);
    }
    return 0;
}


标签:hdu,Mat,temp,int,res,矩阵,++,ori,1575
From: https://blog.51cto.com/u_10729926/6577255

相关文章

  • hdu 5249(set + queue)
    题意:Input有大约100组数据。每组数据第一行有一个n(1≤n≤10000),代表服务记录数。接下来有n行,每一行有3种形式“inx”:代表重要值为x(0≤x≤109)的请求被推进管道。“out”:代表服务拉取了管道头部的请求。“query:代表我想知道当前管道内请求重要值的中间值.那就是......
  • poj 3233(矩阵快速幂)
    题意:给出一个矩阵A和数字k,要求出矩阵S=A+A^2+A^3+…+A^k。题解:首先A^x可以计算,然后需要折半计算,比如s(k)=(1+A^(k/2))*s(k/2),但k的奇偶不同需要分情况。#include<stdio.h>#include<string.h>constintN=35;structMat{intg[N][N];};intn,k,m......
  • hdu 5256(最长上升子序列)
    题意:我们有一个数列A1,A2…An,你现在要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。请输出最少需要修改多少个元素。题解:这是一个很机智的想法,每个数字和它的对应位置的差值存到数组s中,n-s序列的最长上升子序列就是解。#incl......
  • hdu 3117(矩阵快速幂)
    题意:求斐波那契序列的第n个数。如果超过8位,只输出前4位和后4位。题解:后4位比较好办,直接mod10000就可以了,前4位不知道怎么求,网上看到一个人写的很详细易懂,需要用到斐波那契通项公式,详细见→传送门#include<stdio.h>#include<math.h>#include<string.h>structMat{long......
  • uva 12470(矩阵快速幂)
    题意:公式f(n)=f(n-1)+f(n-2)+f(n-3),给出n,f(1)=0,f(2)=1,f(3)=2,要求得出f(n)。题解:普通的矩阵快速幂模板题。#include<stdio.h>#include<string.h>constintMOD=1000000009;structMat{longlongg[3][3];}ori,res;longlongn;Matmultiply(......
  • hdu 5254(暴力)
    题解:暴力所有点,直到不存在可以0变1的点。#include<stdio.h>#include<string.h>constintN=505;intn,m,k,g[N][N],temp[N][N],vis[N][N];booljudge(intx,inty){if(x-1>=0&&y-1>=0){if(temp[x-1][y]&&te......
  • hdu 2855(矩阵快速幂)
    题意:计算公式题解:想推出矩阵相乘的形式,想了很久也想不出,然后看别人的题解有一个高中就学过的式子:(1+x)^n=Cn0x^n+Cn1x^(n-1)+Cn2x^(n-2)+Cn3x^(n-3)+····+Cnn然后想到斐波那契矩阵是{1,1,1,0},1看做一个单位阵{1,0,1,0},就会做了。。。#include<stdio.h>#include......
  • hdu 1241(dfs)
    ProblemDescriptionTheGeoSurvCompgeologicsurveycompanyisresponsiblefordetectingundergroundoildeposits.GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdividesthelandintonumeroussquareplots.......
  • 2023-06-28《计算方法》- 陈丽娟 - 向量和矩阵基础.md
    2023-06-28《计算方法》-陈丽娟-向量和矩阵基础Matlab计算方法矩阵范数导数条件数本问补充向量和矩阵范数的相关知识,为下一章节的线性方程组的迭代法以及误差分析做准备。除了参考《计算方法》一书,还参考了华东师范大学数学学院的课程材料《迭代方法与预处理》以及陈新宇、伍......
  • 免费修复一加手机高通崩溃qualcomm crashdump mode
    qualcommcrashdumpmodequalcommcrashdumpmodequalcommcrashdumpmode高通崩溃高通崩溃高通崩溃希望崩溃的小朋友们,送修之前能搜到。。线刷下载,挨个刷。。国内找个网站比较恶心,下载要要两块钱。。这个免费。。。https://onepluscommunityserver.com/list/Unbrick_Too......