2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分
矩阵
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
现在已知一个n*n矩阵A,S = A+A^2+A^3+...+A^k,输出S,因为每一个元素太大了,输出的每个元素模10
Input
Output
Sample Input
1
3 2
0 2 0
0 0 2
0 0 0
Sample Output
0 2 4
0 0 2
0 0 0 矩阵快速幂 + 等比数列二分求和
AC
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int maxn= 1e5+;
const double eps= 1e-;
const int inf = 0x3f3f3f3f;
const int mod =;
typedef long long ll;
int n,m;
struct matrix
{
int m[][];
matrix()
{
memset(m,,sizeof(m));
}
};
matrix operator *(const matrix &a,const matrix &b)
{
matrix c;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
c.m[i][j]=;
for(int k=;k<=n;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
}
return c;
}
matrix quick(matrix base,int pow)
{
matrix a;
for(int i=;i<=n;i++) a.m[i][i]=;
while(pow)
{
if(pow&) a=a*base;
base=base*base;
pow>>=;
}
return a;
}
matrix sum(matrix a,matrix b)
{
for(int j=;j<=n;j++)
for(int k=;k<=n;k++)
a.m[j][k]=(a.m[j][k]+b.m[j][k])%mod;
return a;
}
matrix solve(matrix x,int y)
{
matrix a,s;
if(y==)
return x;
a=solve(x,y/);
s=sum(a,a*quick(x,y/));
if(y&)
s=sum(s,quick(x,y));
return s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
matrix a;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a.m[i][j]);
matrix ans=solve(a,m);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(j==n)
printf("%d\n",ans.m[i][j]);
else
printf("%d ",ans.m[i][j]);
}
} }
}
2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分的更多相关文章
- 2017 ECJTU ACM 程序设计竞赛
大厦 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission ...
- 华南师大 2017 年 ACM 程序设计竞赛新生初赛题解
题解 被你们虐了千百遍的题目和 OJ 也很累的,也想要休息,所以你们别想了,行行好放过它们,我们来看题解吧... A. 诡异的计数法 Description cgy 太喜欢质数了以至于他计数也需要用质 ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- 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+...+ ...
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...
- HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)
M斐波那契数列 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- POJ 3233 矩阵快速幂&二分
题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...
- HDU:Gauss Fibonacci(矩阵快速幂+二分)
http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...
- 矩阵快速幂+二分 poj3233
#include <iostream> #include <cstdio> #include <string> #include <cstring> # ...
随机推荐
- ES6 函数的扩展3
箭头函数 基本用法 ES6允许使用"箭头"(=>)定义函数 var f = v => v; 上面的箭头函数等同于: var f = function(v) { retu ...
- 腾讯云主机 MySQL 远程访问配置方法
使用腾讯云主机安装 MySQL 之后,需要通过以下步骤进行配置以实现远程访问,主要分为两大部分 一.服务器端口配置 1.如果你的云主机配置了安全组,如果没有配置安全组就可以直接跳过“步骤1”的操作,否 ...
- java 类的继承和接口的继承
父类 public class person { String name; int age; void eat(){ System.out.println("吃饭"); } voi ...
- centos 打包RPM包 ntopng
需要在centos7上,将ntopng及其依赖的包一起打包成rpm包,了解centos7打包. 1.执行: yum -y install rpmdevtools 安装rpm工具 2.接下来执行:rp ...
- TensorFlow常用的函数
TensorFlow中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个 或多个集合中 ...
- Unity 3d游戏逆向及.NET Reflector工具使用介绍
移动平台游戏框架主要有unity 3d和cocos 2d.我们首先得识别游戏使用的框架.识别Unity游戏Android平台的apk包可以直接解压,看是否有./assets/bin/Data/Mana ...
- Swift语言中与C/C++和Java不同的语法(四)
这一节,我们将会讨论一下Swift中的函数相关的基本内容 首先是函数的创建: func sayHello (name:String) -> String { return "Hello ...
- zabbix 3.0.4 中文字体替换
zabbix 对中文支持不是很好,会出现乱码: 从windows系统里 找到字体包:如图: 拷贝到zabbix-server里面,注意,把文件名改成小写: 我linux 是centos7.2版本 [r ...
- SpringMvc开发步骤
1.导入基本jar包 2.在Web.xml中配置DispatcherServlet <!-- 配置 DispatcherServlet --> <servlet> <se ...
- JS函数的参数声明中用 var 与不用 var的区别
1.var 声明的变量,作用域是当前 function 2.没有声明的变量,直接赋值的话, 会自动创建变量,但作用域是全局的. 例如: function doSth() { a = "AAA ...