HDU5950Recursive sequence(构造矩阵+矩阵乘法)
来源:互联网 发布:mac不能无线键盘 编辑:程序博客网 时间:2024/06/08 09:39
题目大意:
求
思路:http://m.blog.csdn.net/chen_ze_hua/article/details/52973395
首先一看这个题目,很容易想到构造矩阵:那么我们现在来分析一下怎么构造这个矩阵,那么
然后
然后
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll mod = 2147493647;struct Matrix{ int r, c; ll mat[7][7];};ll mat[7][7]={ {1, 4, 6, 4, 1, 0, 0}, {0, 1, 3, 3, 1, 0, 0}, {0, 0, 1, 2, 1, 0, 0}, {0, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 1}, {1, 4, 6, 4, 1, 2, 1}};Matrix mul(Matrix a, Matrix b){ Matrix ret; ret.r = a.r; ret.c = b.c; for(int i = 0; i < ret.r; i++) for(int j = 0; j < ret.c; j++) { ret.mat[i][j] = 0; for(int k = 0; k < a.c; k++) { ret.mat[i][j] += a.mat[i][k] * b.mat[k][j]; ret.mat[i][j] %= mod; } } return ret;}Matrix quick_pow(Matrix a, ll b){ Matrix ret; ret.r = ret.c = 7; memset(ret.mat, 0, sizeof(ret.mat)); for(int i = 0; i < 7; i++) ret.mat[i][i] = 1; while(b) { if(b&1) ret = mul(ret, a); a = mul(a, a); b >>= 1; } return ret;}int main(){ int t; scanf("%d", &t); while(t--) { ll n, a, b; scanf("%I64d%I64d%I64d", &n, &a, &b); if(n == 1) printf("%I64d\n", a); else if(n == 2) printf("%I64d\n", b); else { Matrix temp; temp.r = temp.c = 7; memcpy(temp.mat, mat, sizeof(mat)); Matrix ret = quick_pow(temp, n-2); temp.r = 7;temp.c = 1; temp.mat[0][0] = 16,temp.mat[1][0] = 8,temp.mat[2][0] = 4,temp.mat[3][0] = 2,temp.mat[4][0] = 1,temp.mat[5][0] = a%mod, temp.mat[6][0] = b%mod; ret = mul(ret, temp); printf("%I64d\n", ret.mat[6][0]); } } return 0;}
阅读全文
0 0
- HDU5950Recursive sequence(构造矩阵+矩阵乘法)
- HDU5950Recursive sequence(矩阵快速幂)
- Number Sequence hdu 构造矩阵乘法
- HDU 5950 Recursive sequence(构造矩阵+矩阵乘法)——2016ACM/ICPC亚洲区沈阳站(重现赛)
- HDU 2971(数论,构造矩阵+矩阵乘法优化)
- HDU 2855(数论,构造矩阵+矩阵乘法)
- HDU 5950 Recursive sequence(矩阵构造+矩阵快速幂)
- [矩阵乘法 + 矩阵构造] HDU 2971 Tower
- 矩阵构造方法与矩阵乘法
- POJ 3735 构造矩阵乘法
- POJ 3420 构造矩阵乘法
- POJ 1977 构造矩阵乘法
- 矩阵乘法(矩阵)【hpu】
- POJ 2778-DNA Sequence(AC自动机+矩阵乘法)
- poj2778 DNA Sequence (AC自动机+矩阵乘法)
- 【codevs1287】矩阵乘法(矩阵乘法)
- 【codevs1287】矩阵乘法(矩阵乘法)
- ZOJ Alice's Sequence II //矩阵乘法
- UVA-508 Morse Mismatches(STL模拟)
- 框架设计的一些思考
- 三大主流框架的优缺点
- hdu-1532
- 模拟
- HDU5950Recursive sequence(构造矩阵+矩阵乘法)
- php使用session来控制仅有一个账号登录应用
- java和jQuery实现跨域
- python-数字 字符串 列表的转换
- LeetCode 383 Ransom Note
- Unity 基础常用的脚本(一)
- C/C++之常用字符串比较总结
- Java中对象的赋值与引用
- 一个m*n的矩阵,从左到右从上到下都是递增的,给一个数x,判断x是否在矩阵中。(高效率)