BOZJ 2045: Crazy steamed buns (and check set)

Source: Internet
Author: User

Main topic: There are N steamed bread in a row, the initial color of 0, for M-time dyeing, I Will (I*P+Q) mod n to (i*q+p) mod n steamed bread all dyed into color I, for the last of all steamed bread color. N<=10^6 m<=10^7

Analysis: NM is very large can not segment tree, we may consider using and check set, we found that each steamed bread may be dyed several times, but only the last dyeing can determine its final color, it is inverted to do, and make each point color only modified once, for the interval [x, y], from the beginning of x will be except the point father all point to So this interval all points to Y, the next modification will skip the interval, because only modify the n points so efficiency is O (n)

Code:

 ProgramASFD;varA:Array[0..1000001] ofLongint; F:Array[0..1000001] ofLongint; N,i,m,p,q,k,x,y,t,j:longint;functionFind (X:longint): Longint;varI,j,k:longint;beginI:=x; j:=x;  whileI<>f[i] Doi:=F[i];  whileI<>j Do   beginK:=F[J]; F[j]:=i; j:=K; End; Exit (i);End;beginreadln (N,M,P,Q);  fori:=1  toN DoF[i]:=i; k:=0;  forI:=mDownto 1  Do   beginx:= (i*pMoDN+Q)MoDn+1; Y:= (i*qMoDN+P)MoDn+1; ifX>y Then beginT:=x; X:=y; y:=t;End; J:=find (x);  whileJ<=y Do      beginA[j]:=i; f[j]:=j+1; Inc (k); ifK=n ThenBreak ; J:=find (j); End; End;  fori:=1  toN DoWriteln (A[i]); READLN;End.
View Code

BOZJ 2045: Crazy steamed buns (and check set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.