結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー 👑 naipianaipia
提出日時 2020-02-14 23:33:21
言語 Go
(1.21.3)
結果
AC  
実行時間 1,981 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 11,453 ms
コンパイル使用メモリ 207,716 KB
実行使用メモリ 9,288 KB
最終ジャッジ日時 2023-08-10 02:44:48
合計ジャッジ時間 17,245 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 30 ms
5,548 KB
testcase_11 AC 264 ms
4,380 KB
testcase_12 AC 1,981 ms
5,484 KB
testcase_13 AC 94 ms
4,376 KB
testcase_14 AC 30 ms
4,380 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 AC 32 ms
6,052 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 1,980 ms
5,544 KB
testcase_19 AC 699 ms
4,376 KB
testcase_20 AC 60 ms
9,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"sort"
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string { sc.Scan(); return sc.Text() }
func scanInt() int { a,_ := strconv.Atoi(scanString()); return a }
func scanInt64() int64 { a,_ := strconv.ParseInt(scanString(),10,64); return a }
func scanFloat64() float64 { a,_ := strconv.ParseFloat(scanString(),64); return a }

func scanInts(n int) []int {
	res := make([]int, n); for i := 0; i < n; i++ { res[i] = scanInt() }; return res
}

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪( -ω-)ノ ( ・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1000000)

	n := scanInt()
	m := scanInt()
	k := scanInt()
	op := scanString()

	ans := 0

	b := scanInts(m)
	a := scanInts(n)

	if op == "+" {
		cnt := make(map[int]int, 0)
		for i := 0; i < m; i++ {
			b[i] %= k
			cnt[b[i]]++
		}

		sort.Ints(b)
		// fmt.Println(b)
	
		for i := 0; i < n; i++ {
			val := (-a[i]%k + k)%k
			// l := sort.Search(len(b), func(j int) bool { return val <= b[j] })
			// r := sort.Search(len(b), func(j int) bool { return val < b[j] })
			//ans += r-l
			ans += cnt[val]
			// fmt.Println(val)
		}
	} else {
		kf := factorize(int64(k))
		cnt := make(map[int]int, 0)
		for i := 0; i < n; i++ {
			af := factorize(int64(a[i]))
			v := 1
			for val, c := range kf {
				v *= pow(int(val),max(0,c-af[val]))
			}
			cnt[v]++
		}
		// fmt.Println(cnt)
		for val, c := range cnt {
			for i := 0; i < m; i++ {
				if b[i]%int(val) == 0 {
					ans += c
				}
			}
		}
		// fmt.Println(cnt)
	}
	

	fmt.Println(ans)
}

func factorize(n int64) map[int64]int {
	res := make(map[int64]int, 0)
	for i := int64(2); i*i <= n; i++ {
		for n%i == 0 {
			res[i]++
			n /= i
		}
	}
	if n!=1 {
		res[n] = 1
	}
	return res
}

func pow(a,n int) int {
	res := 1
	for n>0 {
		if n&1 == 1 { res *= a }
		a *= a; n >>= 1
	}
	return res
}
0