NOIP2007 matrix fetch number [dp| high precision]

Source: Internet
Author: User

Title Description

Handsome often play with classmates a matrix take number game: for a given n*m matrix, each element in the matrix AIJ is a nonnegative integer. The rules of the game are as follows:

1. Each fetch must take one element from each row, a total of n. After M times, all elements of the matrix are taken out;

2. Each element taken away can only be the beginning or end of the line where the element is located;

3. Each fetch has a score value, the sum of the scores for each row, the number of points per row = The value of the element taken away *2^i, where I is the number of times I fetch (starting from 1);

4. The end of the game must be divided into M-time score of the sum.

Handsome want to ask you to help write a program, for any matrix, you can find out the maximum score after the number.

Input/output format

Input format:

The input file game.in includes the n+1 line:

1th Act two integers separated by spaces N and M.

The 2~n+1 behavior n*m Matrix, where each line has m a nonnegative integer separated by a single space.

Data range:

60% of data meet: 1<=n, m<=30, answer no more than 10^16

100% data satisfies: 1<=n, m<=80,0<=aij<=1000

Output format:

The output file Game.out contains only 1 rows, an integer, which is the maximum score after the input matrix is taken.

Input and Output Sample input example # #:
2 31 2 33 4 2
Sample # # of output:
82
Description

NOIP 2007 Raising the third question

-------------------------------------

Dp

The rows and rows are independent from each other, and each row is solved separately; must be emptied first .

F (i,j) left I selected, the maximum score of J for the right

State transfer well thought, attention to border processing

High precision [This time starting from array 1]

High height, high multiplication and low

Be careful when outputting 0

It is recommended to write a long long to facilitate debugging

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intn= -;Const intB=1e4,l= -;structbig{intSize,d[l]; Big (intA=1): Size (a) {memset (d,0,sizeof(int)*L);}};voidJia (Big &a,big &b) {    intg=0, I;  for(i=1;; i++){        if(g==0&&i>a.size&&i>b.size) Break; inttmp=G; if(i<=a.size) tmp+=A.d[i]; if(i<=b.size) tmp+=B.d[i]; A.d[i]=tmp%C; G=tmp/C; } a.size=i-1;}voidChengint (Big &a,intk) {    intg=0, I;  for(i=1; i<=a.size;i++){        inttmp=a.d[i]*K; A.d[i]= (tmp+g)%C; G= (tmp+g)/B; }     while(g) {a.d[++a.size]=g%B; G/=C; }}voidCopy (Big &t,big &s) {T.size=s.size;  for(intI=1; i<=s.size;i++) t.d[i]=s.d[i];} Big Max (Big&a,big &b) {    if(a.size>b.size)returnA; if(a.size<b.size)returnb;  for(inti=a.size;i>=1; i--){        if(A.d[i]>b.d[i])returnA; if(A.d[i]<b.d[i])returnb; }    returnA;}voidPrint (Big &a) {     for(inti=a.size;i>=1; i--){        if(i==a.size); Else if(a.d[i]<Ten) cout<<"0000"; Else if(a.d[i]< -) cout<<"xx"; Else if(a.d[i]< +) cout<<"0"; cout<<A.d[i]; } cout<<"\ n";}intn,m;inta[n][n];big f[n][n];big Ans,fin=0; big Two[n];voidPRE () {two[0].d[1]=1;  for(intI=1; i<=m+1; i++) {Copy (Two[i],two[i-1]); Chengint (Two[i],2); }}big Sol (intnum) {ans=big ();  for(intI=0; i<=n;i++)         for(intj=0; j<=n;j++) f[i][j]=big (); f[1][0].d[1]=a[num][1]*2; f[0][1].d[1]=a[num][m]*2;  for(intI=2; i<=m;i++){//f[i][0]=f[i-1][0]+a[num][i]* (LL) 1<<i);//f[0][i]=f[0][i-1]+a[num][m-i+1]* (LL) 1<<i);Copy (f[i][0],f[i-1][0]); Big TMP; Copy (Tmp,two[i]);        Chengint (Tmp,a[num][i]); Jia (f[i][0],tmp); Copy (f[0][i],f[0][i-1]); Big TMP2; Copy (Tmp2,two[i]); Chengint (Tmp2,a[num][m-i+1]); Jia (f[0][I],TMP2); }     for(intI=1; i<=m;i++)         for(intj=1; j<=m-i;j++){//F[i][j]=max (f[i-1][j]+a[num][i]* (LL) 1<< (i+j)),//f[i][j-1]+a[num][m-j+1]* (LL) 1<< (i+j));big X, y; Copy (X,f[i-1][j]); Big TMP; Copy (Tmp,two[i+J]);            Chengint (Tmp,a[num][i]);                        Jia (x,tmp); Copy (Y,f[i][j-1]); Big TMP2; Copy (Tmp2,two[i+J]); Chengint (tmp2,a[num][m-j+1]);                        Jia (Y,TMP2); F[I][J]=Max (x, y); }         for(intI=0; i<=m;i++) Ans=max (ans,f[i][m-i]); returnans;}intMain () {//System ("pause");Cin>>n>>m;    PRE ();  for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++) Cin>>A[i][j];  for(intI=1; i<=n;i++) {Big tmp=Sol (i); Jia (fin,tmp);}    Print (Fin); }

NOIP2007 matrix fetch number [dp| high precision]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.