B. Composite Coloring

_Yxc發表於2024-03-14

https://codeforces.com/problemset/problem/1332/B

void solve(){
    int n;
    cin >> n;

    vector<vector<int>> ans(1234);
    for (int i = 0; i < n; ++i){
        int t;
        cin >> t;
        for (int j = 2; j <= t; ++j){
            if (t % j == 0){
                ans[j].emplace_back(i);
                break;
            }
        }
    }

    vector<int> res(n);
    int cnt = 0;
    for (int i = 1; i <= 1233; ++i){
        if (!ans[i].empty()){
            cnt ++;
            for (const auto& index : ans[i]){
                res[index] = cnt;
            }
        }
    }

    cout << cnt << '\n';
    for (int i = 0; i < n; ++i){
        cout << res[i] << " \n"[i == n - 1];
    }

}