A Simple Math Problem

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
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF = 0x4fffffff;
17 const double EXP = 1e-5;
18 const int MS = 1005;
19 const int SIZE = 10;
20
21 typedef vector<vector<int> > mat;
22
23 mat matrix(SIZE,vector<int>(SIZE));
24
25 int n,mod;
26
27 mat mul(mat &m1,mat &m2)
28 {
29     mat m(SIZE,vector<int>(SIZE));
30     for(int i=0;i<SIZE;i++)
31         for(int j=0;j<SIZE;j++)
32             for(int k=0;k<SIZE;k++)
33         {
34             m[i][j]=(m1[i][k]*m2[k][j]+m[i][j])%mod;
35         }
36     return m;
37 }
38
39 mat pow(int n)
40 {
41     mat m(SIZE,vector<int>(SIZE));
42     for(int i=0;i<SIZE;i++)
43         m[i][i]=1;
44     while(n)
45     {
46         if(n&1)
47             m=mul(m,matrix);
48         matrix=mul(matrix,matrix);
49         n>>=1;
50     }
51     return m;
52 }
53
54 int main()
55 {
56     while(scanf("%d%d",&n,&mod)!=EOF)
57     {
58         for(int i=0;i<SIZE;i++)
59             for(int j=0;j<SIZE;j++)
60                 matrix[i][j]=(i-1)==j;
61         for(int i=0;i<SIZE;i++)
62             scanf("%d",&matrix[0][i]);
63
64         if(n<10)
65         {
66             printf("%d\n",n%mod);
67             continue;
68         }
69         matrix=pow(n-9);
70         int ans=0;
71         for(int i=0;i<SIZE;i++)
72             ans+=matrix[0][i]*(9-i)%mod;
73         printf("%d\n",ans%mod);
74     }
75 }

转载于:https://www.cnblogs.com/767355675hutaishi/p/4435100.html

A Simple Math Problem 矩阵打水题相关推荐

  1. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...

  3. HDU - 5974 A Simple Math Problem 题解

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

  4. FZYZ-2071 A Simple Math Problem IX

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

  5. 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] ...

  6. A Simple Math Problem题目翻译

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

  7. 1.24A Simple Math Problem

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

  8. Simple Math Problem

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

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

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

最新文章

  1. 数据结构与算法:15 树
  2. laravel框架安装(奶妈式手把手一步步操作)
  3. 6计算机系统的组成是,计算机系统的组成(范文)(6页)-原创力文档
  4. linux 查看进程的信号,Linux 进程信号查看与控制
  5. springboot 2.0 配置 logback
  6. 重磅!华为方舟编译器招募 2000 程序员!
  7. 库克终于承认:苹果滞销因iPhone价高,连累财报即将降价促销
  8. 【数据库】Mean web开发 05-Linux上配置MongoDB自动启动及远程连接
  9. Sendmail 邮件服务器安装和优化
  10. 怎么解除Word2003安全模式打开问题
  11. python分析qq聊天记录
  12. 比较神秘的网址!也许有用!收藏一下!
  13. Word文档插入图片的问题
  14. linux 云主机 管理软件,常用6款云服务器管理软件(上)
  15. esp8266解析php,ESP8266 Bootloader开源代码解析之rboot(一)
  16. 深入理解ArcGIS的地理坐标系、大地坐标系
  17. svn+ssh服务器与客户端配置方法
  18. 五大列级庄_1855列级庄分级
  19. 工业物联网体系架构概述及基于工业物联网的智能制造
  20. 计算机在机械设制造中的应用实例,机械设计与制造毕业设计一体化分析

热门文章

  1. Variables多种表达
  2. 聊聊 Spring Boot 2.x 那些事儿
  3. 部分 DNS 查询因闰秒 bug 报错
  4. 分布式缓存技术memcached学习系列(五)—— memcached java客户端的使用
  5. Oracle 默认表空间(default permanent tablespace) 说明
  6. Java之 == 和 equals() 区别 hashCode() 解析
  7. RabbitMQ原理及实现
  8. python部署到iis效率_IIS 部署 Python Django网站流程(受够了野路子)
  9. UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 723269-723270: illegal multibyte s
  10. 深入理解PHP之源码目录结构