http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811

矩阵快速幂

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<stdexcept>
#include<bitset>
#include<cassert>
#include<deque> using namespace std; typedef long long ll;
typedef unsigned int uint;
const double eps=1e-12;
const int INF=0x3f3f3f3f;
const int N=50;
ll ma[N][N],mb[N][N],mc[N][N];
void matrixMul(ll a[][N],ll b[][N],int n,int m,int k,ll MOD)
{
memset(mc,0,sizeof(mc));
for(int i=1;i<=n;++i)
for(int j=1;j<=k;++j)
for(int l=1;l<=m;++l)
mc[i][j]=(mc[i][j]+a[i][l]*b[l][j]%MOD)%MOD;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
a[i][j]=mc[i][j];
}
int main()
{
//freopen("data.in","r",stdin);
int d,n;
ll m;
while(cin>>d>>n>>m)
{
if(!d&&!n&&!m) break;
memset(mb,0,sizeof(mb));
for(int i=1;i<=d;++i)
cin>>mb[i][1];
for(int i=2;i<=d;++i)
mb[i-1][i]=1;
for(int i=d;i>=1;--i)
cin>>ma[1][i];
if(n<=d)
{
cout<<ma[1][d+1-n]<<endl;
continue;
}
n-=d;
while(n)
{
if((n&1))
matrixMul(ma,mb,1,d,d,m);
n=n>>1;
matrixMul(mb,mb,d,d,d,m);
}
cout<<ma[1][1]<<endl;
}
return 0;
}

UVa 10870 - Recurrences的更多相关文章

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

    UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...

  2. 矩阵快速幂 UVA 10870 Recurrences

    题目传送门 题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m.训 ...

  3. UVA 10870 Recurrences(矩阵乘法)

    题意 求解递推式 \(f(n)=a_1*f(n-1)+a_2*f(n-2)+....+a_d*f(n-d)\) 的第 \(n\) 项模以 \(m\). \(1 \leq n \leq 2^{31}-1 ...

  4. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

  5. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  6. UVa 10870 (矩阵快速幂) Recurrences

    给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...

  7. Recurrences UVA - 10870 (斐波拉契的一般形式推广)

    题意:f(n) = a1f(n−1) + a2f(n−2) + a3f(n−3) + ... + adf(n−d), 计算这个f(n) 最重要的是推出矩阵. #include<cstdio> ...

  8. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  9. UVA - 10870 UVA - 10870

    Problem ARecurrencesInput: standard inputOutput: standard output Consider recurrent functions of the ...

随机推荐

  1. poj1696Space Ant(逆时针螺旋形)

    链接 贪心做法,没次找最外面的点,也就是相对前面那条线偏转角度最小的点,除第一个点需要找到最下面的点即Y坐标最小,其余的每次进行极角排序. #include <iostream> #inc ...

  2. postgresql如何实现group_concat功能

    MySQL有个聚集函数group_concat, 它可以按group的id,将字段串联起来,如 表:id name---------------1 A2 B1 B SELECT id, group_c ...

  3. Forbidden You don't have permission to access / on this server. You don't have permission to access /phpmyadmin/ on this server. 解决办法

    Forbidden  You don't have permission to access / on this server.   解决办法 打开 httpd.conf 文件, 将 #   onli ...

  4. 使用bufferevent进行libevent服务端和客户端的开发

    参考了网上的一些例子,实验了基于bufferevent的开发. 首先是服务端: #include <netinet/in.h> #include <sys/socket.h> ...

  5. C#_DataTable导出Execl为自定义标题

    public bool ExportExcel(DataTable tb, string path, string tbName) { //excel 2003格式 string connString ...

  6. hiho_100_八数码

    题目大意 8数码问题,在3x3的矩阵中填入0-8九个数字,0可以和它相邻的数字进行交换.从初始状态到达状态F(3x3的方格从上到下,从左到右,形成的数字串为123456780)所需要最少移动的次数. ...

  7. php 新特性

    PHP 5.6 1.可以使用表达式定义常量 https://php.net/manual/zh/migration56.new-features.php 在之前的 PHP 版本中,必须使用静态值来定义 ...

  8. ajax 跨域解决 网上资料

    PHP中运用jQuery的Ajax跨域调用实现代码,需要的朋友可以参考下   可以在页面定义一个调用方法,如下: 复制代码代码如下: function getData(){ $.getJSON(&qu ...

  9. OpenCV3编程入门笔记(5)重要章节小节及核心函数

  10. listview去掉底部多出的边框黑色

    listview去掉底部多出的边框黑色 android:fadingEdge="none" //去掉listview黑色底边 listview.setDivider(null);