A Simple Math Problem

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

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
 
题意:给出递推公式求第n项
题解:矩阵快速幂,注意初始矩阵为[9,8,7,6,5,4,3,2,1,0]
#include<bits/stdc++.h>
#define N 10
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
struct mat{
ll a[N][N];
mat(){
memset(a, , sizeof(a));
}
mat operator *(mat b){
mat c;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
for(int k=;k<N;k++)
c.a[i][j] = (c.a[i][j] + a[i][k]*b.a[k][j])%mod;
return c;
}
};
mat f(mat x,ll m){
mat t;
for(int i=;i<N;i++)
t.a[i][i] = ;
while(m){
if(m&) t = t*x;
x = x*x;
m >>= ;
}
return t;
}
int main()
{
ll k, i;
while(~scanf("%I64d%I64d", &k, &mod)){
if(k<){
printf("%I64d\n", k);
continue;
}
mat A, s;
for(i=;i<;i++)
s.a[][i] = -i;
for(i=;i<;i++)
scanf("%I64d", &A.a[i][]);
for(i=;i<;i++)
A.a[i][i+] = ;
s = s*f(A, k-);
printf("%I64d\n", s.a[][]);
} return ;
}

HDU1757 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. 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 ...

  3. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  4. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

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

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

  6. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

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

  8. 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 & ...

  9. hdu1757 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. SparkSQL JSON数据操作(1.3->1.4)

    1.用户自定义schema data json串格式如下: { "partner_code": "demo", "app_name": &q ...

  2. 有图有真相——关于“视频专辑:零基础学习C语言 ”

  3. SqlServer 中如何查看某一个Sql语句是复用了执行计划,还是重新生成了执行计划

    我们知道SqlServer的查询优化器会将所执行的Sql语句的执行计划作缓存,如果后续查询可以复用缓存中的执行计划,那么SqlServer就会为后续查询复用执行计划而不是重新生成一个新的执行计划,因为 ...

  4. uboot启动参数

    bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0bootdelay=2baudrate=115200ethaddr=08:00:3 ...

  5. Intel Edison

    起步: https://software.intel.com/zh-cn/node/628224 刷机: https://software.intel.com/zh-cn/flashing-firmw ...

  6. TM1680主从I2C设置.

    TM1680的工作流程通常是: 1. 通电, 没啥可说 2. SYS DIS (主从), 1000-0000即0x80, 关闭系统时钟跟LED循环, 主机都从机地址都要发 3. COM设置 (主从), ...

  7. 【JQGRID DOCUMENTATION】.学习笔记.6.Editing:Common Rules

    1 公共编辑属性 要在grid中显示数据的一个关键原因是能简单快速地编辑它.jgGrid提供三种编辑方式: cell editing:编辑指定cell inline editing:编辑同一行的几个c ...

  8. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  9. 24、jQuery常用AJAX-API/Java调用MySQL / Oracle过程与函数

      1)掌握jQuery常用AJAX-API 2)掌握Java调用MySQL / Oracle过程与函数 一)jQuery常用AJAX-API 目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取 ...

  10. day3 python 集合 文件

    字典是无序的,列表是有序的 a='zhangsan' print (a[1]) a[2]=222 #字符串不能赋值 集合(set):把不同的元素组成一起形成集合 info=[1,2,34,5,6,7] ...