hdu 1757 A Simple Math Problem (矩阵快速幂)
Description
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
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
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 这就算我写的第一道矩阵快速幂的题了!
矩阵快速幂的给我的感觉就是先找规律,YY出一个n*n的矩阵,矩阵左边写一列数,矩阵右边写一列数。让矩阵乘上右边那一列数等于左边的那列数。盗张图!!
然后......然后就没有然后了QWQ,输出答案就行了。
代码如下:
#include <bits/stdc++.h> using namespace std;
const int N=;
int k,m;
struct Matrix//用结构体来存矩阵
{
long long int mat[N][N];//直接上long long int 别找事
}matrix;
void init()
{
for (int i=;i<N;++i)
scanf("%lld",&matrix.mat[][i]);
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
if (i==j+)
matrix.mat[i][j]=;
else
matrix.mat[i][j]=;
}
}
}
Matrix operator * (Matrix a,Matrix b)//定义乘号
{
Matrix c;
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
c.mat[i][j]=;//初始化值为0
for (int k=;k<N;++k)
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=m;//记得取模,否则就炸了
}
}
return c;
}
Matrix Pow (int n)//定义快速幂函数
{
Matrix t;
if (n==)
return matrix;
if (n&)
return matrix*Pow(n-);
else
{
Matrix temp=Pow(n>>);
return temp*temp;
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&k,&m))
{
init();
if (k<)
{
printf("%lld\n",matrix.mat[][k]%m);
continue;
}
Matrix temp=Pow(k-);
long long int ans=;
for (int i=;i<N;++i)
{
ans += temp.mat[][i]*(N-i-);
ans%=m;
}
printf("%lld\n",ans);
}
return ;
}
hdu 1757 A Simple Math Problem (矩阵快速幂)的更多相关文章
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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 ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- 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) + …… ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
随机推荐
- SpringMvc Filter的使用
一:Filter过滤器. 先自定义一个过滤器. package com.jbj.filter; import org.springframework.web.filter.OncePerRequest ...
- appium 安装
1.npm安装: cmd——cnpm install -g appium 卸载: cmd——npm uninstall -g appium 2.下载安装包安装: Appium server: 1. A ...
- MacOS Catalina 导致bash命令失效的一些总结
欢天喜地的升级了最新的OS后,第一个发现bash失效的是使用pod命令出现: -bash: pod: command not found 这个不会导致cocoapods无法正常使用,可以使用绝对路径里 ...
- Linux 删除特殊文件名的文件
1.文件名含有特殊字符: 1) 执行 ls -i 命令 ,文件前面会出现一个数字,这个数字是文件的节点号 2) 使用find命令删除 find ./ -inum 节点号 -delete 2.文件名是以 ...
- (转载)虚拟机出现无法连接虚拟设备sata0:0,因为主机上没有相应设备
虚拟主机需要镜像文件, 如果是拷贝的虚拟机,还需要桥接联网的话,更改mac地址,
- MongoDB(7):集群部署实践,包含复制集,分片
注: 刚开始学习MongoDB,写的有点麻烦了,网上教程都是很少的代码就完成了集群的部署, 纯属个人实践,错误之处望指正!有好的建议和资料请联系我QQ:1176479642 集群架构: 2mongos ...
- C#-Newtonsoft.Json生成复杂JSON
官方文档:https://www.newtonsoft.com/json/help/html/SerializeObject.htm 一种方式就可以生成所有的 JSON Collection -> ...
- 爬取拉勾网所有python职位并保存到excel表格 对象方式
# 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...
- CentOS7下Docker与.net Core 2.2
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...
- python基础----求水仙花数
水仙花数,即一个三位数,各个位上的数字的三次方相加,等于该数本身.如:153 = 1**3 + 5 ** 3 + 3 ** 3 def is_narc_num(n): # if n <100 o ...
