9 条题解
-
6
模板题,下面提供一份封装的矩乘板子:
#include <cstring> #include <iostream> using std::cin; using std::cout; const char endl = '\n'; const int N = 1005, mod = 1e9 + 7; int n, m, p; class Matrix { private: int data[N][N]; public: Matrix() { memset(data, 0x00, sizeof(data)); } int* operator[](int i) { return data[i]; } Matrix operator*(Matrix b) const { Matrix c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= p; j++) { for (int k = 1; k <= m; k++) { c[i][j] = (c[i][j] + 1ll * data[i][k] * b[k][j] % mod) % mod; } } } return c; } } a, b, c; int main() { std::ios::sync_with_stdio(false); cin >> n >> m >> p; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= p; j++) { cin >> b[i][j]; } } c = a * b; for (int i = 1; i <= n; i++) { for (int j = 1; j <= p; j++) { cout << (c[i][j] % mod + mod) % mod << ' '; } cout << endl; } return 0; }
-
3
比如:
(此图来自网络)
代码如下:
#include<cstdio> #define ll long long using namespace std; ll a[505][505],b[505][505]; const ll mod=1000000007; ll n,m,p; inline ll read(){ ll a=0,k=1; char c=getchar(); while (!('0'<=c&&c<='9')){ if (c=='-'){ k*=-1; } c=getchar(); } while ('0'<=c&&c<='9'){ a=a*10+c-'0'; c=getchar(); } return a*k; } int main(){ n=read(),p=read(),m=read(); for (ll i=1;i<=n;i++){ for (ll j=1;j<=p;j++){ a[i][j]=read(); } } for (ll i=1;i<=p;i++){ for (ll j=1;j<=m;j++){ b[i][j]=read(); } } for (ll i=1;i<=n;i++){ for (ll j=1;j<=m;j++){ ll tmp=0; for (ll k=1;k<=p;k++){ tmp=(tmp+(a[i][k]*b[k][j]+mod)%mod+mod)%mod; } printf("%lld ",tmp); } putchar('\n'); } return 0; }
-
2
【模板】矩阵乘法
这道题是一道模板题,还不会的同志们看下面这张图:
比如说: 给出一个矩阵A: 1 2 3 3 2 1 再给出矩阵B: 1 1 2 2 3 3 根据公式得出矩阵C: 14 14 10 10
过程如下
那么按照公式模拟即可,别忘了取模!(尽量一边算一边mod 代码:
#include <bits/stdc++.h> using namespace std; long long A[1114][1114],B[1114][1114],C[1114][1114]; const long long mod=1000000007; int main(){ int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>A[i][j]; } } for(int i=1;i<=m;i++){ for(int j=1;j<=k;j++){ cin>>B[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=k;j++){ long long sum=0; for(int l=1;l<=m;l++){ sum=(sum+(A[i][l]*B[l][j]+mod)%mod+mod)%mod; } C[i][j]=sum%1000000007; //cout<<sum<<" "; } //cout<<endl; } //if(you use the grey one)return 0 here for(int i=1;i<=n;i++){ for(int j=1;j<=k;j++){ cout<<C[i][j]<<" "; } cout<<endl; } return 0;
完结撒花!!!
-
0
#include<bits/stdc++.h> #define ll long long using namespace std; ll a[505][505],b[505][505]; const ll mod=1000000007; ll n,m,p; inline ll read(){** ll a=0,k=1; char c=getchar(); while (!('0'<=c&&c<='9')){ if (c=='-'){ k*=-1; } c=getchar(); } while ('0'<=c&&c<='9'){ a=a*10+c-'0'; c=getchar(); } return a*k; } int main(){ n=read(),p=read(),m=read(); for (ll i=1;i<=n;i++){ for (ll j=1;j<=p;j++){ a[i][j]=read(); } } for (ll i=1;i<=p;i++){ for (ll j=1;j<=m;j++){ b[i][j]=read(); } } for (ll i=1;i<=n;i++){ for (ll j=1;j<=m;j++){ ll tmp=0; for (ll k=1;k<=p;k++){ tmp=(tmp+(a[i][k]*b[k][j]+mod)%mod+mod)%mod; } printf("%lld ",tmp); } putchar('\n'); } return 0; }
-
-2
#include #define ll long long
using namespace std; ll a[505][505],b[505][505]; const ll mod=1000000007; ll n,m,p;
inline ll read(){ ll a=0,k=1; char c=getchar(); while (!('0'<=c&&c<='9')){ if (c=='-'){ k*=-1; } c=getchar(); } while ('0'<=c&&c<='9'){ a=a10+c-'0'; c=getchar(); } return ak; }
int main(){ n=read(),p=read(),m=read(); for (ll i=1;i<=n;i++){ for (ll j=1;j<=p;j++){ a[i][j]=read(); } } for (ll i=1;i<=p;i++){ for (ll j=1;j<=m;j++){ b[i][j]=read(); } } for (ll i=1;i<=n;i++){ for (ll j=1;j<=m;j++){ ll tmp=0; for (ll k=1;k<=p;k++){ tmp=(tmp+(a[i][k]*b[k][j]+mod)%mod+mod)%mod; } printf("%lld ",tmp); } putchar('\n'); } return 0; }
-
-2
#include<cstdio> #define ll long long using namespace std; ll a[505][505],b[505][505]; const ll mod=1000000007; ll n,m,p; inline ll read(){ ll a=0,k=1; char c=getchar(); while (!('0'<=c&&c<='9')){ if (c=='-'){ k*=-1; } c=getchar(); } while ('0'<=c&&c<='9'){ a=a*10+c-'0'; c=getchar(); } return a*k; } int main(){ n=read(),p=read(),m=read(); for (ll i=1;i<=n;i++){ for (ll j=1;j<=p;j++){ a[i][j]=read(); } } for (ll i=1;i<=p;i++){ for (ll j=1;j<=m;j++){ b[i][j]=read(); } } for (ll i=1;i<=n;i++){ for (ll j=1;j<=m;j++){ ll tmp=0; for (ll k=1;k<=p;k++){ tmp=(tmp+(a[i][k]*b[k][j]+mod)%mod+mod)%mod; } printf("%lld ",tmp); } putchar('\n'); } return 0; }
-
-8
#include <bits/stdc++.h> #include <windows.h> #define ll long long #define mod 1000000007 #define maxn 510 inline int read() { int X = 0; bool f = false; char ch = getchar(); while (ch > '9' || ch < '0') {f |= ch == '-'; ch = getchar();} while (ch <= '9' && ch >= '0') {X = (X << 3) + (X << 1) + (ch ^ 48); ch = getchar();} return f ? -X : X; } inline void write(ll X) { if (X == 0) {putchar('0'); return;} if (X < 0) {/*putchar('-'); X = -X;*/ X += mod;} short cnt = 0; char ch[31]; while (X) {ch[++cnt] = (X % 10) ^ 48; X /= 10;} while (cnt) putchar(ch[cnt--]); return; } int n, p, m; ll A[maxn][maxn], B[maxn][maxn], ANS[maxn][maxn]; int main() { n = read(); p = read(); m = read(); 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 (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int k = 1; k <= p; ++k) { ANS[i][j] = (ANS[i][j] + (A[i][k] * B[k][j] % mod)) % mod; } write(ANS[i][j]); putchar(' '); } putchar('\n'); } system("pause"); return 0; }
-
-14
不会把还有人不知道矩阵乘法#include<bits/stdc++.h> #define int long long//记得开longlong using namespace std; const int N = 600, M = 1e9 + 7; int a[N][N], b[N][N]; inline int max(int x, int y){return x > y ? x : y;} inline int min(int x, int y){return x > y ? y : x;} inline int read(){ int x = 0, f = 1; char c = getchar(); while (!isdigit(c)){if (c == '-') f = -1; c = getchar();} while (isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } inline void write(int x){ if (x < 0) {x = ~(x - 1); putchar('-');} if (x > 9) write(x / 10); putchar(x % 10 ^ 48); } signed main(){ int n = read(), p = read(), m = read(); for (int i = 1; i <= n; i ++) for (int j = 1; j <= p; j ++) a[i][j] = (read() + M) % M;//记得取模 for (int i = 1; i <= p; i ++) for (int j = 1; j <= m; j ++) b[i][j] = (read() + M) % M;//记得取模 for (int i = 1; i <= n; i ++){ for (int j = 1; j <= m; j ++){ int sum = M; for (int k = 1; k <= p; k ++) sum = (sum + a[i][k] * b[k][j]) % M; write(sum % M);putchar(' '); } puts(""); } return 0; }
- 1
信息
- ID
- 60
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 802
- 已通过
- 202
- 上传者