[矩阵快速幂]T-shirt(2018江苏邀请赛I题)

题目描述

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

题意:求f[a][b]+f[b][c]+f[c][d]+...+f[p][q](有n-1项)的最大值;思路:从简单情况入手,当n=2时,求f[a][b]的最大值,两重循环遍历即可;当n=3时,求f[a][b]+f[b][c]的最大值,三重循环遍历即可;当n=4时,自然可以四重循环遍历,,,我们换一个角度考虑,可以将f[a][c]的值以f[a][b]+f[b][c]中的最大值替代(得到新的最优的f[1~m][1~m]——此时f[i][j]表示第一天穿i,第三天穿j可以得到的最大快乐),接着求f[a][c]+f[c][d]的最大值,回到n=3的情况,也就是进行三重循环遍历;当n=5时类似,将f[a][d]以f[a][c]+f[c][d]中的最值替代后,计算f[a][d]+f[d][e]的最值,与此同时,得到新的f[1~m][1~m];总结一下,相当于不断地更新f[1~m][1~m];一开始f[i][j]表示第1天穿i,第2天穿j的所能得到的最大快乐,更新k次后f[i][j]表示第1天穿i,第2+k天穿j所能得到的最大快乐;这样的话需要在原始f上更新n-2次得到最终f——f[i][j]表示第一天穿i,第n天穿j所能得到的最大快乐;每一次更新的复杂度为O(m^2),暴力n次更新复杂度O(n*m^2)会超时;可以将更新视为定义在f上的一种运算‘#‘,f经n-2次更新,可视为f#f#...#f(n-1个f相#),用“快速#”计算n-1个f相#即可;类似于矩阵快速幂;

AC代码:
#include <iostream>
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;

ll n,m;

struct Matrix{
  ll f[110][110];
  Matrix(){memset(f,0,sizeof(f));}
  Matrix operator *(Matrix & mat){
    Matrix ret;
    for(ll i=1;i<=m;i++){
        for(ll k=1;k<=m;k++){
            for(ll j=1;j<=m;j++){
                ret.f[i][j]=max(ret.f[i][j],mat.f[i][k]+f[k][j]);
            }
        }
    }
    return ret;
  }
};

Matrix qpow(Matrix a,ll b){
  Matrix ret;
  while(b){
    if(b&1) ret=ret*a;
    a=a*a;
    b>>=1;
  }
  return ret;
}

int main()
{
    while(scanf("%lld%lld",&n,&m)!=EOF){
        Matrix mat;
        for(ll i=1;i<=m;i++){
            for(ll j=1;j<=m;j++){
                scanf("%lld",&mat.f[i][j]);
            }
        }
        mat=qpow(mat,n-1);
        ll ans=0;
        for(ll i=1;i<=m;i++){
            for(ll j=1;j<=m;j++){
                ans=max(ans,mat.f[i][j]);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/lllxq/p/9157343.html

时间: 2024-08-30 12:22:24

[矩阵快速幂]T-shirt(2018江苏邀请赛I题)的相关文章

POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】

题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #include <iostream> #include <cstring> using namespace std; const int MOD = 10000; void mul(int f[2], int base[2][2]) { int c[2]; memset(c, 0, sizeof

POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)

Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5611   Accepted: 2209 Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout

HDOJ M斐波那契数列 4549【矩阵快速幂+快速幂+费马小定理+欧拉函数】

M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2096    Accepted Submission(s): 596 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给

Recursive sequence(HDU5950 矩阵快速幂)

题目: Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows wou

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k

2014 BNU 邀请赛E题(递推+矩阵快速幂)

Elegant String 题意:给定一个字符串,由0-k数字组成,要求该串中,子串不包含0-k全排列的方案数 思路:dp[i][j]表示放到i个数字,后面有j个不相同,然后想递推式,大概就是对应每种情况k分别能由那几种状态转移过来,在纸上画画就能构造出矩阵了,由于n很大,所以用快速幂解决 代码: #include <stdio.h> #include <string.h> const long long MOD = 20140518; int t; long long n; i

BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的某一个全排列 邀请赛上A的较多的一道题,比赛的时候死活想不出,回来之后突然就想通了,简直..... = =! 解题思路: 对于所有串我们都只考虑末尾最多有多少位能构成全排列的一部分(用l来表示),即最多有多少位不重复的数字出现,将问题转化为求末尾最多有k位能构成全排列的串的总数量 假设k为5,有一个

2014北京邀请赛E题-矩阵快速幂

题意:长度为n(1<=n<=10^18)的并且任意连续子串都不是0-k(1<=k<=9)的一个排列的字符串有多少种. 解法:矩阵快速幂.dp[i][j]表示i长度最后连续j个不同(即最后j个无重复,最后j+1个有重复)的字符串的个数.状态选好很重要.设计状态时最重要考虑是唯一性和可传递性,比赛时明明知道肯定是矩阵快速幂,但是一直没想到这个状态表示,自己设计的自己都不会转移. dp[i][j]有了后,后边加一个字符,这个字符可以是j之内的任意一个,也可以是j以外的,这样枚举每种情况,

华东交通大学2018年ACM“双基”程序设计竞赛 C. 公式题 (2) (矩阵快速幂)

题目链接:公式题 (2) 比赛链接:华东交通大学2018年ACM"双基"程序设计竞赛 题目描述 令f(n)=2f(n-1)+3f(n-2)+n,f(1)=1,f(2)=2 令g(n)=g(n-1)+f(n)+n*n,g(1)=2 告诉你n,输出g(n)的结果,结果对1e9+7取模 输入描述: 多组输入,每行一个整数n(1<=n<=1e9),如果输入为0,停止程序. 输出描述: 在一行中输出对应g(n)的值,结果对1e9+7取模. 示例1 输入 1 5 9 456 0 输出