-
Notifications
You must be signed in to change notification settings - Fork 27
/
10036.go
54 lines (48 loc) · 895 Bytes
/
10036.go
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
// UVa 10036 - Divisibility
package main
import (
"fmt"
"os"
)
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func divisible(n, k int, num []int) bool {
dp := make([][]bool, n)
for i := range dp {
dp[i] = make([]bool, k)
}
dp[0][num[0]%k] = true
for i := 1; i < n; i++ {
for j := range dp[i-1] {
if dp[i-1][j] {
dp[i][(j+num[i])%k] = true
dp[i][(j-num[i]+k)%k] = true
}
}
}
return dp[n-1][0]
}
func main() {
in, _ := os.Open("10036.in")
defer in.Close()
out, _ := os.Create("10036.out")
defer out.Close()
var kase, n, k, tmp int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%d%d", &n, &k)
num := make([]int, n)
for i := range num {
fmt.Fscanf(in, "%d", &tmp)
num[i] = abs(tmp) % k
}
if divisible(n, k, num) {
fmt.Fprintln(out, "Divisible")
} else {
fmt.Fprintln(out, "Not divisible")
}
}
}