徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂
题目
题目描述
JSZKC is going to spend his vacation!
His vacation has N days. Each day, he can choose a T-shirt to wear. Obviously, he doesn’t want to wear a singer color T-shirt since others will consider he has worn one T-shirt all the time.
To avoid this problem, he has M different T-shirt with different color. If he wears A color T-shirt this day and B color T-shirt the next day, then he will get the pleasure of f[A][B].(notice: He is able to wear one T-shirt in two continuous days but may get a low pleasure)
Please calculate the max pleasure he can get.
输入
The input file contains several test cases, each of them as described below.
The first line of the input contains two integers N,M (2 ≤ N≤ 100000, 1 ≤ M≤ 100), giving the length of vacation and the T-shirts that JSZKC has.
The next follows M lines with each line M integers. The jth integer in the ith line means f[i][j](1<=f[i][j]<=1000000).
There are no more than 10 test cases.
输出
One line per case, an integer indicates the answer
样例输入
3 2
0 1
1 0
4 3
1 2 3
1 2 3
1 2 3
样例输出
2
9
题目来源
The 2018 ACM-ICPC China JiangSu Provincial Programming Contest
题意:有n天,m件衣服,如果某一天穿了第i件衣服,第二天穿了第j件衣服,那么就会获得dp[i][j]的权值。然后给你矩阵f,每件衣服可以穿无限多次,问第n天能获得的最大权值是多少。
分析:设dp[t][i][j]为第1天穿第i件衣服第t天穿第j件衣服时能获得的最大的权值。假设a+b=t显然有dp[t][i][j]=max1≤k≤m(dp[t][i][j], dp[a][i][k]+dp[b][k][j]),因为牵扯到矩阵所以考虑矩阵快速幂。
参考博客:https://blog.csdn.net/xs18952904/article/details/80595275
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 107;
const int mod = 1e9 + 7;
typedef long long ll;
struct matrix {
ll a[maxn][maxn];
};
ll n, m;
matrix base, ans;
matrix mul( matrix x, matrix y ) {
matrix tmp;
memset( tmp.a, 0, sizeof(tmp.a) );
for( ll i = 0; i < m; i ++ ) {
for( ll j = 0; j < m; j ++ ) {
for( ll k = 0; k < m; k ++ ) {
tmp.a[i][j] = max( tmp.a[i][j], x.a[i][k] + y.a[k][j] );
}
}
}
return tmp;
}
void qow( ll x ) {
while( x ) {
if( x&1 ) {
ans = mul( ans, base );
}
base = mul( base, base );
x /= 2;
}
}
int main() {
std::ios::sync_with_stdio(false);
while( cin >> n >> m ) {
memset( ans.a, 0, sizeof(ans.a) );
for( ll i = 0; i < m; i ++ ) {
for( ll j = 0; j < m; j ++ ) {
cin >> base.a[i][j];
}
}
qow( n-1 );
ll sum = 0;
for( ll i = 0; i < m; i ++ ) {
for( ll j = 0; j < m; j ++ ) {
//cout << ans.a[i][j] << " ";
sum = max( sum, ans.a[i][j] );
}
// cout << endl;
}
cout << sum << endl;
}
return 0;
}
徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂的更多相关文章
- 2019南昌邀请赛 C. Angry FFF Party 大数矩阵快速幂+分类讨论
题目链接 https://nanti.jisuanke.com/t/38222 题意: 定义函数: $$F(n)=\left\{\begin{aligned}1, \quad n=1,2 \\F(n- ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
[解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...
- 2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】
Problem Description We define a sequence F: ⋅ F0=0,F1=1;⋅ Fn=Fn−1+Fn−2 (n≥2). Give you an integer k, ...
- Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站
题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
随机推荐
- C#实现简单爬虫
分享之前写过的一个爬虫,采集数据,存入数据库的简单实现. github地址:https://github.com/CodesCreator/biu-biu-biu-
- 【C++】string::substr函数
形式:s.substr(p, n) 返回一个string,包含字符串s中从p开始的n个字符的拷贝(p的默认值是0,n的默认值是s.size() - p,即不加参数会默认拷贝整个s) int main( ...
- mysql优化---订单查询优化(2):异步分页处理
订单分页查询: 老的代码是顺序执行查询数据和计算总记录数,但是如果条件复杂的话(比如关联子表)查询的时间要超过20s种 public static PagedList<Map<String ...
- android ——网络编程
一.WebView 这个View就是一个浏览器,用于展示网页的. 布局文件: <LinearLayout xmlns:android="http://schemas.android.c ...
- 【0802 | Day 7】Python进阶(一)
目 录 数字类型的内置方法 一.整型内置方法(int) 二.浮点型内置方法(float) 字符串类型内置方法 一.字符串类型内置方法(str) 二.常用操作和内置方法 优先掌握: 1.索引取值 2. ...
- C#_会员管理系统
https://www.cnblogs.com/start-from-scratch/p/5420588.html
- Ubuntu 17 安装Chrome浏览器
1.进入下载文件存放目录 cd Downloads 2.下载chrome文件 2.1 32位使用如下命令 wget https://dl.google.com/linux/direct/google- ...
- C语言连接mysql,用GCC编译
1. main.c文件内容如下 #include <stdlib.h>#include <stdio.h>#include <winsock.h>#include ...
- Linux配置部署_新手向(二)——Nginx安装与配置
目录 前言 Nginx 配置(后续补充) 小结 @ 前言 上一篇整完Linux系统的安装,紧接着就开始来安装些常用的东西吧,首先Nginx. Nginx 简介 Nginx作为转发,负载均衡,凭着其高性 ...
- spark通过JDBC读取外部数据库,过滤数据
官网链接: http://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases http:// ...