【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
Sample Output
题意:按f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10) (x>=10) ; f(x) = x(x<10)来计算f(x)%m的值。
分析:这题要用递推,并且k值很大,所以需要用矩阵快速幂。
构造的矩阵是:
|
* |
|
= |
|
写个结构类型代表矩阵,以及矩阵的相乘的函数和矩阵快速幂的函数,注意一下初始化。
#include<stdio.h>
#include<string.h>
int n,k,m;
struct matrix
{
int a[][];
int row,col;
void init(int r,int c){
memset(a,,sizeof(a));
row=r;col=c;
}
} big,f,u;
matrix mul(matrix a,matrix b)
{
matrix c;
c.init(a.row,b.col);
for(int i=; i<a.row; i++)
for(int j=; j<b.col; j++)
{
for(int k=; k<a.col; k++)
c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m;
c.a[i][j]%=m;
}
return c;
}
void init()
{
big.init(,);
f.init(,);
for(int i=; i<; i++)
big.a[i][i-]=;
for(int i=; i<; i++)
f.a[i][]=-i;
}
matrix qpow(matrix a,int k)
{
matrix ans;
ans.init(a.row,a.col);
for(int i=;i<a.row;i++)
ans.a[i][i]=;
while(k)
{
if(k&)ans=mul(ans,a);
a=mul(a,a);
k>>=;
}
return ans;
}
int main()
{
init();
while(~scanf("%d%d",&k,&m))
{
for(int i=; i<; i++)
scanf("%d",&big.a[][i]);
u=mul(qpow(big,k-),f);
printf("%d\n",u.a[][]%m);
}
return ;
}
【HDU 1757】 A Simple Math Problem的更多相关文章
- 【hdu 2486】A simple stone game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【HDOJ5974】A Simple Math Problem(构造,解方程)
题意:给定A与B,要求构造出一组X,Y,使得X+Y=A,lcm(X,Y)=B A<=2e4,B<=1e9 思路:A的范围较小,考虑以A为突破口 枚举A的约数k,复杂度O(sqrt(A)) ...
- 【HDU 5399】Too Simple
题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...
- 【hdu 5628】Clarke and math (Dirichlet卷积)
hdu 5628 Clarke and math 题意 Given f(i),1≤i≤n, calculate \(\displaystyle g(i) = \sum_{i_1 \mid i} \su ...
- 【bzoj 3489】A simple rmq problem
题目 \(kdt\)就是数点神器 我们先扫两遍处理出每个数上一次出现的位置\(pre_i,nxt_i\),之后变成\((i,pre_i,nxt_i)\)这样一个三维空间上的点 就变成了求一个立方体的最 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- 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 (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
随机推荐
- CSS select样式优化 含jquery代码
CSS 下拉选择菜单基本的CSS样式不怎么好看,通过一些简单的样式优化,可以得到如下图这样的: html结构如下: <div class="sel_wrap"> < ...
- Debian安装中文输入法
简单暴力: apt-get install ibus-pinyin 你也可以通过GUI下面到Synaptic Package Manager里面选中ibus-pinyin进行安装 安装完成后重启计算机 ...
- java8-2 多态的概述
1.多态:同一个对象(事物),在不同时刻体现出来的不同状态. 举例: 猫是猫,猫是动物. 水(液体,固体,气态). 多态的前提: A:要有继承关系. B:要有方法重写. 其实没有也是可以的,但是如果没 ...
- 内联(行级)元素不能设置margin-top
内联(行级)元素 不能设置宽高,但padding属性可以设置,需要注意的是行级元素不能设置margin-top和margin-bottom属性,但可以设置margin-left和margin-righ ...
- TIF、JPG图片手动添加地理坐标的方法(转载)
题目:为TIF.JPG图片添加地理坐标/平面直角坐标. 图片来源:GOOGLE EARTH.(当然也可以是其他知道四角点坐标的图片) 截图工具:GEtscreen(此软件截图时可以自动生成图片四角点坐 ...
- zepto的tap事件的穿透分析
首先是什么情况下会发生zepto(tap)的事件穿透: 当一个弹出层用tap点击之后这个层隐藏或者是移走,都会触发下面对应位置的点击事件(click)和一些标签的默认行为(a标签的跳转.input获取 ...
- 22Mybatis_订单商品数据模型_多对多查询以及对多对多查询的总结
之前讲了一对一,一对多查询,这篇文章讲的是多对多. 先给出需求:查询用户及用户购买商品信息. 我们由之前的文章知道,这个需求是多对多的. 还是那个终止我们的mybatis所做的不管是之前的一对一还是一 ...
- C# 鼠标穿透窗体功能
通过以下代码,在窗体启动后调用方法SetPenetrate() 即可实现窗体的穿透功能. 同样该功能需要加载命名空间 using System.Runtime.InteropServices; pri ...
- Socket Programming in C#--Getting Started
Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...
- OAF 中对文字实现html效果及对超级长文本实现默认换行,对只读的messageTextInput中的内容自动换行
今天遇到一个需求,客户注册页面客户化了一个超级长的注册须知,内容很多.但是样式相对又要做起来好看点. 注册须知的内容使用多个message拼接而成. 老大说rawText支持html样式,于是我想到了 ...