JZOJ 1421. Binary Tree

topic

Description

  In an infinite binary tree in full, it has the following characteristics:
  (1) Each node has two sons - left and right son son;
  (2) if a node number is X, then its left son number of 2X, the right son 1 + 2X;
  (. 3) the root node number 1.
  Now go from the root, every step has three options: go left his son, his son went to the right and stay still.
  With the letter "L" indicates the left son went, "R" denotes the right son come, "P" represents a stop in place, the three-letter string representation of a clear routes.
The value of a clear routes to reach the final number of nodes, such as LR value to 5, and RPP value to 3.
  We use the character "L", "R", "P" and "*" represents a character string composed of a set of routes, where "*" may be any one of "L", "R", "P" in All this with the string matching routes are considered to be feasible.
  E.g. L * R contains LLR, LRR and LPR. The ** includes LL, LR, LP, RL, RR, RP, PL, PR , and PP these nine routes.
  A set value of the sum of all the routes that match the route pattern and the Value. Please programming to calculate the value of a given route.
 

Input

  Enter a string representing a set of routes, which contains only the "L", "R", "P" and "*" four kinds of characters, the length of not more than 10,000.

Output

  The output value of the route.
 

Sample Input

Input 1:
P*P

Input 2:
L*R

Input 3:
**

Input 4:
LLLLLRRRRRLLLLLRRRRRLLLLLRRRRRLLLLL

Sample Output

Output 1:
6

Output 2:
25

Output 3:
33

Output 4:
35400942560
 

Data Constraint

 
 

Hint

[Data] range
  of 30% to meet the scheme of data free "*";
  50% of the data satisfies a maximum of only three "*."

Source / Author: COCI2008

analysis

  •  
     Suppose after the k * Number, constructed of 3 ^ k different numbers.
    The next is L: 2 * Each number at the same time, i.e. the sum of 2 *
    next is R: each of the number of simultaneous * 2 + 1, i.e., the sum of the number of * (3 ^ k) 2 + 1 * number of
    next * bit is: the number k becomes 3 ^ 3 ^ (k + 1) the number of
    which does not change number k ^ 3, the sum of 1 *
    additional 3 plus a number k ^ L, 2 * sum
    also 3 plus a number k ^ R, 1 + 2 * number of each, i.e. the sum of the number of * 2 + 1 * number (3 ^ k)
    in summary, the sum * 5 + 3 ^ k.
    Plus a charge-bit precision gone.

 

Code

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 const int N=1e4+10,mo=1e8;
 6 char s[N];
 7 int f[N],p[N];
 8 long long ans=0;
 9 void mul(int x)
10 {
11     for (int i=f[0];i>=1;i--) f[i]*=x,f[i+1]+=f[i]/mo,f[i]%=mo;
12     if (f[f[0]+1]) f[0]++;
13 } 
14 void tmp()
15 {
16     for (int i=p[0];i>=1;i--) p[i]*=3,p[i+1]+=p[i]/mo,p[i]%=mo;
17     if (p[p[0]+1]) p[0]++;
18 }
19 void add()
20 {
21     for (int i=1;i<=max(p[0],f[0]);i++) f[i]+=p[i],f[i+1]+=f[i]/mo,f[i]%=mo;
22     if (f[f[0]+1]) f[0]++;
23 }
24 int main ()
25 {
26     int len;
27     cin>>s+1;
28     len=strlen(s+1);
29     p[0]=p[1]=f[0]=f[1]=1;
30     for (int i=1;i<=len;i++)
31     {
32         if (s[i]=='L') mul(2);
33         else if (s[i]=='R') mul(2),add();
34         else if (s[i]=='*') mul(5),add(),tmp(); 
35     }
36     cout<<f[f[0]];
37     for (int i=f[0]-1;i>=1;i--) printf("%08d",f[i]);
38 }

 

Guess you like

Origin www.cnblogs.com/zjzjzj/p/11100026.html