# 非标准库的一些好用的东西

Bit Opearation Built-in Functions

int __builtin_ffs (int x)
//Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
int __builtin_clz (unsigned int x)
//Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
int __builtin_ctz (unsigned int x)
//Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
int __builtin_clrsb (int x)
//Returns the number of leading redundant sign bits in x, i.e. the number of bits following the most significant bit that are identical to it. There are no special cases for 0 or other values.
int __builtin_popcount (unsigned int x)
//Returns the number of 1-bits in x.
int __builtin_parity (unsigned int x)
//Returns the parity of x, i.e. the number of 1-bits in x modulo 2.


# Code

int MinimumRepresentation(int *a,int n) {
++a;
int p1=0,p2=1,len=0;
while(p1<n && p2<n && len<n) {
if(a[(p1+len)%n]==a[(p2+len)%n])
len++;
else {
(a[(p1+len)%n]>a[(p2+len)%n]?p1:p2)+=len+1;
if(p1==p2)
p2++;
len=0;
}
}
return std::min(p1,p2)+1;
}