QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#741274#7953. Product Deliveryyuto1115#WA 0ms3740kbC++201.6kb2024-11-13 14:00:152024-11-13 14:00:15

Judging History

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

  • [2024-11-13 14:00:15]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3740kb
  • [2024-11-13 14:00:15]
  • 提交

answer

#include<bits/stdc++.h>
#define rep2(i,j,k) for(ll i = ll(j); i < ll(k); i++)
#define rep(i,k) rep2(i,0,k)
#define rrep2(i,j,k) for(ll i = ll(j)-1; i >= ll(k); i--)
#define rrep(i,k) rrep2(i,k,0)
#define eb emplace_back
#define SZ(a) ll(a.size())
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
const ll inf = LLONG_MAX/4;
bool chmin(auto& a, auto b) {return a>b ? a=b, 1 : 0;}
bool chmax(auto& a, auto b) {return a<b ? a=b, 1 : 0;}

vector<char> v;
int n;
int it = 0;

void out() {
	cout << -1 << endl;
	exit(0);
}

using ull = unsigned long long;

struct frac { 
	ull p, q;
	frac(ull p_=0, ull q_=1) : p(p_), q(q_) {
		ull g = gcd(p, q);
		p /= g;
		q /= g;
	}
};

frac add(frac a, frac b) {
	ull q = lcm(a.q, b.q);
	ull p = a.p * (q / a.q);
	p += b.p * (q / b.q);
	return frac(p, q);
}

frac div(frac a, frac b) {
	swap(b.p, b.q);
	ull g = gcd(a.q, b.p);
	a.q /= g, b.p /= g;
	g = gcd(b.q, a.p);
	b.q /= g, a.p /= g;
	return frac(a.p * b.p, a.q * b.q);
}

frac f();
frac g();

frac f() {
	if(it == n or v[it] != '(') out();
	++it;
	vector<frac> fs(3);
	rep(i, 3) fs[i] = g();
	if(it == n or v[it] != ')') out();
	++it;
	return add(fs[0], div(fs[1], fs[2]));
}

frac g() {
	if(it == n) out();
	if(v[it] == '(') return f();
	else if(isdigit(v[it])) {
		++it;
		return frac(v[it]-'0', 1);
	}
	else out();
	assert(false);
}

int main(){
	cin.tie(0) -> sync_with_stdio(0);	
	cin >> n;
	v.resize(n);
	rep(i, n) cin >> v[i];
	auto ans = f();
	cout << ans.p << ' ' << ans.q << endl;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3740kb

input:

4
13 15
5 8
6 14
3 7

output:

-1

result:

wrong answer 1st lines differ - expected: '2', found: '-1'