Cod sursa(job #2577226)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 8 martie 2020 18:48:30
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.64 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

class OutParser {

	private:

		static const int buffSZ = (1 << 15);
		ofstream File;
		char buff[buffSZ];
		int buffPos;
		vector <int> digits;

		void _advance() {

			if (++buffPos == buffSZ) {

				File.write(buff, buffSZ);
				buffPos = 0;
			}
		}

		void printChar(char no) {

			buff[buffPos] = no;
			_advance();
		}

	public:

		OutParser(const char *FileName) {

			File.open(FileName);
			digits.resize(11);
			buffPos = 0;
		}

		~OutParser() {

			File.write(buff, buffPos);
			buffPos = 0;
		}

		OutParser& operator <<(char ch) {

			printChar(ch);
			return *this;
		}

		OutParser& operator <<(int no) {

			int idx = 0;

			if (no == 0)
				digits[++idx] = 0;

			while (no) {

				digits[++idx] = no % 10;
				no /= 10;
			}

			for (; idx; --idx)
				printChar(digits[idx] + '0');

			return *this;
		}
};

ifstream fin("curcubeu.in");
OutParser fout("curcubeu.out");

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int DMAX = 1e6+10;

int a[DMAX];
int b[DMAX];
int c[DMAX];

int ans[DMAX];
int tata[DMAX];
int lg[DMAX];

bool uz[DMAX];

int n;

int findd(int node);
void unire(int x,int y);

int main(){
    int t,i,j;
    int x,y;

    fin>>n>>a[1]>>b[1]>>c[1];

    lg[1]=1;
    for(i=2;i<n;i++){
        a[i]=(1LL*a[i-1]*i)%n;
        b[i]=(1LL*b[i-1]*i)%n;
        c[i]=(1LL*c[i-1]*i)%n;
        lg[i]=1;
    }
    for(i=n-1;i>=1;i--){
        x=min(a[i],b[i]);
        y=max(a[i],b[i]);
        for(;x<=y;x+=lg[x]){
            if(!uz[x]){
                uz[x]=true;
                ans[x]=c[i];
                if(uz[x-1])
                    unire(x-1,x);
                if(uz[x+1])
                    unire(x,x+1);
            }
            x=findd(x);
        }
    }
    for(i=1;i<n;i++)
        fout<<ans[i]<<'\n';

    return 0;
}
int findd(int node){
    int ans,aux;
    ans=node;
    while(tata[ans])
        ans=tata[ans];

    while(tata[node]){
        aux=node;
        node=tata[node];
        tata[aux]=ans;
    }
    return ans;
}
void unire(int x,int y){
    x=findd(x);
    y=findd(y);
    if(x == y)
        return;
    tata[y]=x;
    lg[x]+=lg[y];
}