题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340)。a1 , a2 ..... ad。f(1), f(2) ..... f(d),求 f(n) = a1*f(n-1) + a2*f(n-2) +....+ ad*f(n-d),计算f(n) % m。

析:很明显的矩阵快速幂,构造矩阵,

,然后后面的就很简单了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 20 + 10;
const int maxm = 1e6 + 2;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Matrix{
int a[15][15], n;
void init(){ ms(a, 0); }
void toOne(){ FOR(i, n, 0) a[i][i] = 1; }
Matrix operator * (const Matrix &rhs){
Matrix res; res.n = n; res.init();
FOR(i, n, 0) FOR(j, n, 0) FOR(k, n, 0)
res.a[i][j] = (res.a[i][j] + (LL)a[i][k] * rhs.a[k][j]) % m;
return res;
}
}; Matrix fast_pow(Matrix x, int n){
Matrix res; res.n = x.n; res.init(); res.toOne();
while(n){
if(n&1) res = res * x;
x = x * x;
n >>= 1;
}
return res;
} int main(){
int d;
while(scanf("%d %d %d", &d, &n, &m) == 3 && n+m+d){
Matrix x, y; x.init(); y.init();
x.n = y.n = d;
for(int i = 0; i < d; ++i){
scanf("%d", &y.a[i][0]);
y.a[i][0] %= m;
}
for(int i = d-1; i >= 0; --i){
scanf("%d", &x.a[0][i]);
x.a[0][i] %= m;
}
if(n <= d){ printf("%d\n", x.a[0][d-n]); continue; }
for(int i = 0; i + 1 < d; ++i) y.a[i][i+1] = 1;
Matrix ans = x * fast_pow(y, n - d);
printf("%d\n", ans.a[0][0]);
}
return 0;
}

  

UVa 10870 Recurrences (矩阵快速幂)的更多相关文章

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

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

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

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

  3. UVA10870 Recurrences —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...

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

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

  5. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  6. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  7. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  8. UVA - 11149 (矩阵快速幂+倍增法)

    第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...

  9. UVA10870—Recurrences(简单矩阵快速幂)

    题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...

随机推荐

  1. selenium验证码和错误截图

    验证码的识别: 1,破解验证码 OCR识别(一般使用tesseract-ocr) 人工智能(AI机器学习 TensorFlow,成本大) 2,绕过验证码 1, 让开发人员临时关闭验证码 2,提供万能验 ...

  2. swift - 16进制颜色扩展(1.支持# 2.支持不带# , 3支持带0X)

    /** * 设置16进制颜色: * 可识别类型 * 1:有# * 2:没有# * 3:含有0X */ extension UIColor{ class func hexadecimalColor(he ...

  3. swift - 听云监测(Testin - Bugout) - 集成

    听云的SDK集成放的太难找了,官方demo到现在也没找到.. 我找了半天没找到在哪,看下面俩链接吧, 切记:添加的三方库 以文档为主,视频里的三方库 不一定正确 iOS SDK 集成:https:// ...

  4. day 08 函数

    函数初始: 什么是函数? 函数:是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段 一个函数封装一个功能. 1,减少重复代码. 2,增加代码的可读性. 函数的结构 def my_len(): ...

  5. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

  6. initialize flexnet service failed error code 50003

    网络上下载回来的绿色版Xshell/Xftp在每次启动时都会报这个错,通过FlexNet Licensing Service 安装与卸载脚本了解到,程序 启动的时候会检查FlexNet Licensi ...

  7. js去除字符串空格(空白符)

    使用js去除字符串内所带有空格,有以下三种方法: ( 1 ) replace正则匹配方法 去除字符串内所有的空格:str = str.replace(/\s*/g,""); 去除字 ...

  8. VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战(原创)

    VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战 1.给指定项目安装Newtonsoft.Json ,Version 4.5.11 PM> Install-Package Ne ...

  9. 洛谷1066 2^k进制数

    原题链接 大力猜结论竟然猜对了.. 对于一对\(k,w\),我们可以把\(w\)位划分成\(k\)位一段的形式,每一段就是转换成十进制后的一位,这个从题面的解释中应该可以理解. 先不考虑可能多出(即剩 ...

  10. Android资源相关语法

    2018-08-29 getResources().getString(Rid)获取配置中的字符串