public int calculate(int state) {
        if (_states[state] != -1)
            return _states[state];
        int previous = 0;
        int i;
        for (i = 0; i < _n; i++) {
            if ((state & (1 << i)) != 0) {
                if (_num[i] > previous)
                    previous = _num[i];
                else
                    break;
            }
        }
        if (i == _n) {
            _states[state] = 0;
            return 0;
        }
        NavigableSet<Integer> set = new TreeSet<Integer>();
        for (i = 0; i < _n; i++) {
            if ((state & (1 << i)) != 0) {
                set.add(calculate(state & (~(1 << i))));
            }
        }
        _states[state] = 1 - set.first();
        return _states[state];
    }