gym 101061-B RGB plants 裸矩阵快速幂
来源:互联网 发布:软件界面模糊 编辑:程序博客网 时间:2024/05/17 12:05
heu_summer_final_contest_G
http://codeforces.com/gym/101061/problem/B
题意略, 快速幂求出matrix 的 (n-1)次方, 对其中所有元素求和即可.
#include <stdio.h>#include <iostream>#include <cmath>#include <cstring>using namespace std;const int MAX = 10;const int mod = 1e9+7;struct Martix { int array[MAX][MAX]; int row, column; void clear() { memset(array, 0, sizeof(array)); row = 0, column = 0; } void getin() { cin >> row >> column; for (int i = 0; i < row; ++i) for (int j = 0; j < column; ++j) cin >> array[i][j]; } void output() { for (int i = 0; i < row; ++i) { for (int j = 0; j < column; ++j) cout << array[i][j] << " "; cout << endl; } } Martix operator + (const Martix &b) const { Martix tmp; tmp.row = row, tmp.column = column; for (int i = 0; i < row; ++i) for (int j = 0; j < column; ++j) tmp.array[i][j] = array[i][j] + b.array[i][j]; } Martix operator - (const Martix &b) const { Martix tmp; tmp.row = row, tmp.column = column; for (int i = 0; i < row; ++i) for (int j = 0; j < column; ++j) tmp.array[i][j] = array[i][j] - b.array[i][j]; } Martix operator * (const Martix &b) const { Martix tmp; tmp.clear(); if (column != b.row) return tmp; tmp.row = row, tmp.column = b.column; for (int i = 0; i < row; ++i) for (int j = 0; j < b.column; ++j) for (int k = 0; k < column; ++k) { long long t = (((long long)(array[i][k]%mod) * (b.array[k][j]%mod)) % mod); tmp.array[i][j] += (int)t; while (tmp.array[i][j] >= mod) tmp.array[i][j] -= mod; } return tmp; }} matrix, mt;Martix matpow(Martix tmp, int n, int mod) { Martix ret; ret.row = tmp.row, ret.column = tmp.column; for (int i = 0; i < tmp.row; ++i) for(int j = 0; j < tmp.row; ++j) ret.array[i][j] = (i == j); while (n) { if (n & 1) { ret = (ret * tmp); } tmp = (tmp * tmp); n >>= 1; } return ret;}void init() { matrix.row = 3, matrix.column = 3; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) matrix.arra[i][j] = j+1+3*i;}void judge() { init(); int n = 0, T = 0; cin >> T; long long sum = 0; while (T--) { cin >> n; sum = 0; mt = matpow(matrix, n-1); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j){ sum += mt.arra[i][j]; while (sum >= mod) sum -= mod; } cout << sum << endl; }}int main() {// freopen("in.in", "r", stdin);// freopen("out.out", "w", stdout); judge(); return 0;}
本来 memset() 函数是用的两个rep循环, 结果因为 row 没有初始化结果一直T掉,找了半天才发现是这里的问题....
2017-08-28
阅读全文
0 0
- gym 101061-B RGB plants 裸矩阵快速幂
- 矩阵快速幂——RGB plants (Gym 101061 B)
- Codeforces Gym 101061 B RGB plants(矩阵快速幂)
- GYM 101061 B.RGB plants(dp+矩阵快速幂)
- Codeforces Gym 101061B RGB plants
- GYM 101061 G.Repeat it(dp+矩阵快速幂)
- Gym - 100625E Encoded Coordinates 矩阵快速幂
- GYM 100827 F.Knights(dp+矩阵快速幂)
- CF 450B矩阵快速幂
- Jzzhu and Sequences - CF 450B 矩阵快速幂版
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
- 矩阵快速幂 CodeForces - 582B Once Again...
- CF 450 B Jzzhu and Sequences(矩阵快速幂)
- BestCoder Round #80 A B C hdu5667矩阵快速幂
- hdu5895Mathematician QSC(矩阵快速幂+a^b%mod做法)
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B-Jzzhu and Sequences (矩阵快速幂)
- BZOJ 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套splay【未AC】
- [BZOJ]3282: Tree lct
- Privacy Policy
- 数组去重之简单,装逼方法,,,
- Apache Shiro V1.2 SecurityUtils与SecurityManager的关系以及Subject的创建
- gym 101061-B RGB plants 裸矩阵快速幂
- 记录一个Tomcat启动参数修改,导致不识别<async-supported>的问题
- Eclipse之Hibernate插件报错
- 论文阅读——利用Binary Hash Codes的深度图像检索
- java得到距离指定时间num时间的时间、以及反射调用方法、Log日志配置信息
- 多个项目部署到一个tomcat下面报错:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
- Vue学习笔记(1)安装vue
- Permutations
- Java多线程(一)、多线程的基本概念和使用