結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー ccppjsrbccppjsrb
提出日時 2020-10-01 11:53:46
言語 Go
(1.21.3)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 11,335 ms
コンパイル使用メモリ 206,760 KB
実行使用メモリ 15,096 KB
最終ジャッジ日時 2023-09-21 06:49:02
合計ジャッジ時間 13,208 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 22 ms
8,048 KB
testcase_11 AC 16 ms
5,492 KB
testcase_12 AC 242 ms
5,560 KB
testcase_13 AC 12 ms
5,476 KB
testcase_14 AC 22 ms
5,572 KB
testcase_15 AC 15 ms
5,480 KB
testcase_16 AC 28 ms
9,004 KB
testcase_17 AC 11 ms
5,484 KB
testcase_18 AC 244 ms
5,496 KB
testcase_19 AC 65 ms
5,488 KB
testcase_20 AC 60 ms
15,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	k := getNextInt(scanner)
	op := getNextString(scanner)
	bb := make([]int, m)
	for i := 0; i < m; i++ {
		bb[i] = getNextInt(scanner)
	}
	aa := make([]int, n)
	for i := 0; i < n; i++ {
		aa[i] = getNextInt(scanner)
	}
	var ans int64
	if op == "*" {
		ma := map[int]int{}
		for i := 0; i < n; i++ {
			g := gcd(aa[i], k)
			ma[g]++
		}
		mb := map[int]int{}
		for i := 0; i < m; i++ {
			g := gcd(bb[i], k)
			mb[g]++
		}
		for a, ac := range ma {
			for b, bc := range mb {
				if gcd(a*b, k) == k {
					ans += int64(ac * bc)
				}
			}
		}
		fmt.Fprintln(writer, ans)
		return
	}
	ma := map[int]int{}
	mb := map[int]int{}
	for i := 0; i < n; i++ {
		ma[aa[i]%k]++
	}
	for i := 0; i < m; i++ {
		mb[bb[i]%k]++
	}
	for a, c := range ma {
		if a == 0 {
			ans += int64(c) * int64(mb[0])
			continue
		}
		ans += int64(c) * int64(mb[k-a])
	}
	fmt.Fprintln(writer, ans)
}
func gcd(m, n int) int {
	if n == 0 {
		return m
	}
	return gcd(n, m%n)
}
0