๋ฐฑ์ค 2830 ํ์ฑ X3
์ฒซ ์๋) ์๊ฐ ์ด๊ณผ
public class Main {
public static void scorecalculator(int[] people){
int score = 0;
for(int i=0; i<people.length-1; i++){
for(int j=i+1; j<people.length; j++){
score+=people[i]^people[j];
}
}
System.out.println(score);
}
public static void main(String[] args) {
int N;
Scanner sc = new Scanner(System.in);
N= sc.nextInt();
int[] people = new int[N];
for(int i=0; i<N; i++){
people[i]=sc.nextInt();
}
scorecalculator(people);
}
}
์๊ฐ ์ด๊ณผ๊ฐ ๋จ๊ธธ๋ Scanner๋ฅผ ์ฌ์ฉํด์ ๊ทธ๋ฐ๊ฐ๋ณด๋ค ์ถ์ด BufferedReader๋ก ๋ค์ ์์ ํด ๋ณด์๋ค.
๋๋ฒ์งธ ์๋) ์๊ฐ ์ด๊ณผ
public class Main {
public static void scorecalculator(int[] people){
int score = 0;
for(int i=0; i<people.length-1; i++){
for(int j=i+1; j<people.length; j++){
score+=people[i]^people[j];
}
}
System.out.println(score);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] people = new int[N];
for(int i=0; i<N; i++){
people[i]=Integer.parseInt(br.readLine().trim());
}
scorecalculator(people);
}
}
์ฌ์ ํ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๊ณ , ์๊ฐ๋ณต์ก๋๊ฐ O(n^2)์ธ ๊ฒ์ด ๋ฌธ์ ์๋ค.
์ธ๋ฒ์งธ ์๋)
XOR ์ฐ์ฐ์ ๋ ๋นํธ๊ฐ ์๋ก ๋ค๋ฅผ ๋๋ง 1์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค. ๋ฐ๋ผ์ ๊ฐ ๋นํธ ์์น์์ 1๊ณผ 0์ด ์ผ๋ง๋ ๋ง๋๋์ง์ ์ฃผ๋ชฉํด์ผํ๋ค.
๊ฐ ๋นํธ์ 1๊ณผ 0์ ๊ฐ์๋ฅผ ๊ณ์ฐํ๊ณ ํด๋น ๊ฐ์๋ฅผ ๊ณฑํด์ฃผ๋ฉด ํด๋น ๋นํธ ์์น์์ ๊ฐ๋ฅํ ๋ชจ๋ XOR ๊ฒฐ๊ณผ ํฉ์ ๊ตฌํ ์ ์๋ค.
public class Main {
public static void scorecalculator(int[] people){
long score = 0; //int score = 0; => ์ค๋ต!
int n = people.length;
for(int bit = 0; bit<31; bit++) {//๊ฐ ๋นํธ๋ง๋ค
long countOne = 0;
for(int person : people){
if((person & (1<<bit)) != 0){
countOne++; // 1์ธ ๋นํธ ๊ฐ์ ์ธ๊ธฐ
}
}
long countZero = n-countOne;
score += (1L << bit) * countOne * countZero;
}
System.out.println(score);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] people = new int[N];
for(int i=0; i<N; i++){
people[i]=Integer.parseInt(br.readLine().trim());
}
scorecalculator(people);
}
}
ํต์ฌ ๋ก์ง์ ์ฌ์ฉ๋ ๊ธฐ๋ณธ ๊ฐ๋
๋นํธ AND ์ฐ์ฐ์ & : ๋ ์ ์์ ์ด์ง ํํ ๋น๊ต ์, ๊ฐ ๋นํธ ์์น์์ ๋ชจ๋ 1์ธ ๊ฒฝ์ฐ์๋ง ํด๋น ์์น์ ๊ฒฐ๊ณผ๊ฐ 1์ด ๋๋ค.
ex) 1010(2) & 1100(2) = 1000(2)
๋นํธ AND ์ฐ์ฐ์ ํน์ ๋นํธ๋ค์ด 1์ธ์ง ํ์ธํ๊ณ ์ถ์ ์ํฉ์ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ ์ ์๋ค. ํด๋น ๋นํธ ์์น์์ 1์ด ์๋ ๋ง์คํฌ๋ฅผ ๋ง๋ค๊ณ & ์ฐ์ฐ์ ์ํํ๋ฉด ๋๋ค.
ex) ์ ์ 'n'์์ 3๋ฒ์งธ ๋นํธ(2^2์ ์์น)๊ฐ 1์ธ์ง n & (1<<2)์ ์ฐ์ฐ์ ํตํด ํ์ธํ ์ ์๋ค. ๊ฒฐ๊ณผ๊ฐ 1์ด๋ผ๋ฉด ํด๋น ๋นํธ๋ 1์ด ๋๋ค.
(num <<bit) bit๋งํผ num์ ๋ ํํธ ์ฌํํธ ์ฐ์ฐ
1 << 0 = 1
1 << 1 = 10(2)
1 << 2 = 100(2)
...
1 << n = 2^(n-1)
๊ทธ๋ฐ๋ฐ ์ด๋ฒ์๋ ํ๋ ธ๋ค๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์๊ณ , ์์ธ์ int score = 0;์ ์์๋ค. ์ ์ ์ค๋ฒํ๋ก์ฐ ๋ฌธ์ ์๊ณ , int๋ง long์ผ๋ก ๋ฐ๊พธ์ด์ฃผ์๋๋ ํด๊ฒฐ๋์๋ค.
์ ์ ์ค๋ฒํ๋ก์ฐ : 'int' ํ์ ์ 32๋นํธ ์ ์๋ฅผ ์ ์ฅํ๊ณ , ์ต๋๊ฐ์ ์ฝ 2.1์ต(2^31 -1)์ด๋ค. ๊ณ์ฐ ๊ฒฐ๊ณผ๊ฐ 'int'์ ์ต๋๊ฐ์ ์ด๊ณผํ๋ฉด, ์ค๋ฒํ๋ก์ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ ์์์น ๋ชปํ ์์ ๊ฐ์ด ๋์ฌ ์ ์๋ค.
๋ฌธ์ ์ ์ ๋ ฅ ํฌ๊ธฐ์ ๊ฒฐ๊ณผ : ๋ฌธ์ ์์ ๊ฑฐ์ฃผ๋ฏผ์ ์ 'N'์ด ์ต๋ 1,000,000์ด๊ณ , ๊ฐ ์ด๋ฆ๋ ์ต๋ 1,000,000์ด๋ผ๊ณ ํ์๋ค. ๋ชจ๋ ๊ฐ๋ฅํ ์์ ์น๋ฐ๋๋ฅผ ๋ํ๋ค๋ฉด 'int'๋ฒ์๋ฅผ ์ด๊ณผํ ๊ฐ๋ฅ์ฑ์ด ์๋ค. ์๋ฅผ๋ค์ด, ๋ชจ๋ ๊ฑฐ์ฃผ๋ฏผ์ ์ด๋ฆ์ด 1,000,000์ด๊ณ , ๊ฑฐ์ฃผ๋ฏผ์ ์๊ฐ 1,000,000์ด๋ผ๋ฉด, ์ต์ข ํฉ์ ๋งค์ฐ ํฐ ์๊ฐ ๋ ๊ฒ์ด๋ค.
'long'์ ์ฌ์ฉ : 'long'์ 64๋นํธ ์ ์๋ฅผ ์ ์ฅํ๋ฉฐ, ์ต๋๊ฐ์ ์ฝ 9.2๊ฒฝ(2^63-1)์ด๋ค. ๋ฐ๋ผ์ 'long'์ ์ฌ์ฉํ๋ฉด ํจ์ฌ ๋ ํฐ์๋ค๋ ์์ ํ๊ฒ ์ฒ๋ฆฌํ ์ ์์ด ์ค๋ฒํ๋ก์ฐ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.