hdu4920——Matrix multiplication(矩阵快速幂or循环外提)

本文介绍了一种解决大规模矩阵乘法问题的方法,利用矩阵快速幂技巧减少计算复杂度,避免了传统方法中的超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matrix multiplication

                                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
Given two matrices A and B of size n×n, find the product of them.
bobo hates big integers. So you are only asked to find the result modulo 3.
Input
The input consists of several tests. For each tests:
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
Output
For each tests:
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Sample Input
1 0 1 2 0 1 2 3 4 5 6 7
Sample Output
0 0 1 2 1

题意:给出两个n*n的矩阵,求这两个矩阵的乘积,结果对3取余。
奇妙的题,循环外提巧妙AC
相关博客:http://blog.youkuaiyun.com/gogdizzy/article/details/9003369
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 805;
int a[N][N], b[N][N], ans[N][N];
int main()
{
    int n, i, j, k;
    while(~scanf("%d",&n))
    {
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
            {
                scanf("%d",&a[i][j]);
                a[i][j] %= 3;
            }
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
            {
                scanf("%d",&b[i][j]);
                b[i][j] %= 3;
            }
        memset(ans, 0, sizeof(ans));
        for(k = 1; k <= n; k++) //经典算法中这层循环在最内层,放最内层会超时,但是放在最外层或者中间都不会超时,不知道为什么
            for(i = 1; i <= n; i++)
                for(j = 1; j <= n; j++)
                {
                    ans[i][j] += a[i][k] * b[k][j];
                    //ans[i][j] %= 3;   //如果在这里对3取余,就超时了
                }
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j < n; j++)
                printf("%d ", ans[i][j] % 3);
            printf("%d\n", ans[i][n] % 3);
        }
    }
    return 0;
}
另外:

解题思路:如果按照先求矩阵C 再求C的(N*N)肯定会超时,因为C有N行N列,最多为1000*1000*1000*log2(1000000)。但是我们可以用到一个矩阵的性质。矩阵虽然不满足交换律但是他满足结合律。这边C是A和B的矩阵乘积。C的N*N次方相当于(A*B)^(N*N) = A*B*A*B...*A*B 总共N*N个。即 原式  = A * (B*A)*(B*A)...(B*A)*B 总共有N*N-1个的B*A。然后我们就可以根据B*A来算了。这边K最多是6 所以算中间这一部分的值为6*6*6*log2(1000000-1).这样,我们使用矩阵快速幂,就不会超时了。

由于结构体里开不了1000*1000的大小,所以只开了6*6的矩阵。剩下的用普通的矩阵乘法计算。

#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
using namespace std;
struct matrix
{
    int ma[15][15];
    int row,col;
};


matrix operator *(matrix a,matrix b)
{
    matrix w;
    memset(w.ma,0,sizeof(w.ma));
    for (int i=0; i<a.row; i++)
        for (int j=0; j<b.col; j++)
            for (int h=0; h<a.col; h++)
                w.ma[i][j]=(w.ma[i][j]+a.ma[i][h]*b.ma[h][j])%6;
    w.row=a.row;
    w.col=b.col;
    return w;
}
matrix operator ^(matrix a,__int64 b)
{
    matrix w;
    for (int i=0; i<6; i++) for (int j=0; j<6; j++) w.ma[i][j]=(i==j);
    w.row=6;
    w.col=6;
    while (b)
    {
        if (b&1) w=w*a;
        a=a*a;
        b>>=1;
    }
    return w;
}
int ans1[1111][1111];
int ans[1111][15];
int a[1111][15],b[15][1111];
matrix va;
int main()
{
    __int64 n,k,i,j;


    while (~scanf(" %I64d %I64d",&n,&k),(n|k))
    {
        for (i=0; i<n; i++)
            for (j=0; j<k; j++) scanf(" %d",&a[i][j]);
        for (i=0; i<k; i++)
            for (j=0; j<n; j++) scanf(" %d",&b[i][j]);


        for (int i=0; i<k; i++)
            for (int j=0; j<k; j++)
            {
                va.ma[i][j]=0;
                for (int h=0; h<n; h++)
                    va.ma[i][j]=(va.ma[i][j]+b[i][h]*a[h][j])%6;
            }


        va.row=k;
        va.col=k;
        __int64 m=n*n;
        va=va^(m-1);




        for (int i=0; i<n; i++)
            for (int j=0; j<k; j++)
            {
                ans[i][j]=0;
                for (int h=0; h<k; h++)
                    ans[i][j]=(ans[i][j]+a[i][h]*va.ma[h][j])%6;
            }




        for (int i=0; i<n; i++)
            for (int j=0; j<n; j++)
            {
                ans1[i][j]=0;
                for (int h=0; h<k; h++)
                    ans1[i][j]=(ans1[i][j]+ans[i][h]*b[h][j])%6;
            }
        __int64 sum=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                sum+=(ans1[i][j]%6);
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化供理论依据和技术支持。; 其他说明:本文不仅供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值