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 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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 |
---|