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

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

1 | MiFaFaOvO | 3681 |

2 | Um_nik | 3544 |

3 | maroonrk | 3431 |

4 | tourist | 3409 |

5 | apiadu | 3397 |

6 | 300iq | 3317 |

7 | ecnerwala | 3260 |

7 | Benq | 3260 |

9 | LHiC | 3229 |

10 | TLE | 3223 |

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

1 | Errichto | 193 |

2 | antontrygubO_o | 191 |

3 | vovuh | 178 |

4 | pikmike | 177 |

5 | tourist | 166 |

6 | Um_nik | 165 |

7 | McDic | 164 |

8 | ko_osaga | 163 |

9 | Radewoosh | 162 |

10 | 300iq | 156 |

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

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/30/2020 13:02:10 (h1).

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.