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
2007省赛集训队练习赛(6)_linle专场
Recommend
lcy   |   We have carefully selected several similar problems for you:  1588 3117 2276 2256 2604 

下面我们研究一下这道题如何运用矩阵。

假设我们已有一个1*10的矩阵f0 f1 f2...f9 只要将其乘以一下矩阵,就可以推导至矩阵f1 f2 f3 ...f10:
                    0 0 0 0 0 0 0 0 0 a(9)
                           1 0 0 0 0 0 0 0 0 a(8) 
                           0 1 0 0 0 0 0 0 0 a(7) 
                           0 0 1 0 0 0 0 0 0 a(6) 
                           0 0 0 1 0 0 0 0 0 a(5) 
                           0 0 0 0 1 0 0 0 0 a(4)
                           0 0 0 0 0 1 0 0 0 a(3)
                           0 0 0 0 0 0 1 0 0 a(2)
                           0 0 0 0 0 0 0 1 0 a(1) 
                           0 0 0 0 0 0 0 0 1 a(0)  
     设这个矩阵为D。

f1,f2,f3...f10再乘以这个矩阵就可以推导至f2,f3,f4...f11。以此类推,递推的过程就变成了不断乘以这个矩阵的过程。这样,如果我们要得到fk,我们就得将初始矩阵(f0,f1,f2...f10)乘以D^(k-9),从而得出目标矩阵(目标矩阵的最后一项即为所求)。而D^(k-9)我们可以用10^3*log(k-9)的时间计算出来,此题圆满解决。

//看懂这个题解之后就可以知道以后再给出这样的就该怎么地推求出矩阵表达式

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
struct node
{long long martix[15][15];
}a,e;
int k,m;
void init()
{memset(a.martix,0,sizeof(a.martix));memset(e.martix,0,sizeof(e.martix));for(int i=0;i<10;++i)e.martix[i][i]=1;for(int i=9;i>=0;--i){cin>>a.martix[i][9];if(i>=1)a.martix[i][i-1]=1;}
}
node POW(node a,node b)
{node ans;for(int i=0;i<10;++i)for(int j=0;j<10;++j){ans.martix[i][j]=0;for(int k=0;k<10;++k){if(a.martix[i][k]&&b.martix[k][j]){ans.martix[i][j]+=(a.martix[i][k]%m*b.martix[k][j]%m)%m;}}}return ans;
}
void martix_pow(int x)
{while(x){if(x&1)e=POW(e,a);a=POW(a,a);x>>=1;}
}
int main()
{while(cin>>k>>m){init();martix_pow(k-9);//long long sum=0;for(int i=0;i<10;++i){sum+=e.martix[i][9]*i%m;}cout<<sum%m<<endl;//注意最后还要%一次,}return 0;
}

hdu A Simple Math Problem相关推荐

  1. HDU - 5974 A Simple Math Problem 题解

    D - Simple Math Problem 戳上方进入原题哟~ 题目大意 给一个数 a,b. 让你求满足一下条件的 X,Y: X+Y = a LCM(X,Y) = b 解析 题目给出时间限制1s, ...

  2. FZYZ-2071 A Simple Math Problem IX

    P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论    ...

  3. A Simple Math Problem(2020 ICPC 江西省省赛)

    A Simple Math Problem ∑i=1n∑j=1if(j)[gcd(i,j)=1]∑i=1n∑j=inf(i)[gcd(i,j)=1]∑i=1n∑j=1nf(i)[gcd(i,j)=1] ...

  4. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x ...

  5. A Simple Math Problem题目翻译

    A Simple Math Problem | JXNUOJhttps://acs.jxnu.edu.cn/problem/ICPCJX2020A 描述: Huanhuan challenges yo ...

  6. 1.24A Simple Math Problem

    原网址:https://acs.jxnu.edu.cn/problem/NOIOPJCH02077215 描述: Huanhuan challenges you to a simple math pr ...

  7. Simple Math Problem

    Simple Math Problem | JXNUOJ 翻译: 描述: 通通又在玩数学游戏.她已经多次遇到这个方针了,所以不用说,她知道你说的是什么.现在她将这个问题推给你.你需要观察下面的方阵,然 ...

  8. HDU 6607 Easy Math Problem(杜教筛 + min_25 + 拉格朗日插值)

    Easy Math Problem 推式子 ∑i=1n∑j=1ngcd(i,j)Klcm(i,j)[gcd(i,j)∈prime]∑i=1n∑j=1ngcd(i,j)K−1ij[gcd(i,j)∈pr ...

  9. HDU 1757 A Simple Math Problem

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

最新文章

  1. 第十七届智能车竞赛英飞凌专题培训 ——英飞凌无刷电机驱动专题
  2. Spring-mybatis 抽取 baseDao。
  3. 汇编语言的准备知识--给初次接触汇编者 (1-4) 转载
  4. HTML5实战——canvas 绘制钟表
  5. Pocket Gem OA: Path Finder
  6. PHPstudy配置局域网
  7. Linux参考资料名称,Linux系统管理员必备参考资料下载汇总
  8. MLX90614红外测温传感器使用arduino采集温度数据OLED显示
  9. Oracle数据库上机练习6
  10. 【RPA之家BluePrism手把手教程】BluePrism下载与安装
  11. 宽带显示不能建立到远程计算机,宽带连接错误820:不能建立到远程计算机的连接 正确处理办法...
  12. 人脸识别技术入驻了哪些行业
  13. 熬夜读书最好吃什么东西补充能量类``
  14. Web前端技术基础实验报告二之格式化文本、段落与列表
  15. Java基于springboot+vue的房屋出租租房系统 前后端分离
  16. 【python制作小游戏】大鼻子马里奥等你来挑战,还原度超高哦~
  17. sas模拟试验构建线性模型_在sas上构建一个有角度的Web应用程序
  18. 实战:怎么比较暂存区和HEAD所含文件的差异?-2021.11.19
  19. java版的短信接口封装,免费送给你,自带200条短信
  20. web前端开发真的会持续高薪吗?(赠超实用前端干货大合集,含特效源码)

热门文章

  1. TC SRM 562 div2 B 题
  2. poj 2418 Hardwood Species
  3. WINCE6.0文件系统及存储管理器
  4. 企业网站制作要以简约为主
  5. Android6.0以上的权限问题
  6. centos7 apache2.4 多站点配置
  7. memcache的windows下的安装和简单使用
  8. [译]Spring Session 与 Spring Security
  9. div根据内容改变大小并且左右居中
  10. 【沟通之道】头脑风暴-女人的心思你别猜