POJ 3734 Blocks
2016-08-16 00:20
351 查看
有一排砖,数量为N。现要将砖全部染上色,有红、蓝、绿、黄四种颜色。
要求被染成红色和绿色的砖块数量必须为偶数,问一共有多少种染色方案。
(由于答案较大,模10007)
矩阵快速幂
这个写的好
要求被染成红色和绿色的砖块数量必须为偶数,问一共有多少种染色方案。
(由于答案较大,模10007)
矩阵快速幂
这个写的好
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #define pi acos(-1) #define LL long long #define ULL unsigned long long #define inf 0x3f3f3f3f #define INF 1e18 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 using namespace std; typedef pair<int, int> P; const int maxn = 4; const int mod = 10007; struct Matrix{ int n, m; int a[maxn][maxn]; Matrix(){ // 矩阵初始化 (叫做构造函数 会自动执行 我学了c++后才知道的..) n = m = 0; memset(a, 0, sizeof(a)); } Matrix operator * (const Matrix &b) const { Matrix tmp; tmp.n = n; tmp.m = b.m; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ if (!a[i][j]) continue; //稀疏矩阵乘法优化 for (int k = 0; k < b.m; k++){ tmp.a[i][k] = (tmp.a[i][k] + a[i][j] * b.a[j][k]) % mod; } } } return tmp; } }; int fast_muti(int n) { Matrix base, ans; base.n = base.m = 4; base.a[0][0] = 2; base.a[0][1] = 1; base.a[0][2] = 1; base.a[0][3] = 0; base.a[1][0] = 1; base.a[1][1] = 2; base.a[1][2] = 0; base.a[1][3] = 1; base.a[2][0] = 1; base.a[2][1] = 0; base.a[2][2] = 2; base.a[2][3] = 1; base.a[3][0] = 0; base.a[3][1] = 1; base.a[3][2] = 1; base.a[3][3] = 2; ans.n = ans.m = 4; for (int i = 0; i < 4; i++) ans.a[i][i] = 1; while (n){ if (n & 1) ans = base * ans; base = base * base; n >>= 1; } return ans.a[0][0]; } int main(void) { // freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); int n, T; cin >> T; while (T--) { cin >> n; printf("%d\n", fast_muti(n)); } return 0; }
相关文章推荐
- NDK支持的C++运行库
- 四大组件之Service
- 虚函数HOOK
- Codeforces277A 【dfs联通块】
- javascript 数组
- 在javascript中读取php数组
- IntentService介绍
- 206. Reverse Linked List
- 开机自动启动NGinx
- Nginx+Tomcat+Redis负载均衡集群服务搭建
- Python 之 闭包
- Elasticsearch笔记一之简介与安装
- HDU 5306 Gorgeous Sequence
- Android开发之代码优化
- 记录:Properties方法演示
- POJ3254 Corn Fields
- Mysql中的commit()语句
- 双谷人才财务管理(1)
- java中的回调机制
- Service 通过 BroadcastReceiver与Activity 进行通信