Skip to content

Commit e6e138c

Browse files
committed
[1800] CF1225D 质因子分解+哈希
1 parent 0952cc5 commit e6e138c

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

main/1200-1299/1225D.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1225D(_r io.Reader, out io.Writer) {
11+
in := bufio.NewReader(_r)
12+
const mx int = 1e5
13+
lpf := [mx + 1]int{1: 1}
14+
for i := 2; i <= mx; i++ {
15+
if lpf[i] == 0 {
16+
for j := i; j <= mx; j += i {
17+
if lpf[j] == 0 {
18+
lpf[j] = i
19+
}
20+
}
21+
}
22+
}
23+
24+
var n, k, v int
25+
ans := int64(0)
26+
cnt := map[string]int{} // 或者用 [6]int 来代替 string,下面改成 p<<8|e 和 p<<8|(k-e)
27+
for Fscan(in, &n, &k); n > 0; n-- {
28+
s := []byte{}
29+
t := []byte{}
30+
Fscan(in, &v)
31+
for v > 1 {
32+
p, e := lpf[v], 1
33+
for v /= p; lpf[v] == p; v /= p {
34+
e++
35+
}
36+
if e %= k; e > 0 {
37+
s = append(s, byte(p>>16), byte(p>>8), byte(p), byte(e))
38+
t = append(t, byte(p>>16), byte(p>>8), byte(p), byte(k-e))
39+
}
40+
}
41+
ans += int64(cnt[string(t)])
42+
cnt[string(s)]++
43+
}
44+
Fprint(out, ans)
45+
}
46+
47+
//func main() { CF1225D(os.Stdin, os.Stdout) }

main/1200-1299/1225D_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1225/D
9+
// https://codeforces.com/problemset/status/1225/problem/D
10+
func TestCF1225D(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
6 3
15+
1 3 9 8 24 1
16+
outputCopy
17+
5`
18+
testutil.AssertEqualCase(t, rawText, 1, CF1225D)
19+
}

0 commit comments

Comments
 (0)