博客
关于我
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/

    你可能感兴趣的文章
    Python + selenium自动化生成测试报告!
    查看>>
    Python - C 嵌入式分段错误
    查看>>
    Python - DM 一个用户 Discord 机器人
    查看>>
    python - Flask 基础 - 蓝图( Blueprint )(2)
    查看>>
    python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量
    查看>>
    Python - while循环
    查看>>
    Python - “if“中的逻辑评估顺序陈述
    查看>>
    Python - 从 Google Cloud Storage 下载整个目录
    查看>>
    Python - 使用 pandas 格式化 Excel 单元格
    查看>>
    python - 列表
    查看>>
    Python - 可用时从串行端口数据逐行读取到列表中
    查看>>
    Python - 在应用程序中显示 Web 浏览器/iframe
    查看>>
    Python - 如何使用管道执行shell命令,但没有‘shell = True‘?
    查看>>
    Python - 如何使这个不可腌制的对象可腌制?
    查看>>
    Python - 如何在 Windows 10 上完全卸载 Anaconda?
    查看>>
    python - 如何并行化python numpy中的总和计算?
    查看>>
    Python - 如何解析 xml 响应并将元素值存储在变量中?
    查看>>
    Python - 安装了扩展的远程 Webdriver
    查看>>
    python - 将字符串中的日期与今天的日期进行比较
    查看>>
    python - 数据描述符(class 内置 get/set/delete方法 )
    查看>>