Hello, can any one please tell how to solve This problem

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

1 | tourist | 3645 |

2 | Radewoosh | 3403 |

3 | Um_nik | 3348 |

4 | LHiC | 3336 |

5 | Benq | 3316 |

6 | V--o_o--V | 3275 |

7 | mnbvmar | 3241 |

8 | yutaka1999 | 3190 |

9 | ainta | 3180 |

10 | Petr | 3106 |

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

1 | Errichto | 191 |

2 | Radewoosh | 177 |

3 | rng_58 | 164 |

4 | PikMike | 162 |

5 | Vovuh | 160 |

6 | majk | 158 |

7 | 300iq | 154 |

8 | antontrygubO_o | 153 |

9 | Um_nik | 151 |

10 | kostka | 149 |

Hello, can any one please tell how to solve This problem

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/19/2019 18:11:48 (e2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Well, if n was

`<= 20`

we could've simply ran through every subset of the the array, but`n <= 40`

. Observe that 40 is still pretty close to 20.So what should we do?

SolutionWe are going to use the Meet in the Middle algorithm (also the name of the task

`:)`

) to solve this problem. That means that we generate all sums for all subsets of the first half and the second half of the array separately. That way time complexity is`O (2^(n/2)*n)`

Now that we have the sums for both halves we want to find in how many ways we can pair two from different halves so that we get x.

We can do that easily by sorting the sum arrays of both halves and use the two pointers method.