I need help understanding where I am going wrong with this solution. Possibly I am doing wrong with modular operations.
This is the problem of recent Codeforces Round #778 , Problem D — Potion Brewing Class
My piece of code
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
Need help with modular operations
I need help understanding where I am going wrong with this solution. Possibly I am doing wrong with modular operations.
This is the problem of recent Codeforces Round #778 , Problem D — Potion Brewing Class
const int N = 2e5+3;
const lli MOD = 998244353;
vector<pair<lli, lli>> a(N);
void dfs(int node, int par, vector<tuple<int,lli,lli>> g[]) {
lli p = a[node].first;
lli q = a[node].second;
for(auto [j,x,y] : g[node]) {
if(j != par) {
a[j].first = (p % MOD * y % MOD) % MOD;
a[j].second = (q % MOD * x % MOD) % MOD;
}
}
for(auto [j,x,y] : g[node]) {
if(j != par) {
dfs(j,node,g);
}
}
}
lli LCM(lli a, lli b) {
return (a*b) / __gcd(a,b);
}
void solve(){
int n;
cin >> n;
vector<tuple<int,lli,lli>> g[n+1];
FOR(k,1,n-1) {
int i,j,x,y;
read(i,j,x,y);
g[i].pb({j,x,y});
g[j].pb({i,y,x});
}
a[1] = {1,1};
dfs(1,0,g);
lli lcm = 1 , ans = 0;
FOR(i,2,n) {
lcm = LCM(lcm, a[i].second);
lcm %= MOD;
}
FOR(i,1,n) {
ans += a[i].first * (lcm/a[i].second);
ans %= MOD;
}
ans /= __gcd(ans,lcm);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
solve();
return 0;
}
| Rev. | Lang. | By | When | Δ | Comment | |
|---|---|---|---|---|---|---|
| en1 |
|
nikhil_vaibhav_17 | 2022-03-23 13:17:58 | 1640 | Initial revision (published) |
| Name |
|---|


