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+...+ ...
随机推荐
- stringstream clear与str("")的问题
一.str与clear函数 C++Reference对于两者的解释: 可见:clear()用来设置错误状态,相当于状态的重置:str用来获取或预置内容 二.区别 运行下面测试代码: #include& ...
- Sublime Text 套件介紹(四):Pretty JSON
JSON,一個輕量級的資料交換語言,目前許多網站AJAX request的回應結果都是JSON格式 以下是一個標準的JSON格式 { "firstName": " ...
- CPP-练习
HW: 1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" ;局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会 ...
- idea Please specify commit message
在idea中使用github来进行版本控制的时候, 当点击提交的时候遇到了这个问题 错误: Please specify commit message 解决方法: 在commit message中填写 ...
- ubuntu 18.* 重启网卡
systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting A ...
- Luogu P2397 yyy loves Maths VI (mode)
题目传送门 虽然只是一道黄题,但还是学到了一点新知识-- 摩尔投票法 用\(O(1)\)的内存,\(O(n)\)的时间来找出一串长度为n的数中的众数,前提是众数出现的次数要大于\(n/2\) 方法很简 ...
- Dubbo中的监控和管理
一.Dubbo中的监控 1.原理 原理:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心. 2.搭建监控服务 3.修改配置文件 修改注册中心的地址: 注意:这个 ...
- JsonUtils工具类
public class JsonUtils { public static void printTimeObject(Object obj, HttpServletResponse response ...
- C++系统学习之四:数组
与vector的异同 相同:都是存放类型相同对象的容器 不同:数组的大小确定不变,不能随意向数组中增加元素 1.定义和初始化内置数组 数组中元素的个数也属于数组类型的一部分,编译的时候维度应该是已知的 ...
- jenkins学习笔记(一)
windows下安装jenkins步骤 1.下载 官网路径:https://jenkins.io/ 2.安装 直接双击安装程序即可 centos7下安装命令: wget -O /etc/yum.rep ...