【POJ 3233】Matrix Power Series
【题目链接】
【算法】
要求 A^1 + A^2 + A^3 + ... + A^k
考虑通过二分来计算这个式子 :
令f(k) = A^1 + A^2 + A ^ 3 + ... + A^k
那么,当k为奇数时,f(k) = f(k-1) + A ^ k
当k为偶数时,f(k) = f(n/2) + A ^ (n/2) * f(n/2)
因此,可以通过二分 + 矩阵乘法快速幂的方式,在O(n^3log(n)^2)的时间内解决此题
【代码】
- #include <algorithm>
- #include <bitset>
- #include <cctype>
- #include <cerrno>
- #include <clocale>
- #include <cmath>
- #include <complex>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <ctime>
- #include <deque>
- #include <exception>
- #include <fstream>
- #include <functional>
- #include <limits>
- #include <list>
- #include <map>
- #include <iomanip>
- #include <ios>
- #include <iosfwd>
- #include <iostream>
- #include <istream>
- #include <ostream>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <stdexcept>
- #include <streambuf>
- #include <string>
- #include <utility>
- #include <vector>
- #include <cwchar>
- #include <cwctype>
- #include <stack>
- #include <limits.h>
- using namespace std;
- #define MAXN 35
- int i,j,n,k,m;
- struct Matrix
- {
- int mat[MAXN][MAXN];
- } a,ans;
- inline Matrix add(Matrix a,Matrix b)
- {
- int i,j;
- Matrix ans;
- memset(ans.mat,,sizeof(ans.mat));
- for (i = ; i <= n; i++)
- {
- for (j = ; j <= n; j++)
- {
- ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % m;
- }
- }
- return ans;
- }
- inline Matrix mul(Matrix a,Matrix b)
- {
- int i,j,k;
- Matrix ans;
- memset(ans.mat,,sizeof(ans.mat));
- for (i = ; i <= n; i++)
- {
- for (j = ; j <= n; j++)
- {
- for (k = ; k <= n; k++)
- {
- ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % m;
- }
- }
- }
- return ans;
- }
- inline Matrix power(Matrix a,int m)
- {
- Matrix ans,p = a;
- for (i = ; i <= n; i++)
- {
- for (j = ; j <= n; j++)
- {
- ans.mat[i][j] = (i == j);
- }
- }
- while (m > )
- {
- if (m & ) ans = mul(ans,p);
- p = mul(p,p);
- m >>= ;
- }
- return ans;
- }
- Matrix solve(int n)
- {
- Matrix tmp;
- if (n == ) return a;
- if (n % == )
- {
- tmp = solve(n/);
- return add(tmp,mul(power(a,n/),tmp));
- } else return add(solve(n-),power(a,n));
- }
- int main()
- {
- scanf("%d%d%d",&n,&k,&m);
- for (i = ; i <= n; i++)
- {
- for (j = ; j <= n; j++)
- {
- scanf("%d",&a.mat[i][j]);
- }
- }
- ans = solve(k);
- for (i = ; i <= n; i++)
- {
- for (j = ; j < n; j++)
- {
- printf("%d ",ans.mat[i][j]);
- }
- printf("%d\n",ans.mat[i][n]);
- }
- return ;
- }
【POJ 3233】Matrix Power Series的更多相关文章
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- 【poj3233】 Matrix Power Series
http://poj.org/problem?id=3233 (题目链接) 题意 给出一个n×n的矩阵A,求模m下A+A2+A3+…+Ak 的值 Solution 今日考试就A了这一道题.. 当k为偶 ...
- 【POJ - 3685】Matrix(二分)
Matrix Descriptions 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. In ...
- 【POJ - 2078】Matrix(dfs)
-->Matrix Descriptions: 输入一个n×n的矩阵,可以对矩阵的每行进行任意次的循环右移操作,行的每一次右移后,计算矩阵中每一列的和的最大值,输出这些最大值中的最小值. Sam ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series
poj 1575 Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15417 Accepted: ...
随机推荐
- 用ffmpeg切割音频文件
ffmpeg -i audio.wav -f segment -segment_time -c copy audio%02d.wav "-segment_time 60" 表示每6 ...
- buf.fill()
buf.fill(value[, offset[, end]][, encoding]) value {String} | {Buffer} | {Number} offset {Number} 默认 ...
- python的学习之路(三)
一.set集合#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harson old_dict = { "#1": {'hos ...
- SSL 握手协议详解
这里重点介绍一下服务端的验证和密钥交换.这个阶段的前面的(a)证书 和(b)服务器密钥交换是基于密钥交换方法的.而在SSL中密钥交换算法有6种:无效(没有密钥交换).RSA.匿名Diffie-Hell ...
- TestNG 练习
java文件 package selniumhomework; import org.testng.annotations.Test; public class Test1 { @Test(group ...
- 解方程(codevs 3732)
题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...
- 【NOIP2016】蚯蚓(单调队列)
题意: 思路: 我们发现,对于任意两次切割i和j,i<j,在进行完第j次切割后,第i次切割的u/v部分一定大于等于第j次切割的u/v部分,第i次的1-u/v部分也一定大于等于第j次的1-u/v部 ...
- 【CodeChef】KNGHTMOV(方案数DP)
题意: 考虑一张无限大的方格棋盘.我们有一个“骑士”,它必须从(0,0)格开始,按照如下规则,移动至(X,Y)格:每一步,它只能从(u,v)格移动至(u+Ax,v+Ay)或者(u+Bx,v+By).注 ...
- 【BZOJ2301】Problem b(莫比乌斯反演)
题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d, 且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000 ...
- Codeforces 645A Amity Assessment【八数码】
题目链接: http://codeforces.com/problemset/problem/645/A 题意: 2*2的八数码问题 分析: 这题n为2,不需要搜索,直接判断字母排列顺序就好了. 注意 ...