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

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

矩阵快速幂计算
#include
using namespace std;
#define ll long long
struct 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/

    你可能感兴趣的文章
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    Node.js 函数是什么样的?
    查看>>
    Node.js 历史
    查看>>
    Node.js 在个推的微服务实践:基于容器的一站式命令行工具链
    查看>>
    Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
    查看>>
    node.js 怎么新建一个站点端口
    查看>>
    Node.js 文件系统的各种用法和常见场景
    查看>>
    node.js 简易聊天室
    查看>>
    node.js 配置首页打开页面
    查看>>
    node.js+react写的一个登录注册 demo测试
    查看>>
    Node.js中环境变量process.env详解
    查看>>
    Node.js卸载超详细步骤(附图文讲解)
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    Node.js安装及环境配置之Windows篇
    查看>>
    Node.js安装和入门 - 2行代码让你能够启动一个Server
    查看>>
    node.js安装方法
    查看>>
    Node.js的循环与异步问题
    查看>>