Problem B: cxlove is a good man
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 121 Solved: 27
[ Submit][ Status][ Web Board]
Description
众所周知,爱酱(cxlove)是个绝世好男人。
好男人的标准之一就是能解决妹子提出的一切问题!
有一天,好奇宝宝娜娜做题时发现了一种神器的树。树的前几层如下图:
如你所见:

除开最上面两个奇怪的东西之外,树的每个节点上都有一个数字,
某个节点的分子是在该节点左上的第一个节点和其右上节点的分子的和,
某个节点的分母是在该节点的左上的第一个节点和其右上第一个节点的分母的和。
例如, 2/3 这个节点的左上第一个节点是1/2,右上第一个节点是 1/1。
现在给定从根节点到某个节点的路径。
用L(x),R(x)表示。L(x)表示像沿着左子树走了x步,R(x)表示沿着右走了x步,
例如:L(2)R(1)L(1) 表示3/8
Input
一个数字n
接下来n行,每行一个字母a,一个数字b
a='L'表示沿着树往左走
a='R' 表示沿着树往右走
n<=10^7
所有b的和小于10^18
Output
两个数 p,q, 分别是到达节点的分子mod 1000000007 和分母mod 1000000007
Sample Input
1
R 5
3
R 2
R 5
R 3
R 5
3
R 2
R 5
R 3
Sample Output
6/1
11/1
11/1
HINT
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
#define LL long long
struct NOD
{
LL a,b;
};
LL re[10000009];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
char ch[5],c,c0;
LL tmp;
int n;
NOD a,b,ta,tb;
while(~scanf("%d",&n)){
if(n==0){
printf("1/1\n");
continue;
}
scanf("%s%lld",ch,&tmp);
if(ch[0]=='L')
c0 = c = 'L';
else
c0 = c = 'R';
int cnt = 0;
re[0] = tmp;
for(int i=1; i<n; i++){
scanf("%s%lld",ch,&tmp);
if(ch[0]==c)
re[cnt] += tmp;
else{
re[++cnt] = tmp;
c = ch[0];
}
}
for(int i=0; i<=cnt; i++)
re[i] = re[i]%1000000007;
b.a = b.b = 1;
if(c0=='L')
a.a = 0,a.b = 1;
else
a.a = 1,a.b = 0;
for(int i=0; i<=cnt; i++){
tb.a = (a.a*re[i]+b.a)%1000000007;
tb.b = (a.b*re[i]+b.b)%1000000007;
ta.a = (a.a*re[i]+b.a-a.a)%1000000007;
ta.b = (a.b*re[i]+b.b-a.b)%1000000007;
a = ta,b = tb;
}
cout<<b.a<<"/"<<b.b<<endl;
}
return 0;
}