HDU1757 A Simple Math Problem 矩阵快速幂

时间:2023-02-08 13:42:50

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 &lpar;矩阵快速幂&rpar;

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

  2. A Simple Math Problem&lpar;矩阵快速幂&rpar;----------------------蓝桥备战系列

    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&lowbar;矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #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------&lpar;1757&rpar;A Simple Math Problem&lpar;简单矩阵快速幂&rpar;

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

  6. BestCoder Round &num;29——A--GTY&&num;39&semi;s math problem(快速幂(对数法))、B--GTY&&num;39&semi;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&lpar;矩阵&rpar;

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

随机推荐

  1. 远方的塔--Pylons

    转自:https://en.wikipedia.org/wiki/Pylons_project#Pylons_Framework Pylons

  2. CSS的Display属性可能的值

    none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-block 行内块元素.( ...

  3. Redis学习笔记&lpar;5&rpar;-Set

    package cn.com; import java.util.HashMap; import java.util.Map; import java.util.Set; import redis.c ...

  4. JAVA导出数据到excel中大数据量的解决方法

    最近在做项目功能时 ,发现有20万以上的数据.要求导出时直接导出成压缩包.原来的逻辑是使用poi导出到excel,他是操作对象集合然后将结果写到excel中. 使用poi等导出时,没有考虑数据量的问题 ...

  5. Installation of the JDK-9 on ubuntu&lpar;linux上安装jdk-9&rpar;

    Description:Java SE 9 is the latest update to the Java Platform(General Availability on 21 September ...

  6. Java正则表达式应用

    查找html中的图片 import java.util.regex.Matcher; import java.util.regex.Pattern; public class PicDownload ...

  7. springmvc 学习

    第一章回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 第二章回顾struts ...

  8. Java 微信公众号迁移

    背景:公众号换主体,要迁移,粉丝(openId)的业务数据要做处理. 第一步:参照我的另一篇文章,Java 导出微信公众号粉丝. 第二部:数据处理(master-worker模式) 程序主入口:Mai ...

  9. git项目&comma;VSCode显示不同颜色块的含义

    一. 概念 代码里的左侧颜色标识: 红色,未加入版本控制; (刚clone到本地) 绿色,已经加入版本控制暂未提交; (新增部分) 蓝色,加入版本控制,已提交,有改动: (修改部分) 白色,加入版本控 ...

  10. Ping服务

    什么是Ping服务 ping是基于XML_RPC标准协议的更新通告服务,用于博客把内容更新快速通知给百度,以便百度及时进行抓取和更新. Ping服务使用方法 你可以采取手动通知和自动通知两种方式使用p ...