Hdu 4965(矩阵快速幂)
Fast Matrix Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 87 Accepted Submission(s): 39Problem DescriptionOne day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation.
Step 1: Calculate a new N*N matrix C = A*B.
Step 2: Calculate M = C^(N*N).
Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’.
Step 4: Calculate the sum of all the elements in M’.Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.
InputThe input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has K integers between 0 and 5, representing matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing matrix B.The end of input is indicated by N = K = 0.
OutputFor each case, output the sum of all the elements in M’ in a line.Sample Input4 2
5 5
4 4
5 4
0 0
4 2 5 5
1 3 1 5
6 3
1 2 3
0 3 0
2 3 4
4 3 2
2 5 5
0 5 0
3 4 5 1 1 0
5 3 2 3 3 2
3 1 5 4 5 2
0 0Sample Output14
56
/*************************************************************************
> File Name: 1006.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月19日 星期二 12时05分28秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ struct mat {
int n, m;
vector<vector<int> >M;
mat() {}
mat(int a, int b):n(a), m(b) {
M.resize(n);
for (int i = ; i < n; i++) M[i].resize(m, );
}
friend mat operator * (const mat &a, const mat &b) {
mat c(a.n, b.m);
for (int i = ; i < a.n; i++) {
for (int k = ; k < b.n; k++) {
for (int j = ; j < b.m; j++) {
c.M[i][j] += a.M[i][k] * b.M[k][j]; //注意这里的循环顺序
c.M[i][j] %= ;
}
}
}
return c;
}
};
void read(int &res) {
res = ; char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= ''&&c<='') res = res*+c-'', c = getchar();
} int main(void) {
int n, k;
while (~scanf("%d %d", &n, &k) && n + k) {
mat A(n, k), B(k, n);
for (int i = ; i < n; i++) for (int j = ; j < k; j++)
read(A.M[i][j]);
for (int i = ; i < k; i++) for (int j = ; j < n; j++)
read(B.M[i][j]);
mat C = B * A;
int b = n * n - ;
mat res(k, k);
for (int i = ; i < k; i++) res.M[i][i] = ;
while (b) {
if (b&) res = res * C;
C = C * C;
b >>= ;
}
mat ans = A * (res * B);
int sum = ;
for (int i = ; i < n; i++) for (int j = ; j < n; j++)
sum += ans.M[i][j];
printf("%d\n", sum);
} return ;
}
Hdu 4965(矩阵快速幂)的更多相关文章
- HDU 4965 矩阵快速幂
顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> ...
- hdu 4965 矩阵快速幂 矩阵相乘性质
Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Jav ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
- HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识
求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...
- How many ways?? HDU - 2157 矩阵快速幂
题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...
- HDU 5950 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- Caffe系列3——制作Hdf5数据源详细教程(手把手教你制作Hdf5数据源)
制作Hdf5数据源详细教程
- uoj349 即时战略
题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...
- mysql权限操作
1.mysql权限操作 grant select,insert on test1.tb1 to ltx2@127.0.0.1 默认权限:什么都没有 2.用户管理特殊命令: 创建用户:create us ...
- C++内存字节对齐规则
为什么要进行内存对齐以及对齐规则 C/C++—— 内存字节对齐规则 C++内存字节对齐规则
- 19-10-29-Z
%%%ZZYY 只是因为是Z才模一下的. ZJ一下: 考试T1写了三张纸但是它死了. T2T3暴力叕写跪了. 考试一定一定不能不严密,少推两个交点是要命的啊. 就因为叕叕少开龙龙见祖宗了. 如果考试能 ...
- iOS开发之IMP和SEL(方法和类的反射)
1.SEL:类方法的指针,相当于一种编号,区别与IMP! IMP:函数指针,保存了方法的地址! SEL是通过表取对应关系的IMP,进行方法的调用! 2.获取SEL和IMP方法和调用: SEL meth ...
- 转:进程上下文VS中断上下文
源地址:http://www.cnblogs.com/zzx1045917067/archive/2012/12/19/2824552.html 内核空间和用户空间是现代操作系统的两种工作模式,内核模 ...
- Spring MVC(十二)--使用ModelView实现重定向
上一篇总结了使用返回字符串的方式实现重定向以及重定向过程中传递字符串参数和pojo参数的过程,本篇总结另一种重定向的实现方式--返回ModelAndView 这次的场景是这样的:在页面输入一些信息添加 ...
- fastjson对Date类型的格式化
@JSONField(format="yyyy-MM-dd HH:mm:ss.SSS") private Date sendMqDate; //MQ发送时间
- 在scrapy中过滤重复的数据
当为了确保爬到的数据中没有重复的数据的时候,可以实现一个去重的item pipeline 增加构造器方法,在其中初始化用于对与书名的去重的集合 在process_item方法中,先取出item中要判断 ...