CF450B——水矩阵
来源:互联网 发布:linux 网络编程本地ip 编辑:程序博客网 时间:2024/05/21 10:59
传送门:http://codeforces.com/problemset/problem/450/B
题意:题意很简单,就是给递推式写答案。把题中公式移项就可以了,构造就不说了,主要在小细节。这里是用了矩阵快速幂的优化,比较果
注意:对负数取余要先加mod再mod
代码:
#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<ctime>#include<list>#include<vector>#include<set>#include<map>#include<stack>#include<queue>#pragma GCC optimize("02")#define cl(a,b) memset(a,b,sizeof(a))#define in freopen("F://1.txt","r",stdin)#define out freopen("F://2.txt","w",stdout)using namespace std;typedef unsigned long long llu;typedef long long ll;const ll inf=(ll)1<<60;const int maxn=1e5+7;const int MAXN = 2;long long mod = 1000000007;struct Matrix{ long long mat[MAXN][MAXN]; void Zero(){ cl(mat, 0); //memset(mat, 0, sizeof(mat)); } void Unit(){ //memset(mat, 0, sizeof(mat)); cl(mat, 0); for (int i = 0; i < MAXN; i++) mat[i][i] = 1; } void output(){ for (int i = 0; i < MAXN; i++){ for (int j = 0; j < MAXN; j++){ printf("%d ", mat[i][j]); } printf("\n"); } }};Matrix operator*(Matrix &a, Matrix &b){ Matrix tmp; tmp.Zero();///初始化 for (int k = 0; k < MAXN; k++){ for (int i = 0; i < MAXN; i++){ if (!a.mat[i][k]) continue; for (int j = 0; j < MAXN; j++){ tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j] % mod; if ( tmp.mat[i][j] >= mod) tmp.mat[i][j] -= mod; } } } return tmp;}Matrix operator ^(Matrix a, int k){ Matrix tmp;///单位矩阵 tmp.Unit(); if(k<=1) return a; for (; k; k >>= 1){ if (k & 1) tmp = tmp * a; a = a * a; } return tmp;}int main(){ int i,j; ll x, y, n; Matrix t; t.mat[0][0] = 1; t.mat[0][1] = -1; t.mat[1][0] = 1; t.mat[1][1] = 0; while(~scanf("%lld %lld %lld",&x, &y, &n)){ if(n==1){ printf("%lld\n",(x%mod+mod)%mod); continue; } else if(n==2){ printf("%lld\n",(y%mod+mod)%mod); continue; } else{ Matrix temp = t ^ (n - 2); ll ans = ((temp.mat[0][0]*y%mod + temp.mat[0][1]*x%mod)%mod+mod)%mod; printf("%lld\n",ans); } } return 0;}
阅读全文
0 0
- CF450B——水矩阵
- cf450B. Jzzhu and Sequences
- 数组——蛇形矩阵、螺旋矩阵
- 《矩阵》——稀疏矩阵(Java)
- 特殊矩阵——稀疏矩阵
- MKL学习——矩阵矩阵操作
- UVA10518——水矩阵+细节
- poj3233——矩阵
- 数据结构——矩阵
- R数据结构—矩阵
- nRF52832 — 矩阵键盘
- 蓝桥杯—蛇行矩阵
- 线性代数——矩阵基本概念与随机矩阵
- 线性代数矩阵论——矩阵的属性
- CV_MAT_ELEM——获取矩阵元素和初始化矩阵
- shuoj—矩阵幂—矩阵快速幂
- 矩阵手册(四)—— 增广矩阵
- 特殊矩阵——对称矩阵(Symmetric Matrix)
- 【观察】解读甲骨文自治式数据库云 背后的三大信号与三重价值
- keras: Why is the training loss much higher than the testing loss?
- gdb的基础详细用法
- 求1+2+3.+...
- top和ps取长补之自定义要显示的字段列表
- CF450B——水矩阵
- 51nod-1070-Bash游戏 V4
- win7基础 cmd del+*.扩展名 删除指定文件夹下的同一类型的所有文件
- Spring Cache抽象-缓存管理器
- 常见的机器学习&数据挖掘知识点
- python 参数及简单文件操作
- c/c++无结束标志时,输入一串int型
- QT之QApplication的exit,quit调用后仍然显示窗口
- kafka server.properties配置文件