Codeforces 909 C. Python Indentation (dp+ Tree array optimization)

Source: Internet
Author: User

Topic Link: Python indentation

Test instructions

Python does not have curly braces to indicate a block of statements, but rather a strict indentation. There is now a simplified version of Python, with only two statements:

(1) ' s ' statement: simple statements. Equivalent to a general statement. (2) ' F ' statement: for statements. Corresponds to A For loop and specifies that its loop body cannot be empty.

Then give you a non-indented Python program, a total of n rows (n <= 5000). Ask you how many legitimate and different Python programs are available after you add indentation.

Solution: Problem Analysis

DP in the past, if the first position is ' F ', dp[i][j] only add to dp[i+1][j+1], if it is ' s ' can be added to all the numbers from dp[i][0,j]. Interval wigs can be optimized with a tree-like array to reduce the complexity to (Nxnxlog (n)). This problem let me find a long long operation is a bit slower than int, there is a mod operation is really slow, encounter mod operation can reduce the time with If.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intMax_n = 5e3+9;4 Const intMOD = 1e9+7;5 CharVec[max_n];6 intDp[max_n][max_n];7 voidAddintPosintXintnum)8 {9     if(num<0) num+=MOD;Ten      for(; x<max_n;x+= (x&-x)) One     { Adp[pos][x]=dp[pos][x]+num; -         if(Dp[pos][x]>=mod) dp[pos][x]-=MOD; -     } the } - intSumintPosintx) - { -     intAns =0; +      for(;x>0; x-= (x&-x)) -     { +ans=ans+Dp[pos][x]; A         if(Ans>=mod) ans-=MOD; at     } -     returnans; - } - intMain () - { -     intn,m,t; in      while(cin>>N) -     { toMemset (DP,0,sizeof(DP)); +          for(intI=1; i<=n;i++) -         { theCin>>Vec[i]; *         } $dp[1][1] =1;Panax Notoginseng          for(intI=1; i<=n;i++) -         { the              for(intj=1; j<=i;j++) +             { A                 intt =sum (i,j); the                 if(Vec[i] = ='F') +                 { -Add (i+1, j+1, T); $Add (i+1, j+2,-t); $                 } -                 Else -                 { theAdd (i+1,1, T); -Add (i+1, j+1,-t);Wuyi                 } the             } -         } Wu         intAns =0; -          for(intI=1; i<=n;i++) About         { $Ans = ans+sum (n,i); -             if(Ans>=mod) ans-=MOD; -         } -cout<< (Ans+mod)%mod<<Endl; A     } +     return 0; the}

Codeforces 909 C. Python Indentation (dp+ Tree array optimization)

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.