Matrix..0.0

来源:互联网 发布:matlab编程实例 编辑:程序博客网 时间:2024/06/08 17:51

Input

1 2 3
1 2 5

Output

4
7

Hint

1 2 3 4
7 12 19 30
49 80 129 208

Solution

#include <set>#include <map>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cassert>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>#define ALL(x) x.begin(),x.end()#define INS(x) inserter(x,x.begin())typedef long long int LLI;const int MOD = 1000000007;const int N = 4;LLI l,r,t,n;struct Matrix {    LLI mat[N][N];    Matrix operator*(const Matrix& m)const {        Matrix tmp;        for(LLI i = 0 ; i < N ; i++) {            for(LLI j = 0 ; j < N ; j++) {                tmp.mat[i][j] = 0;                for(LLI k = 0 ; k < N ; k++)                    tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*m.mat[k][j])%MOD;                tmp.mat[i][j] %= MOD;            }        }        return tmp;    }};Matrix m;void print(Matrix d){    LLI i,j;    for (i=0;i<4;++i)    {        for (j=0;j<4;++j)            printf("%4lld",d.mat[i][j]);        printf("\n");    }    printf("\n");}LLI Pow() {    Matrix ans,tt=m;    for (LLI i=0; i<4; ++i)        ans.mat[i][0]=l;    for (LLI i=0; i<4; ++i)        ans.mat[i][1]=r;    for (LLI i=0; i<4; ++i)        ans.mat[i][2]=l+r;    for (LLI i=0; i<4; ++i)        ans.mat[i][3]=l+r+r-1;    n--;    while(n) {        if (n%2) tt = tt*m;        //print(tt);        n /= 2;        m = m*m;        //print(m);    }    ans=ans*tt;    //print(ans);    return ans.mat[0][t]%MOD;}int main() {    while(~scanf("%lld%lld",&l,&r)) {        for (LLI i=0; i<4; ++i) m.mat[0][i]=0;        m.mat[1][0]=0;        m.mat[2][0]=m.mat[3][0]=m.mat[1][1]=m.mat[1][2]=m.mat[1][3]=m.mat[3][1]=1;        m.mat[2][1] = m.mat[3][2]=2;        m.mat[2][2] = 3;        m.mat[2][3]=m.mat[3][3]=4;        scanf("%lld", &n);        t=(n-1)%4;        n=(n-1)/4;        if (!n)        {            if (t==3) printf("%lld\n",r+r+l-1);            else if (t==2) printf("%lld\n",r+l);            else if (t==1) printf("%lld\n",r);            else printf("%lld\n",l);        } else   printf("%lld\n", Pow());    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 C语言实验题――最小公倍数和最大公约数 C语言实验题――分数序列 C语言实验题――数组逆序 C语言实验题――一元二次方程 C语言实验题――某年某月的天数 相邻最大数 Picture 羊村运动会――撑杆跳高 字符矩阵排序 1535:找规律填数字 idm6.38 C语言实验题――圆周率 缺陷检测 成都瓶儿 谭松韵 桥本 YouTube、Google+、Facebook、Twitter、维基百科最全hosts C语言实验题――拍皮球 求奇数的乘积 C语言实验题――字符编码 intellijlicense andriod导航栏 mfc文件上传下载 Eclipse4.8中搭建Android开发环境 1178:C语言实验题――单词统计 现在小明决定实现一个类似malloc的内存分配系统,具体来说,他需要连续处理若干申请内存的请求,这个 给定某二叉树的前序序列和中序序列,输出该二叉树的后序序列。(输入的前序遍历和中序遍历的结果中都不含重 vibe twitter 幼呦 fasttunnel 一叫三吱 浩君恋母 C语言实现朴素贝叶斯 shadowsocks服务 CCF201509-2日期计算 song:平面上若干个点,求任意两点间的最长距离 求n阶乘(1<=n<=10000) 【问题描述】计算一个班级N个学生三门课的各门课平均分,三门课总分,并输出成绩最高者的信息【输入形式】 NOD32分享区 舞蹈生