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

    你可能感兴趣的文章
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    Node.js 函数是什么样的?
    查看>>