UVa 10870 - Recurrences 矩阵快速幂
构造矩阵.
见《训练指南》p156...
#include <cstdio> #include <cstring> #include <cstdlib>#define LL long long intusing namespace std;const int MAXN = 20;LL mod;struct Matrix {LL a[MAXN][MAXN];int r, c;friend Matrix operator*( Matrix &a, Matrix &b );friend Matrix operator^( Matrix a, LL k ); };Matrix operator*( Matrix &a, Matrix &b ) {Matrix tmp;tmp.r = a.r;tmp.c = b.c;for ( int i = 1; i <= a.r; ++i )for ( int j = 1; j <= b.c; ++j ){tmp.a[i][j] = 0;for ( int k = 1; k <= a.c; ++k ){//printf( "%d %d %d\n", i, j, k );tmp.a[i][j] = ( tmp.a[i][j] + (a.a[i][k] * b.a[k][j]) % mod ) % mod;}}return tmp; }Matrix operator^( Matrix a, LL k ) {Matrix unit;unit.r = a.r;unit.c = a.c;memset( unit.a, 0, sizeof(unit.a) );for ( int i = 0; i < MAXN; ++i )unit.a[i][i] = 1;while ( k ){if ( k & 1 ) unit = unit * a;a = a * a;k = ( k >> 1 );}return unit; }/**************以上矩阵快速幂模板*****************/int n, d; Matrix A, F;int main() {//freopen( "in.txt", "r", stdin );while ( scanf( "%d%d%lld", &d, &n, &mod ), d || n || mod ){memset( A.a, 0, sizeof(A.a) );for ( int i = 1; i < d; ++i )A.a[i][i + 1] = 1;for ( int i = d; i > 0; --i ){scanf( "%lld", &A.a[d][i] );A.a[d][i] %= mod;}A.r = d, A.c = d;memset( F.a, 0, sizeof(F.a) );for ( int i = 1; i <= d; ++i ){scanf( "%lld", &F.a[i][1] );F.a[i][1] %= mod;}F.r = d, F.c = 1;A = A ^ (n - d);F = A * F;printf( "%lld\n", F.a[d][1] % mod );}return 0; }
转载于:https://www.cnblogs.com/GBRgbr/p/3302532.html
UVa 10870 - Recurrences 矩阵快速幂相关推荐
- UVA 10870 Recurrences 矩阵快速幂
题意:给你递推公式要求矩阵快速幂的第n项 解题思路: 经典的矩阵快速幂递推第n项目做法. 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 ...
- 矩阵快速幂2Jzzhu and Sequences Recurrences Contemplation! Algebra Reading comprehension
来!接着写题解,立个flag:矩阵快速幂题解不写完今晚不睡觉! 此题解是接着矩阵快速幂1写的,此篇有哪里写不清楚的地方导致看不懂的可以先看1:https://blog.csdn.net/qq_4536 ...
- 矩阵快速幂+构造方法
与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...
- 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂
原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...
- 快速幂 + 矩阵快速幂
快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...
- HDU4549(矩阵快速幂+快速幂)
f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...
- [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]
解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...
- I-Matrix Power Series POJ - 3233 矩阵快速幂+分治
I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- HDU 6185 Covering 矩阵快速幂 递推
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...
最新文章
- DELL R710 服务器内存排错
- WPF 路由事件初步
- Leetcode 62.不同路径 (每日一题 20210701)
- How to suppress email sending when operating on Activity
- android 进度条_Android仿水波纹流球进度条控制器,实现高端大气的主流特效
- MFC创建模式对话框与非模式对话框
- python环境变量配置步骤_关于人工智能Python系统环境变量设置步骤
- 阿里巴巴公布了一份最新的AI成绩单
- .net开发MIS系统的难处.
- 【项目篇】Android团队项目开发之统一代码规范
- php跳转qq临时会话,怎样做出在网页中弹出QQ临时对话框
- java设置word图片居中_Word中怎样快速对齐图片?这样操作,效率提升10倍
- python-opencv第四期:threshold函数详解
- 微博三方登录账号注册
- python 100days github_GitHub - 2668599092/Python-100-Days: Python - 100天从新手到大师
- php-ews发送邮件,使用php-ews回复电子邮件
- CPU设计——CPU核解析——cv32e40p
- PLC是怎么工作和扫描的
- EasyPoi的简介
- layui动态表格下拉菜单
热门文章
- 微信小程序云开发教程-微信小程序的JS基础-js文件基本结构与生命周期
- excel日期改成字符类型_EXCEL的一些基础知识
- Vuforia3D模型上传
- 内的图标_从零开始画图标系列:线性图标设计实战演示!
- 微信小程序底部突起半圆设计
- 使用wsimport构建WebService客户端
- 操作系统——I/O设备
- 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_08.RequestMapping注解的作用
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_4_字节输出流写入数据到文件...
- 蓝桥杯 算法训练 数字三角形