跳到正文

题解

UVA11350 Stern-Brocot Tree 题解

EastPorridge约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年1月31日
更新
2026年11月9日
篇幅
约 6 分钟
题目
UVA11350

版本

  1. v2当前
  2. v1

两种写法。

题目分析:

正常思路\text{正常思路}

观察题目所给图像进行思考,我们可以维护三个分数,l=a1b1,now=a2b2,r=a3b3l= \frac{a_1}{b_1},now=\frac{a_2}{b_2},r=\frac{a_3}{b_3},nownow 表示现在在哪里,ll 表示在 nownow 左边离 nownow 最近的分数,rr 表示在 nownow 右边离 nownow 最近的分数,初始化 l=01,now=11,r=10l= \frac{0}{1},now=\frac{1}{1},r=\frac{1}{0}。

枚举读入的每一个字符,如果是 LL,ll 不变,rr 变成原来的 nownow,nownow 变成 a1+a2b1+b2\frac{a_1+a_2}{b_1+b_2};RR 同理,rr 不变,ll 变成原来的 nownow,nownow 变成 a2+a3b2+b3\frac{a_2+a_3}{b_2+b_3}。最后 nownow 就是答案。

时间复杂度:O(n)O(n)。

Code.

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node {int x,y;}; char s[1010]; int t,x,y;
signed main()
{
	scanf("%lld",&t);
	while(t --)
	{
		scanf("%s",s+1); node l={0,1},r={1,0},pl={1,1};
		for(int i=1;i<=(int)strlen(s+1);i++)
		if(s[i] == 'L') {r=pl; pl={l.x+pl.x,l.y+pl.y};}
		else {l=pl; pl={pl.x+r.x,pl.y+r.y};}
		printf("%lld/%lld\n",pl.x,pl.y);
	}
	return 0;
}

把灵魂献祭给恶魔淬炼出的不可名状之物\text{把灵魂献祭给恶魔淬炼出的不可名状之物}

本人坐考场上想的奇怪东西。

我们可以将 L,RL,R 的转移量分别用矩阵表示,SS 为当前字符串,f(M(S))f(M(S)) 就是当前字符串的矩阵表示出来的分数。

M(L)=[1101],M(R)=[1011]M(L)=\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix},M(R)=\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}

那么针对任意一个 L,RL,R 字符串,我们都可以将它变成若干个矩阵相乘的形式,以题目第二个样例为例:

M(RLR)=M(R)M(L)M(R)=[1011][1101][1011]=[2132]M(RLR)=M(R)M(L)M(R)=\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 3 & 2 \end{bmatrix}

所以答案就是:

f(M(RLR))=f([2132])=3+22+1=53f(M(RLR))=f(\begin{bmatrix} 2 & 1 \\ 3 & 2 \end{bmatrix}) =\frac{3+2}{2+1} = \frac{5}{3}

推广到一般形式:

f(M(S))=f([n1n2m1m2])=m1+m2n1+n2f(M(S))=f(\begin{bmatrix} n_1 & n_2 \\ m_1 & m_2 \end{bmatrix}) =\frac{m_1 + m_2}{n_1 + n_2}

然后我们发现搞成这样并没有什么实质性的优化,直接乘是 O(23×n)O(2^3 \times n) 的,它甚至还慢了,所以我们考虑一点矩阵上的优化。

显然,我们面对连续乘一堆 LL 或 RR 的时候,我们并不需要都乘,把它变成乘方的形式,举例:

f(M(LLLRRL))=f(M(L)3M(R)2M(L))f(M(LLLRRL)) = f(M(L)^3M(R)^2M(L))

然后就可以上矩阵快速幂了。

时间复杂度:O(log⁡n∼n)O(\log n \sim n),自带一个 232^3 的常数,按理说 nn 越长效果越明显,这里只是不怎么明显罢了。

Code.

C++
#include<bits/stdc++.h>
#define int long long
using namespace std; char ch[110],top;
struct node {int a[3][3]; node() {memset(a,0,sizeof a);}} L,R;
node operator *(const node &a,const node &b)
{
	node res;
	for(int k=1;k<=2;k++)
		for(int i=1;i<=2;i++)
			for(int j=1;j<=2;j++)
				res.a[i][j]+=a.a[i][k]*b.a[k][j];
	return res;
}
node ksm(node a,int b)
{
	node res; for(int i=1;i<=2;i++) res.a[i][i]=1;
	while(b) {if(b&1ll) res=(res*a); a=(a*a); b>>=1ll;}
	return res;
}
signed main()
{
	L.a[1][1]=1; L.a[1][2]=1; L.a[2][1]=0; L.a[2][2]=1;
	R.a[1][1]=1; R.a[1][2]=0; R.a[2][1]=1; R.a[2][2]=1;
	int t; scanf("%lld",&t);
	while(t -- )
	{
		node res; for(int i=1;i<=2;i++) res.a[i][i]=1;
		scanf("%s",ch+1);
		for(int i=1,l=1;i<=(int)strlen(ch+1);i++)
		{
			if(i == 1) top=ch[1];
			else if(top != ch[i] || i == (int)strlen(ch+1))
			{
				if(top == 'L') res=(res*ksm(L,i-1-l+1)),top='R',l=i;
				else if(top == 'R') res=(res*ksm(R,i-1-l+1)),top='L',l=i;
			}
		}
		if(ch[strlen(ch+1)] == 'L') res=(res*L); else res=(res*R);
		int jk=res.a[2][1]+res.a[2][2],dk=res.a[1][1]+res.a[1][2],cd=__gcd(jk,dk);
		printf("%lld/%lld\n",jk/cd,dk/cd);
	}
	return 0;
}

研究了一个并没有什么用的东西导致后面挂分了。

原文
存档评论1 条抓取于