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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | 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 | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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 |
---|