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. spark streaming - kafka updateStateByKey 统计用户消费金额

    场景 餐厅老板想要统计每个用户来他的店里总共消费了多少金额,我们可以使用updateStateByKey来实现 从kafka接收用户消费json数据,统计每分钟用户的消费情况,并且统计所有时间所有用户 ...

  2. paper 20 :color moments

    图像的颜色特征是很重要的,其中颜色矩也是很重要的一部分.(还有一个关于图像颜色特征的review,对于image color写的比较全面).还有,我要强调一下,本blog上的链接都是Google学术上 ...

  3. 关于MVC4中EFCoderFirst 数据迁移的三句经典指令

    首先输入这句指令     enable-migrations -contexttypename SchoolContext  ---------(SchoolContext为你设置的数据库名)它会自动 ...

  4. VS2010 发布网站时文件丢失

      问题:使用VS发布网站时,发现一些Flv等文件丢失,没有发布到指定文件夹中. 解决办法:打开文件属性窗口,找到生成操作,选项选择“内容”即可. 详细内容可参考官方文档: http://msdn.m ...

  5. c/c++ 常用的几个安全函数

    _stprintf_s  // 格式化字符串 _vsntprintf_s // 格式化 不定长参数用到

  6. bash环境变量读取顺序

    bash环境变量读取顺序: 交互式登录的用户: /etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bas ...

  7. linux man

    man能够为除命令之外的配置文件.系统调用.库调用等都能提供帮助手册,他们分别位于不同的章节中: 1.用户命令 2.系统调用 3.库调用 4.设备文件 5.配置文件 6.游戏 7.杂项 8.管理命令

  8. React Native微信分享 朋友圈分享 Android/iOS 通用

    超详细React Native实现微信好友/朋友圈分享功能-Android/iOS双平台通用   2016/06/16 |  React Native技术文章 |  Sky丶清|  暂无评论 |  1 ...

  9. 利用python进行数据分析 (学习笔记)

    第一章:准备工作 1.重要的Python库 (1)NumPy:Python科学计算的基础包.功能有:

  10. ACE的接受器(Acceptor)和连接器(Connector):连接建立模式

    ACE_Acceptor工厂的open()方法,或是它的缺省构造器(它实际上会调用open()方法),来开始被动侦听连接.当接受器工厂的open()方法被调用时,如果反应堆单体还没有被实例化,open ...