QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#390863#4771. Bird treeI_Love_Sonechka#AC ✓2ms4268kbC++171.2kb2024-04-16 00:09:032024-04-16 00:09:04

Judging History

你现在查看的是最新测评结果

  • [2024-04-16 00:09:04]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:4268kb
  • [2024-04-16 00:09:03]
  • 提交

answer

#include <bits/stdc++.h>
#include <math.h>
			
using namespace std;

// c++ short types
#define Int long long
#define vt vector

void solver() {
	int a, b; char c;
	cin >> a >> c >> b;
	// (a, b, h+1, id) -> (p_a, p_b, h, prev_id)
	// if from left: id = prev_id + 2 ^ h
	// if from right:id = prev_id + 2 * 2 ^ h
	vt<int> is_left, bits ;
	auto Rec = [&](auto self, int a, int b) -> void {
		if(a == b) {
			return ;
		}
		if(a < b) {
			// 1 / (x+1) = a / b
			// x + 1 = b / a
			// x = (b-a)/a
			is_left.push_back(1);
			self(self, b-a, a);
			bits.push_back(1);
		} else {
			// 1/x + 1 = a/b
			// 1/x = (a-b)/b
			is_left.push_back(0);
			self(self, b, a-b);
			bits.push_back(2);
		}
	};
	Rec(Rec, a, b);
	bits[0] ++ ;
	for(int i = 0; i < bits.size(); ++i) {
		int cnt = bits[i] / 2;
		if(cnt) {
			if(i == bits.size()-1) {
				bits.push_back(0);
			}
			bits[i+1] += cnt;
		}
		bits[i] %= 2;
	}
	bits.pop_back();
	string res;
	for(int x: bits) {
		res += "LR"[x];
	}
	reverse(res.begin(), res.end());
	cout << res << "\n";
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int tt = 1;
	cin >> tt;
	for(int t = 0; t < tt; ++t) {
    solver();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3580kb

input:

3
1/2
2/5
7/3

output:

L
LRR
RLLR

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 4268kb

input:

96
1/2
1/3
1/4
1/5
1/6
1/7
1/8
2/1
2/3
2/5
2/7
3/1
3/2
3/4
3/5
3/7
3/8
4/1
4/3
4/5
4/7
5/1
5/2
5/3
5/4
5/6
5/7
5/8
6/1
6/5
6/7
7/1
7/2
7/3
7/4
7/5
7/6
7/8
8/1
8/3
8/5
8/7
10000/9999
9999/10000
1/10000
10000/1
1000000000/300001
300001/1000000000
701408733/433494437
433494437/701408733
380211999/53852...

output:

L
LR
LRL
LRLR
LRLRL
LRLRLR
LRLRLRL
R
LL
LRR
LRLL
RL
RR
LLR
LLL
LRRL
LRRR
RLR
RRL
LLRL
LLLR
RLRL
RLL
RRR
RRLR
LLRLR
LLRR
LLLL
RLRLR
RRLRL
LLRLRL
RLRLRL
RLRR
RLLR
RRRL
RRLL
RRLRLR
LLRLRLR
RLRLRLR
RLLL
RRRR
RRLRLRL
RRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLR...

result:

ok 96 lines