Cod sursa(job #3380424)

Utilizator TLuca2021Teodorescu Luca Nicolae TLuca2021 Data 2 decembrie 2026 00:25:51
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("curcubeu.in");
ofstream fout("curcubeu.out");
vector<int> next_uncolored;
int find_next_uncolored(int position) {
    if (next_uncolored[position] == position) {
        return position;
    }
    return next_uncolored[position] = find_next_uncolored(next_uncolored[position]);
}
void solve() {
    int n, first_left, first_right, first_color;
    fin >> n >> first_left >> first_right >> first_color;
    vector<int> left_bound(n);
    vector<int> right_bound(n);
    vector<int> operation_color(n);
    left_bound[1] = first_left;
    right_bound[1] = first_right;
    operation_color[1] = first_color;
    for (int step = 2; step <= n - 1; step++) {
        left_bound[step] = (1LL * left_bound[step - 1] * step) % n;
        right_bound[step] = (1LL * right_bound[step - 1] * step) % n;
        operation_color[step] = (1LL * operation_color[step - 1] * step) % n;
    }
    next_uncolored.resize(n + 1);
    for (int position = 1; position <= n; position++) {
        next_uncolored[position] = position;
    }
    vector<int> final_color(n, 0);
    for (int step = n - 1; step >= 1; step--) {
        int current_left = left_bound[step];
        int current_right = right_bound[step];
        if (current_left > current_right) {
            swap(current_left, current_right);
        }
        int current_position = find_next_uncolored(current_left);
        while (current_position <= current_right) {
            final_color[current_position] = operation_color[step];
            next_uncolored[current_position] = find_next_uncolored(current_position + 1);
            current_position = find_next_uncolored(current_position);
        }
    }
    for (int position = 1; position <= n - 1; position++) {
        fout << final_color[position] << "\n";
    }
}
int main() {
    solve();
    return 0;
}