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 * ...
随机推荐
- 黄金含量版本——KTV
呀,进来的都是盆友,首先先给大家拜年了,祝大家新年快乐,万事如意,家和万事兴~! 大家看了标题进来就不能让大家白进来,一定会让大家带着满满的果实. 下面我们就来讨论讨论KTV这个项目: (1)KTV的 ...
- 20175120彭宇辰 《Java程序设计》第十周学习总结
教材内容总结 十二章 Java多线程机制 一.进程与线程.操作系统与进程 -线程不是进程,是比进程更小的执行单位.但与进程不同的是,线程的中断和恢复可以更加节省系统的开销. -线程可以共享进程中的某些 ...
- Windows 08R2 IIS网站架设
目录 目录 配置和安装IIS 环境设置 安装IIS服务器 网站的站点目录和欢迎页面 配置和安装IIS IIS是Windows的网站服务器,所以配置IIS服务的前提是需要一个网址.和DNS域名并添加主机 ...
- nginx相关总结
1. Nginx 无法启动解决方法 在查看到 logs 中报了如下错误时: 0.0.0.0:80 failed (10013: An attempt was made to access a sock ...
- token是什么?和session什么区别,怎么用
对于初学者来说,对Token和Session的使用难免会限于困境,开发过程中知道有这个东西,但却不知道为什么要用他?更不知道其原理,今天我就带大家一起分析分析这东西. 一.我们先解释一下他的含义: 1 ...
- selenium,webdriver爬取斗鱼主播信息 实操
from selenium import webdriver import time from bs4 import BeautifulSoup class douyuSelenium(): #初始化 ...
- 初学Selenium遇上的问题
1.IWebDriver driver = new InternetExplorerDriver();运行时报关于protecte model的错误 解决办法就是用如下代码设置IEDriverOpit ...
- C# 笔记 获取程序当前目录
在C#中,我们有以下几种方式获取程序当前目录: Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssem ...
- SQLAlchemy应用到Flask中
安装模块 pip install Flask-SQLAlchemy 加入Flask-SQLAlchemy第三方组件 from flask import Flask # 导入Flask-SQLAlche ...
- [转]Linux Shell编程入门
转自:http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的 ...