am1x icon

p102178c

am1x | PRO | 09/11/21 04:18:54 PM UTC (Edited) | 0 ⭐ | 669 👁️ | Never ⏰ | []
C++ |

1.43 KB

|

None

|

0 👍

/

0 👎

#define __STDC_FORMAT_MACROS
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
#include <vector>
#include <algorithm>
 
 
typedef unsigned int uint_t;
const uint_t N = 50;
 
static struct Solver {
    uint_t best;
    uint64_t a;
    std::vector<uint_t> ts;
    std::vector<uint64_t> ms;
 
    void init(uint_t mx, uint64_t _a) {
        best = 6;
        a = _a;
        ts.clear();
        ts.push_back(mx);
        ms.clear();
        ms.push_back(1);
    }
    
    void solve()
    {
        uint_t n = ts.size() - 1;
        uint64_t mn = ms[n];
        if ((a & ~mn) == 0) {
            if (n < best)
                best = n;
            return;
        }
        
        if (n + 1 >= best)
            return;
        
        for (uint_t x = ts[n]; x; x--) {
            ts.push_back(x);
            ms.push_back(mn | (mn << x));
            solve();
            ts.pop_back();
            ms.pop_back();
        }
    }
} s;
 
 
 
int main()
{
    uint64_t a = 0;
    uint_t n = 0, mx = 0;
    int st = scanf("%u", &n);
    assert (st == 1);
    assert (0 < n && n <= N);
    
    for (uint_t i = 0; i < n; i++) {
        uint_t x = 0;
        st = scanf("%u", &x); 
        assert (st == 1);
        assert (0 < x && x <= N);
        a |= 1ULL << x;
        if (mx < x)
            mx = x;
    }
 
    s.init(mx, a);
    s.solve();
    printf("%u", s.best);
 
    return 0;
}
 

Comments