HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂
先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html
http://www.cppblog.com/acronix/archive/2010/08/23/124470.aspx?opt=admin
http://www.cnblogs.com/vongang/archive/2012/04/01/2429015.html
http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html
233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 257 Accepted Submission(s): 165
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
1
2 2
0 0
3 7
23 47 16
2799
72937
 
题解1:http://www.cnblogs.com/whatbeg/p/3971994.html
题解2:http://blog.csdn.net/u013368721/article/details/39271565
题目分析:矩阵快速幂,构建一个如下的矩阵即可:
- n+2行的矩阵
- -- -- -- --
- | 1 1 1 1 1 1 1 0 | | a1 |
- | 0 1 1 1 1 1 1 0 | | a2 |
- | 0 0 1 1 1 1 1 0 | | a3 |
- | 0 0 0 1 1 1 1 0 | | a4 |
- | 0 0 0 0 1 1 1 0 | * | a5 |
- | 0 0 0 0 0 1 1 0 | | an |
- | - - - - - - - - - - - | | |
- | 0 0 0 0 0 0 10 1 | | 233|
- | 0 0 0 0 0 0 0 1 | | 3 |
- -- -- -- --
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 15
#define M 15
#define mod 10000007
#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll nn,m;
ll n;
ll x[];
//ll ans; struct Mat
{
ll mat[N][N];
}; Mat e,f,g;
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
ll i,j,k;
for(k = ; k < n ; k++)
{
for(i = ; i < n ;i++)
{
if(a.mat[i][k]==) continue;//优化
for(j = ;j < n ;j++)
{
if(b.mat[k][j]==) continue;//优化
c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod;
}
}
}
return c;
}
Mat operator ^(Mat a,ll k)
{
Mat c;
ll i,j;
for(i = ; i < n ;i++)
for(j = ; j < n ;j++)
c.mat[i][j] = (i==j);
for(; k ;k >>= )
{
if(k&) c = c*a;
a = a*a;
}
return c;
} void ini()
{
ll i,j;
for(i=;i<=nn;i++){
scanf("%I64d\n",&x[i]);
}
memset(e.mat,,sizeof(e.mat));
memset(f.mat,,sizeof(f.mat));
e.mat[][]=;
e.mat[][]=;
e.mat[][]=+x[];
for(i=;i<=nn;i++){
e.mat[][i+]=e.mat[][i]+x[i];
}
for(j=;j<nn+;j++){
if(j!=){
f.mat[][j]=;
}
f.mat[][j]=;
}
for(i=;i<nn+;i++){
for(j=i;j<nn+;j++){
f.mat[i][j]=;
}
}
n=nn+;
} void solve()
{
if(m>){
g= e* (f^(m-) );
}
else{
g.mat[][nn+]=e.mat[][nn+];
}
} void out()
{
printf("%I64d\n",g.mat[][nn+]);
} int main()
{
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
// while(T--)
while(scanf("%I64d%I64d",&nn,&m)!=EOF)
{
ini();
solve();
out();
} return ;
}
HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂的更多相关文章
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛  Coin 矩阵快速幂
		Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face u ... 
- hdu 1757 A Simple Math Problem  (矩阵快速幂,简单)
		题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ... 
- 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)
		题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ... 
- HDU - 5015  233 Matrix (矩阵快速幂)
		In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ... 
- 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 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
		Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ... 
- 广工十四届校赛 count 矩阵快速幂
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ... 
- hdu 2604 Queuing  dp找规律  然后矩阵快速幂。坑!!
		http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ... 
- POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)
		题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ... 
随机推荐
- js 前端不调接口直接下载图片
			// 下载图片 downPhoto (path) { this.downloadFiles(path) }, // 下载 downloadFiles (content) { console.log(c ... 
- RMQ求区间最大最小值
			#include<iostream> #include<cmath> #include<cstdio> #define N 50005 using namespac ... 
- readystatechange
			// alternative to DOMContentLoaded document.onreadystatechange = function () { if (document.readySta ... 
- springmvc导出excel(POI)
			/** * 导出excel表格 */ @RequestMapping(value = "/doExportData", method = {RequestMethod.POST, ... 
- node爬虫(简版)
			做node爬虫,首先像如何的去做这个爬虫,首先先想下思路,我这里要爬取一个页面的数据,要调取网页的数据,转换成页面格式(html+div)格式,然后提取里面独特的属性值,再把你提取的值,传送给你的页面 ... 
- mybatis 批量操作增删改查
			在介绍批量操作之前,首先先介绍一个语法:foreach.可以说是,foreach是整个批量操作的灵魂. 属性 描述 item 循环体中的具体对象. 支持属性的点路径访问,如item.age,item. ... 
- Ajax跨域问题---jsonp
			跨域:跨域名 一个域名下的文件去请求了和他不一样的域名下资源文件,那么就会产生跨域请求 解决跨域问题办法: 1.将要访问的外部资源存到本域名下的一个php文件 2.用flash方式 3.JSONP: ... 
- 【php】png 图片压缩 透明底色变黑
			需要使用gd库的方法 php需要引入gd扩展支持 /* * 图片压缩 ----------------------------------------------------------------- ... 
- java各种数据库连接
			MySQL: String Driver="com.mysql.jdbc.Driver"; //驱动程序 String URL="jdbc:mys ... 
- js根据银行卡号判断属于哪个银行,并返回银行缩写及银行卡类型
			在做绑定银行卡,输入银行卡的时候,产品有这么一个需求,需要用户输入银行卡号的时候,显示对应的银行卡名称及简称.于是苦苦寻觅,终于找到了支付宝的开放API,银行卡校验接口 https://ccdca ... 
