zoukankan      html  css  js  c++  java
  • loj #100. 矩阵乘法题解

    这题是比较基础的题目,主要用于理解矩阵乘法。
    (First) (of) (all) ,我们要知道什么是矩阵乘法?
    假设有一个 (n) * (p) 的矩阵 (A)(p) * (n) 的矩阵 (B) ,如果令 (C) 为这两个矩阵相乘得到的乘积,那么有一个这样的式子
    (Ci,j=sum ^{p}_ {k=1}A_{i,k} imes B_{k,j})
    知道这个以后,我们就可以做这道题了:

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #define P 1000000007
    using namespace std;
    long long a[511][511],b[511][511],c[511][511];
    inline long long read()
    {
    	char ch=getchar();
    	long long f=0,w=1;
    	while(ch<'0' || ch>'9')
    	{
    		if(ch=='-') w=-1;
    		ch=getchar();
    	}
    	while(ch>='0' && ch<='9')
    	{
    		f=(f<<3)+(f<<1)+ch-'0';
    		ch=getchar();
    	}
    	return f*w;
    }
    inline void write(long long x)
    {
    	if(x==0) return;
    	if(x<0) putchar('-'),x=-x;
    	write(x/10);
    	putchar(x%10+'0');
    } 
    int main()
    {
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	int n,p,m;
    	scanf("%d %d %d",&n,&p,&m);	
    	for(int i=1;i<=n;++i)
    		for(int j=1;j<=p;++j)
    			a[i][j]=read();
    	for(int i=1;i<=p;++i)
    		for(int j=1;j<=m;++j)
    			b[i][j]=read();
    	for(register int i=1;i<=n;++i)
    		for(register int k=1;k<=p;++k)
    			for(register int j=1;j<=m;++j)
    			{
    				c[i][j]+=(a[i][k]*b[k][j]);
    				c[i][j]%=P;
    			}
    //	printf("%d
    ",-2033333333%P); 
    	for(int i=1;i<=n;++i)
    	{
    		for(int j=1;j<=m;++j)
    		{
    			if(c[i][j]<0) c[i][j]=P+c[i][j]; //若c[i][j]为负数,那么余数等于模数加上这个负数
    			write(c[i][j]); 
    			putchar(' ');
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Netty 源码解读(二)-ChannelPipeline、ChannelHandler、ChannelHandlerContext
    Netty源码解读(一)-服务启动和接收请求源码
    浅谈自动化测试框架开发
    程序运行时环境
    常见的Linux内核线程
    一个好用gdb扩展工具
    使用Qemu模拟Numa机器
    使用qemu的nat方式登录
    2021.32 量化
    2021.31 模型
  • 原文地址:https://www.cnblogs.com/Call-me-zhz/p/11331923.html
Copyright © 2011-2022 走看看