博客
关于我
hdu 1757 A Simple Math Problem(矩阵快速幂)
阅读量:136 次
发布时间:2019-02-27

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

?????????????

?????????????????????????????????????????????????????????????

1. ???????

???????????????????????????????????????? $A$ ? $n$ ??????? $A^n = A \cdot A \cdot \ldots \cdot A$?? $n$ ? $A$??????????? $O(n)$ ??????????????????????????????????????? $O(\log n)$??????????

2. ????

???????????

#include 
using namespace std;#define mod(x) ((x) % m)int k, m;struct mat { int d[10][10]; mat operator*(const mat x) { mat ret; int tmp; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { tmp = 0; for (int k = 0; k < 10; k++) { tmp = mod(tmp + d[i][k] * x.d[k][j]); } ret.d[i][j] = tmp; } } return ret; } void init_unit() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) d[i][j] = (i == j) ? 1 : 0; } void init() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) d[i][j] = (i == j + 1) ? 1 : 0; } void set(int i, int v) { d[9 - i][9] = v; }};mat fastPow(mat base, int pow) { mat res; res.init_unit(); while (pow) { if (pow & 1) res = res * base; base = base * base; pow >>= 1; } return res;}int main() { int tmp; while (scanf("%d%d", &k, &m) == 2) { if (k < 10) { printf("%d\n", k % m); continue; } a.init(); for (int i = 0; i < 10; i++) scanf("%d", &tmp), a.set(i, tmp); a = fastPow(a, k - 9); int ans = 0; for (int i = 0; i < 10; i++) ans = mod(ans + a.d[i][9] * i); printf("%d\n", ans); } return 0;}

3. ????

  • ?????????

    ??? mat ????? 10x10 ?????????????????????????????????????

  • ?????

    • init_unit() ????????????? 1???? 0??
    • init() ????????????????????? 1???? 0?
    • set() ????????????????
  • ?????

    fastPow ???????????????????????????????????????????????????

  • ?????

    ?????? k ? m?????? a????????????? a ???????????????

  • 4. ??

    ???????????????????????????????????????????????????????????????????????????

    转载地址:http://dwib.baihongyu.com/

    你可能感兴趣的文章
    Python基础 | 关于“循环”那些事
    查看>>
    python系列【仅供参考】:python之subprocess模块rsync拉取文件
    查看>>
    python系列【仅供参考】:python 远程rdp
    查看>>
    python基础
    查看>>
    Python基本语法与变量类型
    查看>>
    python基本数据类型(容器)- tuple list dict set
    查看>>
    python基本工资的调整方案_Python薪资又涨了!这可咋办!
    查看>>
    Python基准测试和性能分析内存管理和垃圾回收
    查看>>
    Python基于RESTful风格的接口自动化测试实战
    查看>>
    python基于pygame实现跨年烟花效果
    查看>>
    python基于flask搭建http服务(四)—— Docker容器化部署
    查看>>
    python基于flask搭建http服务(二)—— 实现Excel上传、数据清洗、入库
    查看>>
    python基于flask搭建http服务(三)—— 使用gunicorn部署项目
    查看>>
    python基于flask搭建http服务(一)—— 实现数据查询和Excel导出
    查看>>
    Python块键盘/鼠标输入
    查看>>
    python在心理学研究中的应用有哪些_心理学在线研究可用平台简介和应用进展
    查看>>
    python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
    查看>>
    python在gpu上运行_【python】python开启GPU加速
    查看>>
    Python在def函数中使用for循环
    查看>>
    Python在def函数中使用for循环
    查看>>