国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

【51Nod】1126 - 求遞推數列的第N項(矩陣快速冪 & C++運算符重載)

2019-11-06 07:41:43
字體:
來源:轉載
供稿:網友

題目鏈接:點擊打開題目


這里寫圖片描述


這里寫圖片描述


代碼如下:

#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <stack>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define PI acos(-1.0)#define LL long longconst int MOD = 7;struct Matrix{ int h,w; int m[3][3]; void init(int op) { if (op == 0) //初始化矩陣 CLR(this->m,0); else if (op == 1) //初始化為單位矩陣 { this->h = this->w = 2; CLR(this->m,0); this->m[1][1] = this->m[2][2] = 1; } else if (op == 2) //初始化為初始矩陣 { this->h = 1; this->w = 2; CLR(this->m,0); this->m[1][1] = this->m[1][2] = 1; } } void init(int A,int B) { this->w = this->h = 2; CLR(this->m,0); this->m[1][1] = A; this->m[2][1] = B; this->m[1][2] = 1; } Matrix Operator * (Matrix a) { Matrix t; t.h = this->h; t.w = a.w; t.init(0); for (int i = 1 ; i <= this->h ; i++) { for (int j = 1 ; j <= a.w ; j++) { if (this->m[i][j]) for (int k = 1 ; k <= this->w ; k++) { t.m[i][k] = (t.m[i][k] + this->m[i][j] * a.m[j][k] % MOD) % MOD; } } } return t; } Matrix quickMod(int n) { Matrix t; t.init(1); while (n) { if (n & 1) t = t * (*this); *this = (*this) * (*this); n >>= 1; } return t; }};int main(){ int A,B,n; scanf ("%d %d %d",&A,&B,&n); if (n == 1 || n == 2) puts("1"); else { Matrix PR,ans,ori; pr.init(2); ori.init(A,B); ori = ori.quickMod(n-2); ans = pr * ori; printf ("%d/n",(ans.m[1][1] % MOD + MOD) % MOD); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 岳西县| 辽阳县| 邓州市| 新兴县| 宜黄县| 赤水市| 锦屏县| 文登市| 二连浩特市| 昭通市| 漳州市| 桑日县| 博客| 潜江市| 美姑县| 裕民县| 故城县| 浪卡子县| 天等县| 大理市| 华宁县| 高雄市| 仁寿县| 常宁市| 永川市| 贵阳市| 逊克县| 浮梁县| 奇台县| 镇雄县| 林甸县| 集贤县| 孟津县| 乐清市| 临澧县| 湛江市| 本溪| 淮南市| 宿州市| 嘉禾县| 正安县|