QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#741274 | #7953. Product Delivery | yuto1115# | WA | 0ms | 3740kb | C++20 | 1.6kb | 2024-11-13 14:00:15 | 2024-11-13 14:00:15 |
Judging History
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;
}
Details
Tip: Click on the bar to expand more detailed information
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'