(矩阵快速幂, 数学公式推导)HDU 6050 Funny Function
来源:互联网 发布:善用佳软 知乎 编辑:程序博客网 时间:2024/05/22 10:49
HDU-2017 多校训练赛2-1006-Funny Function
题目网址: HDU 6050 Funny Function
题解(BestCoder提供):
对于任意i>=1,当j>=3时,有
通过归纳法可以得到
- 进而推导出
- 通过矩阵快速幂求解
代码:
#include <iostream>#include <cstring>using namespace std;#define SIZE 2struct matrix{ long long e[SIZE][SIZE]; matrix() { memset(e, 0, sizeof(e)); }}A, A_n;const long long mod = 1e9+7; //上限matrix operator*(matrix &a, matrix &b){ matrix t; for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { for (int k = 0; k < SIZE; ++k) { t.e[i][j] += a.e[i][k]*b.e[k][j]; t.e[i][j] %= mod; } } } return t;}matrix quickpower(matrix &a, long long b){ matrix ans; for (int i = 0; i < SIZE; ++i) { ans.e[i][i] = 1; } while (b) { if (b & 1) ans = ans * a; b >>= 1; a = a * a; } return ans;}int main(int argc, char const *argv[]){ int testn; scanf("%d", &testn); while (testn--) { long long n, m; scanf("%I64d %I64d", &n, &m); A.e[0][0] = 0; A.e[1][0] = A.e[1][1] = 1; A.e[0][1] = 2; A_n = quickpower(A, n); matrix b; if(n & 1) { b.e[0][0] = -1; b.e[0][1] = 2; b.e[1][0] = 1; b.e[1][1] = 0; } else { b.e[0][0] = b.e[1][1] = 1; b.e[0][1] = b.e[1][0] = 0; } A_n.e[0][0] -= b.e[0][0]; A_n.e[0][1] -= b.e[0][1]; A_n.e[1][0] -= b.e[1][0]; A_n.e[1][1] -= b.e[1][1]; A_n = quickpower(A_n, m-1); long long ans = (A_n.e[0][0] + A_n.e[1][0]) % mod; printf("%I64d\n", ans); } return 0;}
阅读全文
1 0
- (矩阵快速幂, 数学公式推导)HDU 6050 Funny Function
- HDU 6050 Funny Function 矩阵快速幂
- hdu 6050Funny Function矩阵快速幂
- hdu 6050 Funny Function (矩阵快速幂)
- HDU 6050 Funny Function(矩阵快速幂或公式)
- HDU 6050 Funny Function(快速幂)
- hdu 6050 Funny Function(快速幂)
- HDU6050 Funny Function[矩阵快速幂]
- HDU 6050 Funny Function(矩阵)
- hdu 6050 Funny Function (构造矩阵)
- hdu 6050 Funny Function (数学题+快速幂)
- HDU 6050 Funny Function【乘法逆元+快速幂】
- 2017多校联合第二场 1006题 hdu 6050 Funny Function 递推公式 / 矩阵快速幂
- HDU 6050 Funny Function
- hdu 6050 Funny Function
- hdu 6050 funny function
- HDU-6050 Funny Function
- HDU 6050 Funny Function
- 图书管理系统
- mysql(四)
- 线程安全、线程私有变量
- 文件系统EXT3,EXT4和XFS的区别
- 日期差值 【codeup 1928】
- (矩阵快速幂, 数学公式推导)HDU 6050 Funny Function
- java推送技术的选择(一)
- Easy kengen
- 接入WebSocket
- Makefile 文件怎么写
- Spring的Resource的方法
- C/C++ | 27-19 写出程序把一个链表中的接点顺序倒排
- Python学习(三)——字典、有序字典、集合
- Vim代码补全插件YouCompleteMe的自动化安装[转]