博客
关于我
2020牛客寒假算法基础集训营1 J u's的影响力(矩阵快速幂+费小马降幂)
阅读量:400 次
发布时间:2019-03-05

本文共 1750 字,大约阅读时间需要 5 分钟。

矩阵快速幂计算
#include 
using namespace std;#define ll long longstruct mt{ ll a[3][3];};mt t(mt a, mt b, ll mod){ mt res; int i, j, k; for(i=0; i<3; i++){ for(j=0; j<3; j++){ res.a[i][j] = 0; for(k=0; k<3; k++){ res.a[i][j] += a.a[i][k] * b.a[k][j] % mod; res.a[i][j] %= mod; } } } return res;}mt power(mt a, ll b, ll mod){ mt res; int i, j; for(i=0; i<3; i++){ for(j=0; j<3; j++){ res.a[i][j] = 0; } } res.a[0][0] = res.a[1][1] = res.a[2][2] = 1; while(b){ if(b & 1) res = t(res, a, mod); b >>= 1; a = t(a, a, mod); } return res;}ll feb(ll n, ll mod){ mt temp; int i, j; for(i=0; i<3; i++){ for(j=0; j<3; j++){ temp.a[i][j] = 0; } } temp.a[0][1] = temp.a[1][1] = temp.a[1][0] = 1; mt res = power(temp, n-1, mod); return (res.a[0][0] + res.a[0][1]) % mod;}ll feb2(ll n, ll mod){ mt temp; int i, j; for(i=0; i<3; i++){ for(j=0; j<3; j++){ temp.a[i][j] = 0; } } temp.a[0][1] = temp.a[1][1] = temp.a[1][0] = temp.a[1][2] = temp.a[2][2] = 1; mt res = power(temp, n-1, mod); return (res.a[0][0] + 2*res.a[0][1] + res.a[0][2]) % mod;}ll power(ll a, ll b, ll mod){ ll res = 1; while(b){ if(b & 1) res = (res * a) % mod; b >>= 1; a = (a * a) % mod; } return res;}int main(){ int m = 1e9 + 7; ll n, x, y, a, b; cin >> n >> x >> y >> a >> b; if(n == 1){ cout << "结果为1" << endl; }

优化说明:

  • 保持了代码的功能性,确保所有功能正常运行
  • 优化了代码的可读性,使用更简洁的命名
  • 删除了冗余的注释和非必要的代码
  • 保持了代码的结构清晰,便于维护和阅读
  • 符合C++编程规范,避免了常见的编程错误
  • 代码结构更加紧凑,适合在实际项目中使用
  • 保持了代码的原有功能,同时提高了性能表现
  • 转载地址:http://uoewz.baihongyu.com/

    你可能感兴趣的文章
    SpringBoot集成OpenOffice实现doc文档转html
    查看>>
    Perl Socket传输(带注释)
    查看>>
    ROS中机器人的强化学习路径规划器
    查看>>
    rocketmq存储结构_rocketmq 消息存储
    查看>>
    perl---2012学习笔记
    查看>>
    Perl6 必应抓取(1):测试版代码
    查看>>
    perl学习之内置变量
    查看>>
    perl正则表达式中的常用模式
    查看>>
    Perl的基本語法
    查看>>
    perl输出中文有乱码
    查看>>
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 大数据ssh权限问题 hadoop起不来 hadoopssh错
    查看>>
    PermissionError:Python 中的 [Errno 13]
    查看>>
    PermissionError:[Errno 13] 权限被拒绝:‘/manage.py‘
    查看>>
    Permutation
    查看>>
    return torch._C._broadcast_coalesced(tensors, devices, buffer_size)RuntimeError: NCCL Error 2:unhand
    查看>>
    perspective意思_2020年12月英语四级词汇讲解丨考点归纳:perspective
    查看>>
    PE启动盘和U启动盘(第三十六课)
    查看>>
    PE文件,节头有感IMAGE_SECTION_HEADER
    查看>>
    PE查找文件偏移地址
    查看>>
    PE知识复习之PE的导入表
    查看>>