Uva Recurrences 矩阵优化

演算法 - 分治法(Divide-and-Conquer) 排版真的乱到一个 ¥*@#&?一,Divide-and-Conquer(fenzhifa)二,Recurrences(递归)1,替代法(Substitution method)2,Tree Method3,The Master Method三,Divide-and-Conquer使用時機四,遞迴演算法則的設計1,Binary Search (二分搜尋)五,总结1,替代法(Substitut... 阅读详情

Consider recurrent functions of the following form:

f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + … + adf(n − d), for n > d,
where a1, a2, … , ad are arbitrary constants.
A famous example is the Fibonacci sequence, defined as: f(1) = 1, f(2) = 1, f(n) = f(n − 1) +
f(n − 2). Here d = 2, a1 = 1, a2 = 1.
Every such function is completely described by specifying d (which is called the order of recurrence),
values of d coefficients: a1, a2, … , ad, and values of f(1), f(2), … , f(d). You’ll be given these numbers,
and two integers n and m. Your program’s job is to compute f(n) modulo m.
Input
Input file contains several test cases. Each test case begins with three integers: d, n, m, followed by
two sets of d non-negative integers. The first set contains coefficients: a1, a2, … , ad. The second set
gives values of f(1), f(2), … , f(d).
You can assume that: 1 ≤ d ≤ 15, 1 ≤ n ≤ 2
31 − 1, 1 ≤ m ≤ 46340. All numbers in the input will
fit in signed 32-bit integer.
Input is terminated by line containing three zeroes instead of d, n, m. Two consecutive test cases
are separated by a blank line.
Output
For each test case, print the value of f(n)( mod m) on a separate line. It must be a non-negative integer,
less than m.
Sample Input
1 1 100
2
1
2 10 100
1 1
1 1
3 2147483647 12345
12345678 0 12345
1 2 3
0 0 0
Sample Output
1
55
423

题意:给出公式f(n) = a1*f(n-1) + a2*f(n-2) + … + ad*f(n-d);题意已知n, m, d, ai和f(1)~f(d);
计算最后f(n)%m.

解题思路:
1.
f(n) = a1*f(n-1) + a2*f(n-2) + … + ad*f(n-d); 拆分成矩阵ax=b形式;
计算得: [ad, ad-1, ad-2, …, a2, a1] (行向量) * [f(n-d), …, f(n-1)] (列向量) = f(n);
2.
A*F(n-1) = F(n)有下列式子
0 1 0 … 0 0 f(n-d-1) f(n-d)
0 0 1 … 0 0 … …
…….. * … = …
0 0 0 … 0 1 f(n-2) f(n-1)
ad ad-1 .… a2 a1 f(n-1) f(n)
3.
F(n) =A*F(n-1), F(n-1) = A*F(n-2), …. , F(d+1)=A*F(d);

矩阵优化,ac代码如下:

///@zhangxiaoyu
///2015/8/14

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long LL;
const LL maxn=16;

LL d;
LL a[maxn];
LL f[maxn];
struct matrix{
   LL arr[maxn][maxn];
}Unit;

matrix mul(matrix a,matrix b,LL n,LL mod)
{
    LL i,j,k,temp;
    matrix val;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            temp=0;
            for(k=1;k<=n;k++)
            {
                temp=(temp+(a.arr[i][k]%mod*b.arr[k][j]%mod)%mod)%mod;
                val.arr[i][j]=temp;
            }
        }
    }
    return val;
}

matrix matrixPow(matrix a,LL n,LL m,LL mod)
{
    matrix res=Unit,temp;
    temp=a;
    for(;m;m/=2)
    {
        if(m&1)
            res=mul(res,temp,n,mod);
        temp=mul(temp,temp,n,mod);
    }
    return res;
}

void init(LL n)
{
    LL i;
    memset(Unit.arr,0,sizeof(Unit.arr));
    for(i=1;i<=n;i++)
        Unit.arr[i][i]=1;
}

int main()
{
    LL m,n,i,j;
    while(~scanf("%lld%lld%lld",&d,&n,&m))
    {
        if(d==0&&n==0&&m==0)
            break;
        LL ans=0;
        matrix maze;
        matrix temp;
        memset(temp.arr,0,sizeof(temp.arr));
        memset(maze.arr,0,sizeof(maze.arr));
        init(d);
        for(i=1;i<=d;i++)
        {
            scanf("%lld",&a[i]);
        }
        for(i=1;i<=d;i++)
        {
            scanf("%lld",&f[i]);
        }
        for(i=1;i<=d-1;i++)
        {
            j=i+1;
            maze.arr[i][j]=1;
        }
        for(i=1;i<=d;i++)
        {
            maze.arr[d][i]=a[d-i+1];
        }
      /*  for(i=1;i<=d;i++)
        {
            for(j=1;j<=d;j++)
            {
                printf("%I64d ",maze.arr[i][j]);
            }
            puts("");
        }
       */
        temp=matrixPow(maze,d,n-d,m);
        ///prLLf("%d\n",temp.arr[1][1]);
        /*
        for(i=1;i<=d;i++)
        {
            for(j=1;j<=d;j++)
            {
                printf("%I64d ",temp.arr[i][j]);
            }
            puts("");
        }
        */
        for(i=1;i<=d;i++)
        {
            ans+=(temp.arr[d][i]*f[i]%m);
            ans%=m;
        }
        printf("%lld\n",ans%m);
    }
    return 0;
}
思科华三设备组网思科生成树PVST发BPDU导致全网闪断 一、故障现象 1.思科PVST BPDU报文导致全网闪断。 2.光衰低时不同品牌网络设备无法兼容导致接入交换机中断。 二、网络拓扑 图1 网络拓扑图 组网结构为H3C汇聚,接入交换机有思科,也有H3C,而且与汇聚直连的接入交换机下面串接了较多的交换机,或者hub。 三、排查思路 1.回忆一下最近做了什么事情,什么时候开始出的问题。 将网络汇聚交换机从思科换为H3C,H3C汇聚和思科接入对接出问题... 阅读详情

相关推荐

C#读取CAD文件(dwg/dxf)并处理

项目要求识别CAD图纸(图纸内容与现实事物比例是1:1)中的内容,并提取出一些关键信息。这里的CAD图纸是指CAD文件,包含多种格式,dwg/dxf/dwf/stl/pdf等等。识别其中内容,主要指文件中的图元信息(也叫实体Entity),指线段、圆弧、矩形等图形元素。得到图元基础信息(比如长度、坐标、粗细)后,进一步做处理(如得到交点、得到线段间间隔、圆弧与线段切点等)。听起来有点难,因为作为软件工程师,对CAD相关事物并不了解,它是未知的;

三千幻想乡 1万+

UVA 10870 Recurrences 矩阵乘法

Consider recurrent functions of the following form:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + … + adf(n − d), for n > d, where a1, a2, … , ad are arbitrary constants. A famous example is the F

ShineRoyal 963

基于车辆运动学的规划算法 - Hybrid A*/ State Lattice Planning (前置知识:自行车模型、Dubins、Reeds Shepp、多项式曲线、螺旋曲线)

本文将按顺序讲解基于车辆运动学的规划算法,以及需要的前置知识:自行车模型、Dubins和Reeds Shepp曲线、多项式曲线和螺旋曲线

BigDavid123的博客 4872

UVA - 10870 Recurrences(构造矩阵)

传送门Consider recurrent functions of the following form: f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + … + adf(n − d), for n > d, where a1, a2, … , ad are arbitrary constants. A famous example is the

代码改变世界 926

从V形槽到光模块:深度解析光纤阵列FA的精密制造与核心工艺

本文深度解析光纤阵列FA的精密制造与核心工艺,从V形槽基板材料选择到纳米级开槽工艺,再到光纤排布与端面处理技术。特别探讨了高密度型FA在光通信中的应用,以及如何通过自动对准技术和可靠性验证确保产品性能。文章还分享了湿法刻蚀等关键工艺参数,为光模块制造提供实用参考。

weixin_30423977的博客 599

Quartus原理图设计入门:从半加器到4位全加器的保姆级教程

本文提供Quartus原理图设计入门教程,详细讲解从半加器到4位全加器的实现过程。通过原理图输入法,逐步构建数字电路设计基础模块,涵盖环境搭建、逻辑设计、层次化实现及功能验证等关键步骤,帮助FPGA初学者快速掌握Quartus工具链和加法器设计方法。

weixin_29291185的博客 474

UVA - 10870 Recurrences

Consider recurrent functions of the following form: f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d, where a1, a2, . . . , ad are arbitrary constants. A famous example i

Lngxling的博客 329

MATLAB新手必看:5分钟搞定直流无刷电机Simulink仿真(附完整模型下载)

本文为MATLAB新手提供直流无刷电机Simulink仿真的快速入门指南,涵盖从环境配置到速度控制闭环实现的完整流程。通过详细的模块选择和参数设置说明,帮助读者5分钟内搭建基础仿真模型,并附赠完整模型下载,助力快速掌握电机控制仿真技术。

efc123456的博客 992

Uva 10870 Recurrences 解题报告(矩阵快速幂)

Problem A Recurrences Input: standard input Output: standard output Consider recurrent functions of the following form: f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), f

SF-_- 的 ACM 博客 871

【第三只眼:Oracle通过归档日志查看系统的业务高峰sql】

根据oracle归档的业务高峰量,查找业务sql,进行优化

霍然的专栏 583

Linux下搭建简易的HTTP服务器完成图片显示

篇文章作为Linux下socket(TCP)网络编程的练习,使用C语言代码搭建一个简单的HTTP服务器,完成与浏览器之间的交互,最终在浏览器上显示一张图片;通过这个例子可以巩固socket里多线程使用,也可以方便学习了解HTTP协议。

2593

uva 10870 Recurrences(矩阵优化递推)

uva 10870 Recurrences 比较裸的矩阵

yjCola 581

CloudCompare 二次开发(6)——插件中拖拽添加Qt窗口(区域生长算法为例)

手动拖拽的方式搭建Qt对话框界面的制作流程,以PCL中的点云区域生长算法为例进行制作。

点云侠的博客 2155

UVA 10870 Recurrences(构造矩阵和快速幂)

题目链接: UVA 10870 Recurrences 题意: 给出递推式: f(n)=a1∗f(n−1)+a2∗f(n−2)+a3∗f(n−3)+...+ad∗f(n−d),n>df(n)=a1*f(n-1)+a2*f(n-2)+a3*f(n-3)+...+ad*f(n-d),n>d 其中a1,a2,a3...ada1,a2,a3...ad以及f(1),f(2),f(3)...f(3)f

ramay7 374

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

UVA 10870 - Recurrences(矩阵快速幂)

UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. 已知前d项求第n项 思路:矩阵快速幂,对应矩阵为 |a1 a2 a3 ... ad| |1 0 0 ... 0 0 0| |0 1 0 ... 0 0

Remilia's 1249

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

UVa 10870 Recurrences (矩阵快速幂)

UVA 10870 Recurrences题目大意:f(n)=a1f(n−1)+a2f(n−2)+a3f(n−3)+...+adf(n−d)f(n)=a_{1}f(n-1)+a_{2}f(n-2)+a_{3}f(n-3)+...+a_{d}f(n-d). 给出a1a_{1}~ada_{d},f(1)f(1)~f(d)f(d),nn,mm,求f(n)%mf(n)\%m. (1≤d≤15,1≤n≤2

Continue 603

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

UVA10870 Recurrences —— 矩阵快速幂

题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用。比一般的斐波那契数推导式多了几项而已。 代码如下: 1 #include <bits/stdc++.h> 2 #define rep(i,s,t) for(int (i)=(s); (i)&l...

alince20008的博客 156

UVa 10870 - Recurrences 矩阵快速幂

构造矩阵. 见《训练指南》p156... #include <cstdio> #include <cstring> #include <cstdlib> #define LL long long int using namespace std; const int MAXN = 20; LL mod; stru...

aegt43853的博客 151

UVA 10870 - Recurrences(矩阵高速功率)

UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n &gt; d. 已知前d项求第n项 思路:矩阵高速幂,相应矩阵为 |a1 a2 a3 ... ad| |1 0 0 ... 0 0 0| |0 1 0...

weixin_33768481的博客 92

【矩阵快速幂】Recurrences UVA - 10870

Think: 1知识点:矩阵快速幂 2题意: 现输入d, n, m求解f(n) 注:f(i) = f(i) mod m; 3题意思考: (1):写出系数矩阵,矩阵快速幂求解 4反思: (1):注意系数矩阵与初始序列的对应关系 (2):输入数据中每组测试数据换行相隔,而并不是要求在输出中每组测试数据以换行相隔,注意读题的严谨,注意细节以下为Accepted代码#include <cs

BlessingXRY的博客 473

UVA - 10870 Recurrences 【矩阵快速幂】

题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 n 很大 自然想到是 矩阵快速幂 那么问题就是 怎么构造矩阵 我们想到的一种构造方法是 n = 2 时 n = 3 时 ...

dianxi0949的博客 127

uva 10870 - Recurrences(矩阵快速幂)

题目链接:uva 10870 - Recurrences 题目大意:考虑线性递推,给出递推方程,求f(n)%m. 解题思路:矩阵快速幂。 #include #include #include using namespace std; const int maxsize = 100; typedef long long ll; typedef long long type;

不慌不忙、不急不躁 998

uva 10870 Recurrences(数学:矩阵快速幂)

给出一个递推关系,让你求关系中德di

小华在努力~ 564

uva10870 Recurrences矩阵快速幂

点击打开链接 f[n]=a1*f[n-1]+a2*f[n-2]+...+ad*f[n-d];      由于n太大,不能直接递推,需要用矩阵快速幂来解决,时间复杂度为O(d^3logn)      举例,d=5的矩阵关系式为:                  |a1 a2 a3 a4 a5|    | f[n]   |   | f[n+1] |

Every day 452
上一篇: Hdoj 1019 Least Common Multiple
下一篇: Uva 10341 Solve It(数论)
just_sort
博客等级 码龄11年 2360粉丝 1667原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值