結果

問題 No.941 商とあまり
ユーザー りあんりあん
提出日時 2019-12-04 07:38:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,112 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 204,184 KB
最終ジャッジ日時 2025-01-08 07:44:14
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 104
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;



int x;
vector<bool> ans;

void output() {
    for (bool i : ans) {
        cout << i;
    }
    cout << '\n';
}

int gcd(int a, int b) {
    while (b > 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}


void dfs(const vector<int>& v, int j, int s) {
    ans[s] = true;
    if (j == v.size()) return;
    for (int i = 0; s + i < x; i += v[j]) {
        if (i > 0 && j + 1 < v.size() && i % v[j + 1] == 0) break;
        dfs(v, j + 1, s + i);
    }
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n >> x;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    long long sum = 1;
    for (int i = 0; i < n; ++i) {
        sum *= a[i] + 1;
        if (sum > M) break;
    }
    ans = vector<bool>(x);
    if (sum - 1 > x) {
        output();
        return 0;
    }
    int bs = sum - 1;
    if (n == 1) {
        ans[a[0] - 1] = true;
        output();
        return 0;
    }
    vector<int> b;
    for (int i = 0; i < n; ++i) {
        if (i == 0 || a[i - 1] < a[i]) {
            b.push_back(a[i]);
        }
    }
    int sz = b.size() < a.size() ? b.size() : b.size() - 1;
    vector<int> g(1 << b.size());
    for (int i = 1; i < (1 << b.size()); ++i) {
        vector<int> c;
        bool f = true;
        for (int j = 0; j < b.size(); ++j) {
            if (i >> j & 1) {
                if (g[i] == 0) g[i] = gcd(g[i ^ (1 << j)], b[j]);
                if (g[i] == g[i ^ 1 << j]) f = false;
                c.push_back(b[j]);
            }
        }
        if (f && c.size() <= sz) {
            do
            {
                vector<int> v(c.size());
                int ss = 1;
                for (int i = 0; i < c.size(); ++i) {
                    v[i] = ss * c[i];
                    ss *= c[i] + 1;
                }
                dfs(v, 0, bs - 1);

            } while (next_permutation(c.begin(), c.end()));

        }
    }
    output();

    return 0;
}
0