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+...+ ...
随机推荐
- 如何写好一个vue组件,老夫的一年经验全在这了【转】 v-bind="$attrs" 和 v-on="$listeners"
如何写好一个vue组件,老夫的一年经验全在这了 一个适用性良好的组件,一种是可配置项很多,另一种就是容易覆写,从而扩展功能 Vue 组件的 API 来自三部分——prop.事件和插槽: prop 允许 ...
- 将Xcode的本地代码push到github仓库上
1.首先,你得有一个github账号,如果没有的话就去注册一个,通过下面图片的方式创建一个github仓库. 2.创建仓库后填写相关的信息,比如说仓库名等. 3.在xcode上进行设置,添加远程git ...
- 导出Excel插件——Export-CSV ---20150610
出处:http://bbs.hcharts.cn/thread-99-1-1.html 导出Excel插件——Export-CSV 一.插件信息 插件名:Export-CSV(导出Execl文件) ...
- 微信小程序 wx.request POST请求------中文乱码问题
问题: 一个简单的表单,提交后台返回数据“提交成功”. 以为没问题了,但是没过多久后台小哥就问为啥那么多乱码,找了很久原因,发现在提交的时候就已经乱码了. 嗯,前端问题,然后测试GET/POST方法. ...
- centos7.2 安装nginx
一.首先检查gcc编译器安装了没 二 首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库),检查一下是否已经安装了( ...
- python爬虫入门六:Selenium库
在我们爬取网页过程中,经常发现我们想要获得的数据并不能简单的通过解析HTML代码获取,这些数据是通过AJAX异步加载方式或经过JS渲染后才呈现在页面上显示出来. selenuim是一种自动化测试工具, ...
- ACM训练联盟周赛 Teemo's formula
Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...
- Memcached特性及优缺点
为了加快文件访问速度且提供多个使用者.需要在内存中建立内存缓存数据的管理减小读写磁盘的次数及保证数据的更新.因为需要使用cache缓存. 1.Memcached 主要特性 a.数据仅存在于内存中, ...
- Java实现——Socket网络通信的机制以及实现举例
1. 网络间的进程通信与Socket TCP/IP协议族中网络层的IP地址可以唯一标识网络中的主机,而传输层的协议+端口可以唯一标识主机中的应用程序(进程).这样利用这三元组就可以标识网络的进程了,网 ...
- jmx_exportter+prometheus+grafana监控hadoop
0.介绍(摘录自https://www.hi-linux.com/posts/25047.html) 什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和 ...