当前位置: 首页 > news >正文

dp+矩阵快速幂,CF551D. GukiZ and Binary Operations

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

Problem - 551D - Codeforces


二、解题报告

1、思路分析

今天LATEX怎么不好用了

数据量很大,应该要用O(log) 或者 O(1)的的算法

按位考虑进行dp,计算k每位的方案数累乘即可,详细看下面的LATEX


2、复杂度

时间复杂度: O(L + 8 * log(n)) 空间复杂度:O(L)

3、代码详解

#include <bits/stdc++.h>
using PII = std::pair<int, int>;
using i64 = long long;
using i128 = __int128;
std::ostream& operator<< (std::ostream& out, i128 x) {std::string s;while (x) s += ((x % 10) ^ 48), x /= 10;std::reverse(s.begin(), s.end());return out << s;
}int qp(int a, i64 b, int p) {int res = 1;while (b) {if (b & 1) res = 1LL * res * a % p;a = 1LL * a * a % p, b >>= 1;}return res;
}int P;
template<typename T>
class Matrix {
private:T m, n;std::vector<std::vector<T>> g;
public:Matrix(int _m, int _n) : m(_m), n(_n), g(m, std::vector<T>(n)) {}void init1 () {for (int i = 0; i < m; i ++ ) g[i][i] = 1;}Matrix(const Matrix& x) : m(x.m), n(x.n), g(x.g) {}void init (const std::vector<std::vector<T>> _g) {g = _g;}Matrix<T> operator * (const Matrix<T>& x) const {Matrix<T> res(m, x.n);for (int i = 0; i < m; i ++ )for (int k = 0; k < n; k ++ )for (int j = 0; j < n; j ++ )res.g[i][j] = (res[i][j] + 1LL * g[i][k] * x[k][j] % P) % P;return res;}const std::vector<T>& operator [] (int i) const {return g[i];}Matrix<T> qp(i64 b) {Matrix<T> res(m, n), a(*this);res.init1();while (b) {if (b & 1) res = res * a;a = a * a, b >>= 1;}return res;}
};i64 fib(i64 n) {if (n <= 2) return 1; Matrix<i64> a(1, 2) ,b(2, 2);std::vector<std::vector<i64>> aa { { 1, 1 } }, bb { { 1, 1 }, { 1, 0 } };a.init(aa), b.init(bb);return (a * b.qp(n - 1))[0][0];
}void solve() {i64 N, K, L, M;std::cin >> N >> K >> L >> M;P = M;if (M == 1 || (L < 63 && 1LL << L <= K)) {std::cout << 0;return;}if (L == 0) {if (K == 0) std::cout << 1;else std::cout << 0;return;}std::vector<int> d(L);for (int i = 0; i < L; i ++)d[i] = (K >> i & 1);i64 f = fib(N + 1), q = ((qp(2, N, M) - f) % M + M) % M, res = 1;for (int i = 0; i < L; i ++) {if (d[i]) res = res * q % M;else res = res * f % M;}std::cout << res;}int main () {std::ios::sync_with_stdio(false);   std::cin.tie(0);  std::cout.tie(0);int _ = 1;// std::cin >> _;while (_ --)solve();return 0;
}

相关文章:

  • 【数据分析基础】实验一 Python运算符、内置函数、序列基本用法
  • 什么时候用C而不用C++?
  • mysql当前状态分析(show status)
  • 吃星星(1.5)
  • 网页音频提取在线工具有哪些 网页音频提取在线工具下载
  • 转让无区域商业管理公司挺批行业包变更
  • Windows Server 2008 r2 + NAS
  • 介绍建造者模式
  • Hadoop的Windows环境准备
  • 超详解——识别None——小白篇
  • 『大模型笔记』什么是提示词注入(Prompt Injection)攻击?
  • Java 并发编程中的 synchronized 关键字及其现代优化技术
  • 写在高考之际
  • OpenAI模型规范概览
  • 技术架构的发展
  • dva中组件的懒加载
  • IndexedDB
  • Laravel5.4 Queues队列学习
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • 产品三维模型在线预览
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 如何在 Tornado 中实现 Middleware
  • 如何用纯 CSS 创作一个货车 loader
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​linux启动进程的方式
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • # wps必须要登录激活才能使用吗?
  • # 达梦数据库知识点
  • #DBA杂记1
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • $.proxy和$.extend
  • (1)(1.11) SiK Radio v2(一)
  • (160)时序收敛--->(10)时序收敛十
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (7)摄像机和云台
  • (floyd+补集) poj 3275
  • (k8s)Kubernetes本地存储接入
  • (leetcode学习)236. 二叉树的最近公共祖先
  • (二)WCF的Binding模型
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (一)基于IDEA的JAVA基础1
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • *p++,*(p++),*++p,(*p)++区别?
  • *算法训练(leetcode)第三十九天 | 115. 不同的子序列、583. 两个字符串的删除操作、72. 编辑距离
  • .gitattributes 文件
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET CORE 第一节 创建基本的 asp.net core
  • .Net Core 微服务之Consul(三)-KV存储分布式锁
  • .net Stream篇(六)
  • .net知识和学习方法系列(二十一)CLR-枚举