-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path080.swift
More file actions
83 lines (69 loc) · 1.33 KB
/
080.swift
File metadata and controls
83 lines (69 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
答 = すべて - (Aのどれかと論理積が0)
*/
/*
やりかた
「A1と論理積が0」を足す
「A2と論理積が0」を足す
「A3と論理積が0」を足す
:
:
:
足しすぎたのを引く
「A1とA2の両方と論理積が0」を引く
「A2とA3の両方と論理積が0」を引く
:
:
引きすぎたのを足す
:
足しすぎたのを引く
:
:
:
*/
import Foundation
func readIntArray() -> [Int] {
readLine()!.split(separator: " ").map { Int(String($0))! }
}
let nd = readIntArray()
let n = nd[0]
let d = nd[1]
let a = readIntArray()
let aCombination = Int(pow(2.0, Double(n)))
var counter = [Int](repeating: 0, count: n + 1)
//counter[i] : aの組み合わせの要素数がi個のデータ
func check(combi: Int) -> (aCount: Int, num: Int) {
var combi = combi
var aCount = 0
var union = 0 //和
var i = 0
while combi != 0 {
if combi & 1 == 1 {
aCount += 1
union |= a[i]
}
combi = combi>>1
i += 1
}
var on = 0
while union != 0 {
if union & 1 == 1 {
on += 1
}
union = union>>1
}
let off = d - on
let num = Int(pow(2.0, Double(off)))
return (aCount, num)
}
for i in 1..<aCombination {
let (aCount, num) = check(combi: i)
counter[aCount] += num
}
var dir = 1
var sum = 0
for i in 1...n {
sum += dir * counter[i]
dir *= -1
}
print(Int(pow(2.0, Double(d))) - sum)