1. 程式人生 > >2018徐州邀請賽I題 I. T-shirt

2018徐州邀請賽I題 I. T-shirt

JSZKC is going to spend his vacation!

His vacation has NN 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 MM different T-shirt with different color. If he wears AA color T- shirt this day and BBcolor T-shirt the next day, then he will get the pleasure of f[A][B]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,MN,M (2 \le N \le 100000, 1 \le M \le 100)(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 f[i][j]f[i][j] (1\le f[i][j]\le 1000000)(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

題目來源

思路: 本題就是讓你求一個n-1的序列。f[a][b]+f[b][c]+f[c][d]+~~~~+  f[x][y]+f[y][z]的最大值。

那麼第0 天(也就是題目中的第一天) 可以穿任意的衣服,那麼第一天就根據第0 天的衣服確定最大價值,我們可以設dp[time][i][j] 表示第0天穿i 第time天穿j 的最大價值,那麼我們就可以得出 dp[a+b][i][j] =max(dp[a][i][k]+dp[b][k][j])。那麼對於該式子我們可以用快速冪處理來降低時間複雜度。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N =105;

struct node
{
    ll ma[N][N];
};
int n,m;

node mut(node a,node b)
{
    node ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
            for(int k=1;k<=m;k++){
                ans.ma[i][j]=max(ans.ma[i][j],a.ma[i][k]+b.ma[k][j]);
            }
        }
    }
    return ans;
}

node quick_pow(node a,int k)
{
    node ans;
    memset(ans.ma,0,sizeof(ans.ma));
    while(k)
    {
        if(k&1) ans=mut(ans,a);
        a=mut(a,a);
        k>>=1;
    }
    return ans;
}

int main()
{
    node tmp;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                scanf("%lld",&tmp.ma[i][j]);
            }
        }

        node ans=quick_pow(tmp,n-1);

        ll maxx=0;
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                maxx=max(maxx,ans.ma[i][j]);
            }
        }
        printf("%lld\n",maxx);
    }

    return 0;
}

相關推薦

2018徐州邀請賽I I. T-shirt

JSZKC is going to spend his vacation! His vacation has NN days. Each day, he can choose a T-shirt to wear. Obviously, he doesn't want to

ACM-ICPC 2018 徐州賽區網路預賽 I

Characters with Hash Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encrypted value. For instance, he

ACM-ICPC 2018 徐州賽區現場賽 I. Rikka with Sorting Networks (思維+DFS)

題目連結:https://codeforces.com/gym/102012/problem/I 題意:問有多少個 1 到 n 的排列,使得用給定的 k 個比較器(使 au 和 av 有序)排序後,整個序列的最長上升子序列為 n - 1。 題解:

【倍增】T-shirt @2018acm徐州邀請賽 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

ACM-ICPC 2018徐州網絡賽-H Ryuji doesn't want to study

als ryu using 網絡 nan spa upd div names C*M....死於update的一個long long寫成int了 心累 不想寫過程了 ******** 樹狀數組,一個平的一個斜著的,怎麽斜都行 題庫鏈接:https://nanti.ji

2018 ACM-ICPC 亞洲區域賽北京現場賽 I

做法:打表找規律 大數是過不了這個題的(但可以用來打表) 先找k的字首,字首對應邊緣數字是哪個 如果第0位是2-9 對應奇數長度的1-8 第0位為1時,第1位為0時對應奇數長度的9,為1-9時對應偶數長度的1-9,剩下的根據奇偶判斷先從頭到尾再從尾到頭跑一編即可 如100會得到909 100234會得

Gym 101667I Slot Machines (2017-2018 ACM-ICPC, Asia Daejeon Regional Contest I)

傳送門:http://codeforces.com/gym/101667 Problem I Slot Machines Time Limit: 2 Seconds 題意:陣列a有n個數字,數字範圍0~999999希望求一組最小的k+p,使得任意i>k,都有a[i]=a[i+p],

2018全國多校1-I

唯一的一道題一次過QAQ ٩(๑>◡<๑)۶ https://www.nowcoder.com/acm/contest/67/I #include<stdio.h> #include<string.h> int pd[1005]; int a,

ACM-ICPC 2018 徐州賽區網路預賽(I + H)(水模擬 + 線段樹)

I 題意 給定一個字串ss和一個字元LL,將所有的 |(int)(L−s[i])||(int)(L−s[i])| 轉化為一個兩位數後,按順序拼接在一起,問,這個拼接而成的新序列,去掉前導0後的長度是

ACM-ICPC北京賽區2018重現賽IPalindromes

記錄一下自己的思路等有空了再用程式碼實現。根據打表找到的規律. 1-10    9個 10-99    9個 100 999    90個 1000 9999   90個 10000 99999   900個 100000 999999   900個 也就是說,

2017Summmer_上海金馬五校 F,GI,K

sum 組隊 時序 而且 sizeof bit 屬性排序 print 浪費 以下題目均自己搜 F題 A序列 一開始真的沒懂題目什麽意思,還以為是要連續的子串,結果發現時序列,簡直智障,知道題意之後,好久沒搞LIS,有點忘了,復習一波以後,直接雙向LIS,處理處兩個數組L和

ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽 i Minimum(線段樹)

hellip each pri ger out ont amp define void 描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two typ

HDU 5886 Tower Defence(2016青島網絡賽 I,樹的直徑 + DP)

printf true IT spa clear 期望 post const href 題目鏈接 2016 Qingdao Online Problem I 題意 在一棵給定的樹上刪掉一條邊,求剩下兩棵樹的樹的直徑中較長那的那個長度的期望,答案乘上$n-1$後輸出。

hdu-2685 I won't tell you this is about number theory---gcd和快速冪的性質

return ont 題目 def clas number class HR strong 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=2685 題目大意: 求gcd(am-1,an-1)%k 解題思路: 對於am-1 =

Kayleigh O'Connor - I Won't Be There

over was about cau ember lin HR AS when Do you feel like you‘re about to drown The wave is rushing over you throw you onto now I remem

2017 ACM/ICPC 新疆賽區 I A Possible Tree 帶權並查集

沒有 cpc ant tar tin 傳送門 表示 每天 blank 傳送門 題意:給定一棵帶權樹的形態, 但是並不知道每天條邊的具體權重. 然後給m個信息, 信息格式為u v val, 表示在樹上u 到 v 的路徑上經過的邊的權重的異或和為val, 問前面最多有多少個

ACM-ICPC 2018 徐州賽區網絡預賽 H. Ryuji doesn't want to study (線段樹)

ace esp repr for lin n) mat put 鏈接 Ryuji is not a good student, and he doesn‘t want to study. But there are n books he should learn, each

Ryuji doesn't want to study 2018徐州icpc網絡賽 樹狀數組

vertical 題目 date cpc ear typedef -a void return Ryuji is not a good student, and he doesn‘t want to study. But there are n books he shou

2018徐州網絡賽H. Ryuji doesn't want to study

problem test case numeric -html mac ros posit cpp tex 題目鏈接: https://nanti.jisuanke.com/t/31458 題解: 建立兩個樹狀數組,第一個是,a[1]*n+a[2]*(n-1).

ACM-ICPC 2018 徐州賽區網絡預賽 H Ryuji doesn't want to study (樹狀數組差分)

() clas targe anti icpc -- nbsp nan ant https://nanti.jisuanke.com/t/31460 題意 兩個操作。1:查詢區間[l,r]的和,設長度為L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+.