A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155 Accepted Submission(s): 110
 
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
 
/*
题意:给你题目描述的函数,然后让你求f(k)%m的值 初步思路:简单的爆一下 #错误:超内存! #改进思路:
想一下这个式子的实质,不用递归写,用循环写,但是需要循环1e9次,肯定是不可行的,可以用矩阵相乘f(x)是从0,1,2...9乘上a0,a1,a2...
.a9递推过来的这样就能得出来一个矩阵,线性代数还没学,矩阵真的有点麻烦,构造矩阵的时候老是想错了。 #再次错误:矩阵相乘虽然可以解决空间问题,但是没法解决时间问题;这里真的有点糊涂了,常数都有快速幂,矩阵的快速幂怎么就没有想起来呐!
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll m,k;
struct Matrix{
ll m[][];
void init0(){//构造用来乘的矩阵(相当于“幺元”吧,随便想到了离散中的概念就这么叫吧)
memset(m,,sizeof m);
for(int i=;i<;i++)
m[i][i-]=;
}
void init1(){//构造最初的a0~a9
memset(m,,sizeof m);
for(int i=;i<;i++){
m[i][]=-i;
}
}
};
Matrix Mul_Matrix(Matrix a,Matrix b){
Matrix c;
c.init0();
for(int i=;i<;i++){
for(int j=;j<;j++){
c.m[i][j]=;
for(int k=;k<;k++){
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%m;
}
c.m[i][j]%=m;
}
}
return c;
}
/**************矩阵快速幂*********************/
Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul_Matrix(a,b);
}
a=Mul_Matrix(a,a);
x>>=;
}
return b;
}
/**************矩阵快速幂*********************/
Matrix a,b;
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%lld%lld",&k,&m)!=EOF){
a.init0();
b.init1();
//cout<<k<<" "<<m<<endl;
for(int i=;i<;i++) scanf("%lld",&a.m[][i]);
// for(int i=9;i<k;i++){
// b=Mul_Matrix(a,b);
// for(int i=0;i<10;i++){
// for(int j=0;j<10;j++)
// cout<<a.m[i][j]<<" ";
// cout<<" ";
// for(int j=0;j<10;j++)
// cout<<b.m[i][j]<<" ";
// cout<<endl;
// }
// }
Matrix c=Pow(a,b,k-);//利用矩阵快速幂
printf("%lld\n",c.m[][]);
}
return ;
}

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)的更多相关文章

  1. HDU1757 A Simple Math Problem 矩阵快速幂

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

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

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

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

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

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

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

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

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

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

  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. hdu5015 矩阵快速幂233(好题)

    题意:       给你一个(n+1)*(m+1)的矩阵mat,然后给你mat[0][1] = 233 ,mat[0][2] = 2333,mat[0][3] = 23333...,然后输入mat[1 ...

随机推荐

  1. 第4章 同步控制 Synchronization ----Interlocked Variables

    同步机制的最简单类型是使用 interlocked 函数,对着标准的 32 位变量进行操作.这些函数并没有提供"等待"机能,它们只是保证对某个特定变量的存取操作是"一个一 ...

  2. HDFS概述(4)————HDFS权限

    概述 Hadoop分布式文件系统(HDFS)的权限模型与POSIX模型的文件和目录权限模型一致.每个文件和目录与所有者和组相关联.该文件或目录将权限划分为所有者的权限,作为该组成员的其他用户的权限.以 ...

  3. Sql Server——查询(二)

    上次写了查询里的一些简单的查询方法,如果说上次的是初级查询,那这次的就是高级查询了. 今天主要是聚合函数.分组查询.连接查询.联合查询.在我看来前三个挺简单的,稍微难理解点的也就最后一个,为什么呢?因 ...

  4. SiganlR 系列之概述

    简介 SignalR 是微软的 http 长连接(以下简称长连接)框架,它的出现为我们提供了一套行之有效的实时通信的解决方案. 背景 在http 1.0 时代,preRequest 都会建立新的tcp ...

  5. 原创:TSP问题解决方案-----禁忌搜索算法C实现

    本文着重于算法的实现,对于理论部分可自行查看有关资料可以简略参考该博文:http://blog.csdn.net/u013007900/article/details/50379135 本文代码部分基 ...

  6. vue前端页面跳转参数传递及存储

    不同页面间进行参数传递,实现方式有很多种,最简单最直接的方式就是在页面跳转时通过路由传递参数,如下所示. 路由传递参数 this.$router.push({ name: '跳入页面', params ...

  7. CPU工作方式、多核心、超线程技术详解[转贴]

    CPU是一台电脑的灵魂,决定电脑整体性能.现在的主流CPU都是多核的,有的运用了多线程技术(Hyper-threading,简称HT).多核可能还容易理解些,相信不少玩家都能说出个所以然.但超线程是个 ...

  8. Hadoop(三)手把手教你搭建Hadoop全分布式集群

    前言 上一篇介绍了伪分布式集群的搭建,其实在我们的生产环境中我们肯定不是使用只有一台服务器的伪分布式集群当中的.接下来我将给大家分享一下全分布式集群的搭建! 其实搭建最基本的全分布式集群和伪分布式集群 ...

  9. LeetCode-2 Keys Keyboard

    package Classify.DP.Medium; import org.junit.jupiter.api.Test; /** Initially on a notepad only one c ...

  10. jQuery form插件使用详解

    点击打开: jquery选择器全解 jquery中的style样式操作 jquery中的DOM操作 jquery中的事件操作全解 jquery中的动画操作全解 jquery中ajax的应用 自定义jq ...