In this submission :https://codeforces.com/contest/1775/submission/207782995 I am getting MLE i have scartched my head for hours and its still not resolving,can any comrade help this noob?

# | User | Rating |
---|---|---|

1 | tourist | 3751 |

2 | Benq | 3727 |

3 | cnnfls_csy | 3691 |

4 | Radewoosh | 3651 |

5 | jiangly | 3632 |

6 | orzdevinwang | 3559 |

7 | -0.5 | 3545 |

8 | inaFSTream | 3478 |

9 | fantasy | 3468 |

10 | Rebelz | 3415 |

# | User | Contrib. |
---|---|---|

1 | adamant | 178 |

2 | awoo | 167 |

3 | BledDest | 165 |

4 | Um_nik | 164 |

5 | maroonrk | 163 |

6 | SecondThread | 160 |

7 | nor | 157 |

8 | -is-this-fft- | 154 |

9 | kostka | 146 |

10 | Geothermal | 144 |

In this submission :https://codeforces.com/contest/1775/submission/207782995 I am getting MLE i have scartched my head for hours and its still not resolving,can any comrade help this noob?

↑

↓

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/27/2023 09:08:27 (h1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

(I think maybe you have read the tutorial and trying to do the same thing.)

I just take a quick look. The problem seems to be that there are too many edges. When you build the graph, it's enough to consider only prime factors instead of all the factors because if two numbers have the same common factor, they must have a common prime factor.

can you tell me in which cases MLE errors usually occur i have done that editorial thing still mle

With $$$n$$$ nodes, in the worst case your graph has $$$O(n^2)$$$ edges. This means you can potentially have over $$$10^{10}$$$ values in your adjacency list. Multiplying that by 4 bytes per int, this far exceeds the 256MB memory limit.

Try to implement an algorithm that does not construct this adjacency list.

(What you are going to do is to construct a bipartite graph that one part represents the really nodes and the other stands for prime factors.)

Ya, it seems you only add prime factors (Maybe you should check yourself again). However, you don't need an entry for composite numbers. I think it may pass after getting rid of it.

UPD: I have tried my own and got AC. Here is the code. https://codeforces.com/contest/1775/submission/207871816

Even if you just simply add 3e5 nodes, it'll still pass and is quite lower than the limit. https://codeforces.com/contest/1775/submission/207875652

I found another problem in your code. Why the number of entries is

`(2*M)+1`

? What if M is small? Maybe it tried to use some unitializated memory as a vector due to out-of-bound access and caused a unexpected behavior which allocated a lot of memory. (I'm just guessing.)I forgot the line if (par[x] != -1) continue; in the bfs method, added it and it got ac:https://codeforces.com/problemset/submission/1775/207884247

thanks for all your suggestions

Just don't submit the code:)