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

    你可能感兴趣的文章
    Pandas DataFrame 的 describe()方法详解-ChatGPT4o作答
    查看>>
    Pandas DataFrame中删除列级的方法链接解决方案
    查看>>
    Pandas DataFrame中的列从浮点数输出到货币(负值)
    查看>>
    Pandas DataFrame中的列从浮点数输出到货币(负值)
    查看>>
    pandas DataFrame的一些操作
    查看>>
    Pandas Dataframe的日志文件
    查看>>
    pandas GROUPBY+变换和多列
    查看>>
    pandas Groupby:创建两列的Groupby时,如何按正确的顺序对工作日进行排序?
    查看>>
    Pandas matplotlib 无法显示中文
    查看>>
    pandas PIVOT_TABLE保持索引
    查看>>
    Pandas Plots:周末的单独颜色,x 轴上漂亮的打印时间
    查看>>
    pandas to_latex() 转义数学模式
    查看>>
    Pandas 中的多索引旋转
    查看>>
    Pandas 中的日期范围
    查看>>
    pandas 中的时间序列箱线图
    查看>>
    Pandas 使用指南
    查看>>
    pandas 分组并使用最小值更新
    查看>>
    pandas 均值(mean), 均值填充NA(fill_na)
    查看>>
    Pandas 对数据框的布尔比较
    查看>>
    pandas 将通话数据分割为15分钟的间隔
    查看>>