Блог пользователя Satoru

Автор Satoru, 5 лет назад, По-английски

Hello codeforces

Can you help me solve this problem:

Given an array of size $$$k$$$, the sum of it's elements is equal to $$$n$$$, print a binary string of size $$$n$$$ ('$$$1$$$' if you can create $$$i$$$ by taking a subset from the array, '$$$0$$$' otherwise)

Example

UPD: Constraints: $$$1 \lt = n, k \lt = 10^5$$$

UPD2: thanks purplesyringa for the solution

  • Проголосовать: нравится
  • +42
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Pretty Standard DP Problem I think Surf SUBSET SUM Problem, N*K dp table can do the trick for you

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +26 Проголосовать: не нравится

This is very similar to the knapsack problem so I'm afraid there is no polynomial solution. I think you should try non-asymptotic optimizations. Instead of using arrays, use bitset, this should speed up the code by a factor of 64.

(unchecked) implementation:

bitset<100001> result;

int main() {
    int n, sum;
    cin >> n >> sum;

    result[0] = 1;
    for(int i = 0; i < n; i++) {
        int a;
        cin >> a;
        result |= result << a;
    }
    for(int i = 1; i <= sum; i++) {
        cout << result[i];
    }
    cout << endl;
    return 0;
}
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is there an online judge where we can try this?