hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2791 Accepted Submission(s): 1659
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
代码:
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 #define LL __int64
5 using namespace std;
6 const int maxn=10;
7 LL k,m;
8 int aa[maxn],mat[maxn][maxn];
9 int ans[maxn][maxn];
10
11 void init()
12 {
13 for(int i=0;i<10;i++)
14 {
15 for(int j=0;j<10;j++)
16 {
17 if(i==0)
18 mat[i][j]=aa[j];
19 else
20 if(i==j+1)mat[i][j]=1;
21 else mat[i][j]=0;
22 if(i==j)
23 ans[i][j]=1;
24 else ans[i][j]=0;
25 }
26 }
27 }
28
29 void Matrix(int a[][10],int b[][10])
30 {
31
32 int c[10][10];
33 for(int i=0;i<10;i++)
34 {
35 for(int j=0;j<10;j++)
36 {
37 c[i][j]=0;
38 for(int k=0;k<10;k++)
39 {
40 c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
41 }
42 }
43 }
44 for(int i=0;i<10;i++)
45 {
46 for(int j=0;j<10;j++)
47 {
48 a[i][j]=c[i][j];
49 }
50 }
51 }
52
53 void pow(LL n)
54 {
55 while(n>0)
56 {
57 if(n&1) Matrix(ans,mat);
58 n>>=1L;
59 if(n==0)break;
60 Matrix(mat,mat);
61 }
62 }
63
64 int main()
65 {
66 #ifdef LOCAL
67 freopen("test.in","r",stdin);
68 #endif
69
70 while(scanf("%I64d%I64d",&k,&m)!=EOF)
71 {
72 for(int i=0;i<10;i++)
73 scanf("%d",aa+i);
74 if(k<10) printf("%I64dn",k%m);
75 else
76 {
77 init();
78 pow(k-9);
79 int res=0;
80 for(int i=0;i<10;i++)
81 res=(res+(10-i-1)*ans[0][i])%m;
82 printf("%dn",res);
83 }
84 }
85 return 0;
86 }
- 保存数据返回后可以继续编辑
- 深度学习Matlab工具箱代码详解概览
- android读取sd卡图片并进行缩放操作
- 深度学习Matlab工具箱代码注释之cnnapplygrads.m
- android异步操作
- 深度学习Matlab工具箱代码注释之cnnbp.m
- 仿大众点评悬浮购买框效果
- 深度学习Matlab工具箱代码注释之cnnff.m
- 图片缩放,拖拽等操作
- 深度学习Matlab工具箱代码注释之cnntrain.m
- 深度学习Matlab工具箱代码注释之cnnsetup.m
- 深度学习Matlab工具箱代码注释之MnistTest.m
- 如何以“正确的姿势”阅读开源软件代码
- android 二维码简化,实现仿qq扫描效果
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 踩坑ThinkPHP5之模型对象返回的数据集如何转为数组
- PHP基础编程之鬼斧神工的正则表达式-正则表达式基本语法+简单实例
- PHP+mysql数据库简单分页实例-sql分页
- PHP基础编程之鬼斧神工的正则表达式-正则表达式初探+常用的正则表达式函数
- php基础编程-php连接mysql数据库-mysqli的简单使用
- (数据科学学习手札91)在Python中妥善使用进度条
- MySQL 数据库表分区.
- RMI 远程方法调用
- MyBatis版本升级导致OffsetDateTime入参解析异常问题复盘
- 我靠(call) ,我的未来(Future)在哪里???
- Flutter中ListView加载图片数据的优化
- Python 实现随机森林预测宽带客户离网(附源数据与代码)
- 嘿,你知道新冠疫情现在什么样了吗?
- 关于双目立体视觉的三大基本算法及发展现状的总结
- 权限维持之打造不一样的映像劫持后门