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.

样例输入

  1. 3 2
  2. 0 1
  3. 1 0
  4. 4 3
  5. 1 2 3
  6. 1 2 3
  7. 1 2 3

样例输出

  1. 2
  2. 9

题目来源

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <cmath>
  11. #include <cstdlib>
  12. #include <ctime>
  13. using namespace std;
  14. typedef long long ll;
  15. /*
  16. f[a][b]+f[b][c]+f[c][d]+……+f[y][z]的最大值。(n-1项)
  17. n=2 :二重循环
  18. n=3 :三重循环
  19. n=4 : f[a][b]+f[b][c]+f[c][d]
  20. 可以用f[a][c]来代替f[a][c]和f[a][b]+f[b][c]的较大值,在进行f[a][c]+f[c][d]
  21. 此时的f[a][c]表示第一天a,第三天c的最大值
  22. n>=5的依此类推
  23. 那么可以利用矩阵快速幂的思想,因为(n-2)*m^2会超时
  24. n=2要更新一次来找最大值,n=3就要更新2次。
  25. 因此n要更新n-1次。
  26. */
  27. const int N=;
  28. ll f[N][N],n,m;
  29. struct ma{
  30. ll m[N][N];
  31. ma(){
  32. memset(m,,sizeof(m));
  33. }
  34. };
  35. ll MAX;
  36. ma poww(ma a,ma b)
  37. {
  38. ma c;
  39. for(int i=;i<m;i++)
  40. {
  41. for(int j=;j<m;j++)
  42. {
  43. for(int k=;k<m;k++)
  44. {
  45. c.m[i][j]=max(c.m[i][j],a.m[i][k]+b.m[k][j]);
  46. }
  47. }
  48. }
  49. return c;
  50. }
  51. ma qu(ma a,ll n){
  52. ma c;
  53. while(n){
  54. if(n&) c=poww(c,a);
  55. n>>=;
  56. a=poww(a,a);
  57. }
  58. return c;
  59. }
  60. int main()
  61. {
  62. while(~scanf("%lld%lld",&n,&m)){
  63. ma ans;
  64. for(int i=;i<m;i++)
  65. {
  66. for(int j=;j<m;j++)
  67. {
  68. scanf("%lld",&ans.m[i][j]);
  69. }
  70. }
  71. ans=qu(ans,n-);
  72. MAX=;
  73. for(int i=;i<m;i++){
  74. for(int j=;j<m;j++)
  75. {
  76. MAX=max(MAX,ans.m[i][j]);
  77. }
  78. }
  79. printf("%lld\n",MAX);
  80. }
  81. return ;
  82. }

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt的更多相关文章

  1. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

  2. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set

    Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...

  3. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest(第六场)

    A Plague Inc Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants ...

  4. C.0689-The 2019 ICPC China Shaanxi Provincial Programming Contest

    We call a string as a 0689-string if this string only consists of digits '0', '6', '8' and '9'. Give ...

  5. B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest

    BaoBao has just found a grid with $n$ rows and $m$ columns in his left pocket, where the cell in the ...

  6. ACM ICPC, Damascus University Collegiate Programming Contest(2018) Solution

    A:Martadella Stikes Again 水. #include <bits/stdc++.h> using namespace std; #define ll long lon ...

  7. 计蒜客 39272.Tree-树链剖分(点权)+带修改区间异或和 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest E.) 2019ICPC西安邀请赛现场赛重现赛

    Tree Ming and Hong are playing a simple game called nim game. They have nn piles of stones numbered  ...

  8. 计蒜客 39280.Travel-二分+最短路dijkstra-二分过程中保存结果,因为二分完最后的不一定是结果 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest M.) 2019ICPC西安邀请赛现场赛重现赛

    Travel There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. ...

  9. 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛

    Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...

随机推荐

  1. Jenkins+Gitlab+Ansible自动化部署(二)

    接Jenkins+Gitlab+Ansbile自动化部署(一):https://www.cnblogs.com/zd520pyx1314/p/10210727.html Ansible的配置与部署 工 ...

  2. 高效的设计可视化UI

    http://www.uimaker.com/uimakerdown/uitutorial/35990.html http://maqetta.org/downloads/ .Data.js Data ...

  3. JS和jquery获取各种屏幕的宽度和高度的代码

    Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...

  4. Kendo UI 初始化 Data 属性

    初始化 Data 属性 前面在介绍准备 Kendo UI 开发环境时我们使用 jQuery 的方法将一个  HTML 元素转换成一个 Kendo UI 控制项: $(“#datepicker”).ke ...

  5. js基础的自定义属性练习

    js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...

  6. postgres创建库时指定编码格式

    postgres新建数据库时如果没有指定编码格式,会用默认的编码格式: 对于已经存在的数据库,虽然可以用:set client_encoding to 'UTF8'; set server_encod ...

  7. POJ 1845 Sumdiv (数学,乘法逆元)

    题意: 给出数字A和B,要求AB的所有因子(包括AB和1)之和 mod 9901 的结果. 思路: 即使知道公式也得推算一阵子. 很容易知道,先把分解得到,那么得到,那么的所有因子之和的表达式如下: ...

  8. [VC]获取本地程序的版本信息信息

    CString CQwerApp::IS_GetAppVersion(char *AppName) { ////需要加上version.lib在link里 CString AppVersion; // ...

  9. UI与数据分离 与 UI的演进

    解藕的好处:UI内部模块能够灵活的变化. MVC或者三层架构着重强调了数据.业务逻辑和UI的分离. (MVC中的C只是UI和业务逻辑模块间的一个中转组件,理论上应该是个轻模块.) 以前的关注的解藕技术 ...

  10. 使用ServiceController组件控制计算机服务

    实现效果: 知识运用: ServiceController组件的MachineName属性 //获取或设置服务所驻留的计算机名称 public string MachineName{get;set;} ...