Variety of solutions depending on contraints

Revision en1, by Coder, 2015-09-13 21:08:03

Suppose the input for a problem is positive integer N and answer happens to be 1/N. Here is what happens for different constraints on N.

  1. N <= 10^9 Everyone does math and all solutions are

cout << 1.0 / N

  1. N <= 10^6 You see solutions from above but some contestants decide to use iterative approach, something like

double ans = 1.0; for (int i = 2; i <= N; i++) ans = ans * i / (i — 1);

  1. N <= 10^5 Mostly solutions from above, but some contestants use recursion (maybe with memoization)

double f(int N) { if (N == 1) return 1.0; return f(N — 1) * N / (N — 1); }

  1. N <= 500 Solutions from above, but some contestants go with O(N^3) dynamic programming

  2. N <= 100 Some contestants realize that you can precompute all answers const double ans[100]={1.0, 0.5, 0.333333333, ... }

  3. N <= 36 You see solutions with meet-in-the middle approach

  4. N <= 20 You start to see a lot of bitwise operations in codes — Dynamic programming on subsets

  5. N <= 10 Some C++ codes use next_permutation(), Java coders having hard time

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English Coder 2015-09-13 21:33:59 63 Tiny change: 'n\n\n\n\n---\nNot sure which version is the hardest. Any ideas?' - (published)
en2 English Coder 2015-09-13 21:28:00 486 Tiny change: ' '
en1 English Coder 2015-09-13 21:08:03 1147 Initial revision (saved to drafts)