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. 学习日记day9: PC端页面流程优化

    <!DOCTYPE html><html lang="en"><head> <meta charset="gb2312" ...

  2. Java字符串处理函数

    substring() 它有两种形式,第一种是:String substring(int startIndex)第二种是:String substring(int startIndex,int end ...

  3. Android 自定义列表指示器

    在联系人界面 可以看到这种界面 手指快速滑动右边滑动条时 可以显示相应的字母图标 android里提供了android.widget.SectionIndexer这个接口去实现该效果 可是只能显示字母 ...

  4. Jq_文档操作方法、属性操作方法、CSS操作函数

    JQuery文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法                            描述 addClass()      ...

  5. HBase启动和停止命令

    启动HBase集群: bin/start-hbase.sh 单独启动一个HMaster进程: bin/hbase-daemon.sh start master 单独停止一个HMaster进程: bin ...

  6. json遍历key value

    http://blog.csdn.net/lanshengsheng2012/article/details/17679487 public static void main(String[] arg ...

  7. 删除内容并不能删除field structure -- features_revert

    把内容删了但field structure还在, 在manage_field界面,field还在.http://drupal.stackexchange.com/questions/21501/rev ...

  8. 让你的linux操作系统更加安全【转】

    BIOS安全 记着要在BIOS设置中设定一个BIOS密码,不接收软盘启动.这样可以阻止不怀好意的人用专门的启动盘启动你的Linux系统,并避免别人更改BIOS设置,如更改软盘启动设置或不弹出密码框直接 ...

  9. MySQL中基本的多表连接查询教程

    一.多表连接类型1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用','  如: SELECT * FROM table1 CROSS JOIN ...

  10. python 练习 16

    #!/usr/bin/python # -*- coding: UTF-8 -*- import time myD = {1: 'a', 2: 'b'} for key, value in dict. ...