A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1697    Accepted Submission(s): 959

Problem Description
Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

 
Input
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
 
Output
For each case, output f(k) % m in one line.
 
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
 
Sample Output
45
104
 
Author
linle
 
Source
 
Recommend
lcy
 

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);

|f(10) |       |a0 a1 a2 ...a8 a9|    |f(9)|
| f(9)  |       | 1   0   0 ... 0    0 |    |f(8)|
| .....  |   =  | ..    ...    ...   ...    |    | .. |
| f(2) |        | 0   0   0 ... 0    0|     |f(1)|
| f(1) |   | 0   0   0 ... 1    0|     |f(0)|

另A举证为10*10的举证,如上图。
可以推出:
(f(n),f(n-1),...,f(n-9))^(-1) = A^(n-9)*(f(9),f(8),...,f(0))^(-1)

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; long long k,mod; struct Matrix{
int m[][];
}; Matrix unit,init; void Init(){
memset(init.m,,sizeof(init.m));
for(int i=;i<;i++)
init.m[i][i-]=;
memset(unit.m,,sizeof(unit.m));
for(int i=;i<;i++)
unit.m[i][i]=;
} Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++){
c.m[i][j]=;
for(int k=;k<;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul(a,b);
}
a=Mul(a,a);
x>>=;
}
return b;
} int main(){ //freopen("input.txt","r",stdin); while(cin>>k>>mod){
Init();
for(int i=;i<;i++)
scanf("%d",&init.m[][i]);
if(k<){
cout<<k%mod<<endl;
continue;
}
Matrix res=Pow(init,unit,k-);
int ans=;
for(int i=;i<;i++)
ans+=(res.m[][i]*(-i))%mod;
cout<<ans%mod<<endl;
}
return ;
}

HDU 1757 A Simple Math Problem (矩阵乘法)的更多相关文章

  1. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  2. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  3. HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...

  4. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  6. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  7. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  8. hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...

  9. hdu 1757 A Simple Math Problem (矩阵快速幂)

    Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...

随机推荐

  1. 【BZOJ】【2595】【WC2008】游览计划

    Orz zky神犇http://blog.csdn.net/iamzky/article/details/42029921 spfa的灵活应用!(好像是求了一个叫做斯坦纳树的东西……) o(︶︿︶)o ...

  2. Windows 7系统垃圾清理自写程序

    系统清理.bat @echo off color 0a title windows7系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ...

  3. 第二章 eclipse中m2e插件问题

    1.当引入一个maven项目到eclipse中时,这时候可能会出现找不到一个插件的问题,例如: <plugin> <groupId>org.apache.maven.plugi ...

  4. Android数据解析-JSON解析

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  5. 怎么才能成为一名PHP专家?

    本文作者Bruno Skvorc是一名资深的Web开发者.在这篇文章里主要是讲述成为一名专业的PHP专家所要经历的过程,以及在这个过程里要如何学习掌握技巧和对工具的舍取.(以下为编译内容) 当阅读各种 ...

  6. Ionic 4 and the Lifecycle Hooks

    原文: https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864 ------------------- ...

  7. [转] ssh免密码登录服务器

    1. tencent 同事写的比较靠谱 https://www.chenyudong.com/archives/ssh-using-private-public-key-no-password.htm ...

  8. (转)Unity3D新手引导开发手记

    转自:http://www.cnblogs.com/ybgame/p/3844315.html 最近开始接手新手引导的开发,记录下这块相关的心得 首先客户端是Unity,在接手前,前面的同学已经初步完 ...

  9. DB2数据源在Spring环境中的配置

    简单记录一下,以备不时之需. DB2的java驱动包可以在这里下载:http://pan.baidu.com/s/1gOoEJ DB2数据源的配置如下,粗体字部分是需要根据实际情况修改的: <b ...

  10. 如何使用Total Recorder录制网上的音乐,如何下载只能试听的歌曲

    1 在网上找到了对应的网站.其中正在播放的歌曲正是我们想要的 2 在地址栏输入上面音乐网站的网址,并点击捕获广播.(URL直接给出了音乐的完整地址,比如http://www.someserver.co ...