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
 代码:
 //#define LOCAL
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
const int maxn=;
LL k,m;
int aa[maxn],mat[maxn][maxn];
int ans[maxn][maxn]; void init()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(i==)
mat[i][j]=aa[j];
else
if(i==j+)mat[i][j]=;
else mat[i][j]=;
if(i==j)
ans[i][j]=;
else ans[i][j]=;
}
}
} void Matrix(int a[][],int b[][])
{ int c[][];
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
c[i][j]=;
for(int k=;k<;k++)
{
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
}
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a[i][j]=c[i][j];
}
}
} void pow(LL n)
{
while(n>)
{
if(n&) Matrix(ans,mat);
n>>=1L;
if(n==)break;
Matrix(mat,mat);
}
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif while(scanf("%I64d%I64d",&k,&m)!=EOF)
{
for(int i=;i<;i++)
scanf("%d",aa+i);
if(k<) printf("%I64d\n",k%m);
else
{
init();
pow(k-);
int res=;
for(int i=;i<;i++)
res=(res+(-i-)*ans[][i])%m;
printf("%d\n",res);
}
}
return ;
}

hdu------(1757)A Simple Math Problem(简单矩阵快速幂)的更多相关文章

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

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

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

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  3. hdu 1757 A Simple Math Problem (矩阵快速幂)

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

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

    <font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...

  5. hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...

  6. HDU 1757 A Simple Math Problem(矩阵快速幂模板)

    题意:题意很简单,不多说了. 思路: |f(10) |       |a0 a1 a2 ...a8 a9|    |f(9)|| f(9)  |       | 1   0   0 ... 0     ...

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

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  8. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  9. HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过

    A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...

随机推荐

  1. Keepalive

    https://en.wikipedia.org/wiki/Keepalive Description A keepalive signal is often sent at predefined i ...

  2. Oracle 怎么让id自增加

    --自增长序列 create table test( tid int not null, tname ), tsex ), tbz ) ) --添加主键约束 alter table test add ...

  3. 02~ 一步一步教你使用 SVN之SVN 的介绍

    SVN的介绍 一.学习SVN的主要内容的介绍 1.基本操作:学习SVN有哪些基本操作 2.服务器端的安装配置:SVN中必不可少的2个环境之一,会在下面的课程中介绍服务器如何去安装和配置 3.客户端软件 ...

  4. new的深一步

    new的深一步 new运算符 用于创建对象和条用构造函数 new修饰符 用于隐藏基类中被继承的成员 new约束 用于在泛型声明中约束可能用作类型参数的参数类型 new运算符 用于创建对象和调用构造函数 ...

  5. CSharp Similarities and Differences

    This document lists some basic differences between Nemerle and C# in a terse form. If you know Java ...

  6. HttpServletRequest学习

    package cn.request; import java.io.IOException; import java.io.PrintWriter; import java.io.Unsupport ...

  7. oracle 查看锁表情况并处理锁表

    /* *locked *query locked object and analyse reason,kill it * */ select 'alter system kill session '' ...

  8. STRUTS2 标签 循环次数

    *<s:property value="menus.size()"/> * <s:if test='menus.size()>8'>    sssss ...

  9. java获取中问名字的首字母

    public class FirstLetterUtil { private static int BEGIN = 45217; private static int END = 63486; // ...

  10. ubuntu_常用命令_01

    1. 获取超级用户权限(需要输入超级用户的密码) sudo -i 2. 文本编辑器(暂时觉得 类似Windows里面的txt,更多功能有待发现) gedit /xxx/yyy ZC: 貌似 这个命令 ...