- μ’ λ§λΆ - λΉνΈλ§μ€ν¬
μ μ : μ μμ μ΄μ§μ ννμ μλ£κ΅¬μ‘°λ‘ μ°λ κΈ°λ²
int emptySet = (1<<20);
int fullSet = (1<<20)-1;
elements |= (1<<n);
if(elements & (1<<n))
elements -= (1<<n);
elements &= ~(1<<n);
elements ^= (1<<n);
int bitCount(int x) {
if(x==0) return 0;
return x%2 + bitCount(x/2);
}
μ§ν©μμ κ°μ₯ μμ μμ μ°ΎκΈ° (μ΄λ μλμ§) -> μΌμ Έ μλ μ΅νμ λ²νΈλ₯Ό λ°ννλ©΄ λ¨
int minElement = (elements & -elements);
elements &= (elements-1);
*2μ κ±°λμ κ³±μ μΌμ§ λΉνΈκ° 1κ° λ°μ μλ€. μ¦, μ΅νμ λΉνΈ μμ μ μ 체 κ°μ΄ 0μ΄ λλμ§ νμΈνλ©΄ λλ€.
for(int subset = set; subset; subset = ((subset-1) & set))
subset-1
: μ΅νμ λΉνΈλ λκ³ , κ·Έ μλ λΉνΈλ λͺ¨λ μΌμ§ μνλ‘ λ§λ λ€.
inline bool isPrime(int k) {
return sieve[k>>3] & (1<<(k&7));
}
inline void setComposite(int k) {
sieve[k>>3] &= ~(1<<(k&7));
}
64bit = 4bit * 16
νΌμ¦μ μνλ₯Ό 64λΉνΈλ‘ ννν μ μλ€.
typedef unsigned long long uint64;
/*
* @param mask [number storing the state of the puzzle]
* @param index [position to be changed the value]
*/
int get(uint64 mask, int index) {
return (mask >> (index <<2)) & 15;
}
/*
* @param mask [number storing the state of the puzzle]
* @param index [position to be changed the value]
* @param value [new value of the index]
*/
uint64 set(uint64 mask, int index, uint64 value) {
return mask & ~(15LL << (index<<2)) | (value << (index<<2));
}
κ°μ΄ λλ©΄ νλ°νλ λ¬Όμ§λ€μ΄ μμ‘΄νμ§ μλ μ§ν©μ ꡬνλ λ¬Έμ
/*
* set : λ¬Όμ§λ€μ μ§ν©
* i : λ¬Όμ§μ μΈλ±μ€ κ°
* explodes[i] : iμ κ°μ΄ λλ©΄ νλ°νλ λ¬Όμ§λ€μ μ§ν©
*/
(set & (1<<i)) && (set & explodes[i])