【矩阵快速幂】【杭电OJ1757】
http://acm.hdu.edu.cn/showproblem.php?pid=1757
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5829 Accepted Submission(s): 3555
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.
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.
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
题目分析:看到 1)f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);【一个递推式】
2) k<2*10^9 , m < 10^5【数据超大】
就知道是矩阵快速幂了【好吧..我之前并不知道..qaq..】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long n,k;
void mul(long long wqw[],long long tat[][])
{
long long or2[];
// cout <<
memset(or2,,sizeof(or2));
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
or2[i]+=(wqw[j]*(tat[j][i]))%k;
or2[i]%=k;
}
}
memcpy(wqw,or2,sizeof(or2));
}
void mulself(long long awa[][])
{
long long or3[][];
memset(or3,,sizeof(or3));
for(int i = ; i < ; i++)
{
for(int j = ; j< ; j++)
{
for(int kk = ; kk < ; kk++)
{
or3[i][j]+=((awa[i][kk])%k)*((awa[kk][j])%k)%k;
or3[i][j]%=k;
}
}
}
memcpy(awa,or3,sizeof(or3));
}
int main()
{
while(scanf("%lld%lld",&n,&k)==)
{ long long qaq[][];
long long qwq[];
long long orz[]={,,,,,,,,,};
memset(qaq,,sizeof(qaq));
for(int i = ; i < ; i++)
{
scanf("%lld",&qaq[i][]);
}
for(int i = ; i < ; i++)
{
qaq[i-][i]=;
}
if(n<)
{
printf("%lld\n",n%k);
}
else
{
n=n-;
while(n)
{
if(n&)mul(orz,qaq);
mulself(qaq);
n/=;
}
cout <<orz[]%k<<endl;
}
}
return ;
}
【矩阵快速幂】【杭电OJ1757】的更多相关文章
- 小白详细讲解快速幂--杭电oj2035-A^B
Problem Description 求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<= ...
- 杭电多校第七场 1010 Sequence(除法分块+矩阵快速幂)
Sequence Problem Description Let us define a sequence as below f1=A f2=B fn=C*fn-2+D*fn-1+[p/n] Your ...
- HDU 5607 graph 矩阵快速幂 + 快速幂
这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
随机推荐
- 12月7日,几个错误,拼写错误,遗漏符号:,记忆有误,max-width的作用。gem mini_magick, simple_form
❌: 1. /Users/chentianwei/jdstore3/jdstore/config/routes.rb:6:in `block in <top (required)>': u ...
- 3-30 flash(api),rescue_from(); logger简介
ActionDispatch::Flash < Objec pass temporary primitive-types (String, Array, Hash) between action ...
- 百度安卓SDK秘钥Key错误
下载官方安卓地图demo,输入报名和sha1申请AK,发现key错误 构建的时候要指定生成的key 安卓定位BaiduLocDemo出现aapt.exe finished with non-zero ...
- python-day21--time模块
一.三种表示方法 1.时间戳(timestamp): time.time( ) #得到的是float类型 2.格式化(Format String): time.strftime('%Y/% ...
- vue封装组件的正确方式-封装类似elementui的组件
最近读了下element的源码,仿照他封装了两种不同的组件. 第一种:通过组件来调用显示的 <template> <!--src/component/custom/main.vue- ...
- hdu-6301-贪心
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- consumer filter
ProtocolFilterWrapper中buildInvokerChain方法把Filter链在一起,调用执行的时候,逐个执行filter,最后执行filter中的invoker. //Proto ...
- gitlab永久设置密码
在 .gitconfig 文件中加入: [credential] helper = store .git-credentials close address
- notepad++插件安装
notepad安装目录的 plugins 下重启 notepad.exe程序即可 插件下载地址 : https://sourceforge.net/projects/npp-plugins/fil ...
- duilib 实现列表头任意拖动
1.表头(xml) <List name="List_records" padding="5,10,5,5" bkcolor="#FFFFFFF ...