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

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

题目分析:看到 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】的更多相关文章

  1. 小白详细讲解快速幂--杭电oj2035-A^B

    Problem Description 求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方”  Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<= ...

  2. 杭电多校第七场 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 ...

  3. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

  4. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  5. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  6. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  7. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  8. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  9. 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 ...

随机推荐

  1. 8天掌握EF的Code First开发

    C#高级知识点&(ABP框架理论学习高级篇)——白金版 http://www.cnblogs.com/farb/p/ABPAdvancedTheoryContent.html

  2. C#对GZIP压缩和解压

    /// <summary> /// 将Gzip的byte数组读取为字符串 /// </summary> /// <param name="bytes" ...

  3. oracle创建/删除表空间、创建/删除用户并赋予权限

    创建表空间 分开执行如下sql语句 --创建临时表空间 CREATE SMALLFILE TEMPORARY TABLESPACE "TEMP11" TEMPFILE 'E:\ap ...

  4. memcached-1.4.20 主要启动流程笔记

    以下笔记主要是关注tcp模式下memcached的启动过程. main() 设置信号处理函数为sig_handler() 初始化系统设置,保存在全局变量settings里面 settings_init ...

  5. Android之RecyclerView实现时光轴

    做项目的过程中有个需求需要时光轴,于是网上找了部分资料 ,写了个案例,现在分享给大家. 如图: activity_main.xml <?xml version="1.0" e ...

  6. 安卓出现Invalid layout of java.lang.String at value

    Project->Properties->Run/Debug setting->选择类->classpath->删除Bootstrap Entries下面的文件 参考

  7. 49 BOM 和DOM

    一.BOM window 对象  1.确认,输入,    window.alert(123) // 弹框    let ret = window.confirm("是否删除") / ...

  8. Strip CodeForces - 487B (单调队列)

    题面: Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Ale ...

  9. P3377 【模板】左偏树(可并堆)

    //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack- ...

  10. 2-20 MySQL集群搭建实现高可用

    MySQL集群概述和安装环境 MySQL Cluster是MySQL适合于分布式计算环境的高实用.高冗余版本.Cluster的汉语是"集群"的意思.它采用了NDB Cluster ...