Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems.
×

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

1 | MiFaFaOvO | 3681 |

2 | tourist | 3669 |

3 | Um_nik | 3535 |

4 | 300iq | 3317 |

5 | ecnerwala | 3294 |

6 | maroonrk | 3268 |

7 | TLE | 3223 |

8 | scott_wu | 3209 |

9 | WZYYN | 3180 |

10 | boboniu | 3174 |

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

1 | Errichto | 197 |

2 | antontrygubO_o | 187 |

3 | pikmike | 183 |

4 | Ashishgup | 182 |

5 | vovuh | 179 |

6 | Radewoosh | 168 |

7 | Um_nik | 164 |

8 | tourist | 163 |

8 | Monogon | 163 |

10 | McDic | 162 |

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/07/2020 23:29:38 (i2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

the input size if small (the largest factorial you will need is 20!) so here you can generate all the possible factorials and use bitmasking (or recursion) to pre-calculate all possible values and cache them ... then for every case you just output the answer

I pre-calculate all the value upto 20. But now i cant understand how can i find my desired number .

I have no idea about bitmasking. Is there any other way ?

Use brute-force. For each number there are two cases — to use or not to use :)

If i use brute-force, at some stage summation of fact(num) may exceed my desired n , at that case i may avoid that fact(num) . But can i surely say that at any stage summation of fact(num) will be equal to my desired n ?

I think that this approach will result in TLE since the limit is 0.5 :) I think it is better to read Caraz96's idea and ask if you misunderstood something in it.

It is possible to use a greedy approach: since fact(n) > fact(0)+fact(1)+...+fact(n-1) (this is true for each n > 2) you can iterate from the greatest factorial(20!) to the smallest one, if fact[i] is less than or equal to N you must take it, decrease N by fact[i] and proceed with the next factorial. At the end, if N is 0 you found the solution, otherwise it is impossible to obtain N adding factorials.

Thanks. Got AC.

Thank you! got ac :D

Exactly I started it today and I went totally in this way . As a very beginner I was amazed when I saw that other coders also think in the same way .