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 Bcolor 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.

Input Format

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 MM lines with each line MM integers. The j^{th}jth integer in the i^{th}ith line means [i][j] 1≤f[i][j]≤1000000).

There are no more than 1010 test cases.

Output Format

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

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
/*
f[a][b]+f[b][c]+f[c][d]+……+f[y][z]的最大值。(n-1项)
n=2 :二重循环
n=3 :三重循环
n=4 : f[a][b]+f[b][c]+f[c][d]
可以用f[a][c]来代替f[a][c]和f[a][b]+f[b][c]的较大值,在进行f[a][c]+f[c][d]
此时的f[a][c]表示第一天a,第三天c的最大值
n>=5的依此类推
那么可以利用矩阵快速幂的思想,因为(n-2)*m^2会超时
n=2要更新一次来找最大值,n=3就要更新2次。
因此n要更新n-1次。
*/
const int N=;
ll f[N][N],n,m;
struct ma{
ll m[N][N];
ma(){
memset(m,,sizeof(m));
}
};
ll MAX;
ma poww(ma a,ma b)
{
ma c;
for(int i=;i<m;i++)
{
for(int j=;j<m;j++)
{
for(int k=;k<m;k++)
{
c.m[i][j]=max(c.m[i][j],a.m[i][k]+b.m[k][j]);
}
}
}
return c;
}
ma qu(ma a,ll n){
ma c;
while(n){
if(n&) c=poww(c,a);
n>>=;
a=poww(a,a);
}
return c;
}
int main()
{
while(~scanf("%lld%lld",&n,&m)){
ma ans;
for(int i=;i<m;i++)
{
for(int j=;j<m;j++)
{
scanf("%lld",&ans.m[i][j]);
}
}
ans=qu(ans,n-);
MAX=;
for(int i=;i<m;i++){
for(int j=;j<m;j++)
{
MAX=max(MAX,ans.m[i][j]);
}
}
printf("%lld\n",MAX);
}
return ;
}

最新文章

  1. hihoCoder太阁最新面经算法竞赛15
  2. composer 的使用
  3. Oracle中 Package与Package body的介绍
  4. Getting Started with Mongoose and Node.js – A Sample Comments System | Dev Notes
  5. java面试笔试试题http://www.jobui.com/mianshiti/it/java/6827/
  6. [AngularJS] Html ngSanitize, $sce
  7. IE9下Coolite.Ext出现createContextualFragment错误
  8. poj 1324 状态广搜
  9. iOS开发总结-图片左右滑动浏览
  10. POJ2229 Sumsets 【递归】
  11. 软件测试之α测试和Beta测试
  12. java split函数应该注意的问题
  13. js的DOM操作
  14. POJ 3581 Sequence [后缀数组]
  15. [Redis] redis的设计与实现-对象系统
  16. 【接口时序】6、IIC总线的原理与Verilog实现
  17. golang函数
  18. JSONArray.toJSONString json乱码
  19. python第八十八天----dom js
  20. Fedora瘦身

热门文章

  1. HDU 5875 H - Function 用单调栈水过了
  2. python实现批量远程执行命令及批量上传下载文件
  3. sql 容易被忽视的点
  4. zuul prefix
  5. 编写SQL语句操作数据库(慕课SQLite笔记)
  6. python+selenium之验证码的处理
  7. 用NPOI操作EXCEL-锁定列CreateFreezePane()
  8. UVA 10537 Toll! Revisited (逆推,最短路)
  9. UVA 11853 - Paintball 战场(dfs)
  10. Android(java)学习笔记115:BroadcastReceiver之 Android广播机制