Codeforces 677e-vanya and Balloons

Source: Internet
Author: User

Test instructions: Bomb-like game, but the cross of the bomb trajectory are equal length (that is, 4 directions) (Can fry ten, X), and then the result is the ability to blast the maximum value of the product of each lattice.

Analysis: A glance down is DP, but there are involved in 8 directions, so it is necessary to DP8 a direction respectively. Each direction is a one-dimensional DP.
And the size of the comparison will use to log, because to compare the size of "2^k1 and 3^k2 (K1>K2)", is not directly considered unrealistic, so the use of log into---"K1*log (2) and K2*log (3)" Size comparison.

Spit Time: This code is really difficult to play acridine, debugging N long ..... (The heart is broken)

#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intMOD = 1e9 +7;Const intMAXN = ++Ten;structnode{intn1,n2,n3,l; Node () {N1=n2=n3=l=0;} Node (Charc) {N1=n2=n3=l=0; if(c=='1') n1=l=1; Else if(c=='2') n2=l=1; Else if(c=='3') n3=l=1; } Nodeoperator+ (Node &a) {Node t; T.n1=N1+A.N1; T.N2=N2+A.N2; t.n3=n3+a.n3; returnT; } Nodeoperator-(Node &a) {Node t; T.n1=N1-A.N1; T.N2=N2-A.N2; t.n3=n3-a.n3; returnT; }};CharW[MAXN][MAXN]; Node dp[8][maxn][maxn], ans;intdy[8]={-1,-1,0,1,1,1,0,-1};intdx[8]={0,-1,-1,-1,0,1,1,1};intN; ll Quick_ (ll X,intN) {    if(x==0)return 0; LL ans=1;  while(n) {if(n&1) ans=ans*x%MOD; X=x*x%MOD; N>>=1; }    returnans;}voidPrin (Node No) {cout<<"N1:"<<no.n1<<"N2:"<<no.n2<<"N3:"<<no.n3<<Endl;}voidinit () {memset (W,'0',sizeof(w)); intI, J, Z, X, y; scanf ("%d", &N);  for(i=1; i<=n;++i) scanf ("%s", w[i]+1);  for(i=1; i<=n;++i) for(j=1; j<=n;++j) for(z=0;z<8; ++z) dp[z][i][j]=Node (W[i][j]); //DP8 each direction separately .     for(z=0;z<4;++z) for(i=1; i<=n;++i) for(j=1; j<=n;++j) {x=I+DX[Z]; y=j+Dy[z]; if(w[i][j]!='0') {Dp[z][i][j]=dp[z][i][j]+Dp[z][x][y]; DP[Z][I][J].L+=dp[z][x][y].l+1; }            }     for(z=4;z<6;++z) for(i=n;i>=1;--i) for(j=n;j>=1;--j) {x=I+DX[Z]; y=j+Dy[z]; if(w[i][j]!='0') {Dp[z][i][j]=dp[z][i][j]+Dp[z][x][y]; DP[Z][I][J].L+=dp[z][x][y].l+1; }            }     for(z=6;z<8;++z) for(i=n;i>=1;--i) for(j=1; j<=n;++j) {x=I+DX[Z]; y=j+Dy[z]; if(w[i][j]!='0') {Dp[z][i][j]=dp[z][i][j]+Dp[z][x][y]; DP[Z][I][J].L+=dp[z][x][y].l+1; }                    }}intMain () {DoubleTan1, Tan2, tan=0; intI, J, Z, Lnor, Lrot, X, y;    Init ();  for(i=1; i<=n;++i) for(j=1; j<=n;++j) {Node No, RO; Lrot=lnor=MAXN; //Normal             for(z=0;z<8; z+=2) Lnor=min (Lnor, DP[Z][I][J].L), no=no+Dp[z][i][j];  for(z=0;z<8; z+=2)            {                if(dp[z][i][j].l>Lnor) {x=i+dx[z]* (Lnor); y=j+dy[z]*(Lnor); No=no-Dp[z][x][y]; }            }            //Rotate             for(z=1;z<8; z+=2) Lrot=min (Lrot, DP[Z][I][J].L), ro=ro+Dp[z][i][j];  for(z=1;z<8; z+=2)            {                if(dp[z][i][j].l>Lrot) {x=i+dx[z]* (Lrot); y=j+dy[z]*(Lrot); Ro=ro-Dp[z][x][y]; }            }            //in the middle of the add more            if(w[i][j]=='1') ro.n1-=3, no.n1-=3; Else if(w[i][j]=='2') ro.n2-=3, no.n2-=3; Else if(w[i][j]=='3') ro.n3-=3, no.n3-=3;            Node T; Tan1=no.n2*log (2) +no.n3*log (3); Tan2=ro.n2*log (2) +ro.n3*log (3); if(Tan1>tan) ans=no,tan=Tan1; if(Tan2>tan) ans=ro,tan=tan2; if(Fabs (tan-0) <1e-9)            {                if(ans.n1==0&&no.n1!=0) ans=No; Else if(ans.n1==0&&ro.n1!=0) ans=ro; }} LL Res=quick_ (2, ans.n2) *quick_ (3, ans.n3)%MOD; if(ans.n2==0&&ans.n3==0&&ans.n1==0) res=0; cout<<res<<Endl;}

Codeforces 677e-vanya and Balloons

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.