結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー neko_the_shadowneko_the_shadow
提出日時 2020-02-19 17:14:25
言語 Go
(1.21.3)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 2,411 bytes
コンパイル時間 11,671 ms
コンパイル使用メモリ 213,140 KB
実行使用メモリ 13,036 KB
最終ジャッジ日時 2023-08-10 03:50:04
合計ジャッジ時間 13,039 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 20 ms
7,796 KB
testcase_11 AC 25 ms
7,804 KB
testcase_12 AC 219 ms
8,316 KB
testcase_13 AC 18 ms
7,564 KB
testcase_14 AC 32 ms
7,784 KB
testcase_15 AC 19 ms
7,768 KB
testcase_16 AC 33 ms
8,840 KB
testcase_17 AC 15 ms
5,524 KB
testcase_18 AC 217 ms
8,324 KB
testcase_19 AC 58 ms
8,200 KB
testcase_20 AC 63 ms
13,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func exec(stdin *Stdin, stdout *Stdout) {
	n := stdin.ReadInt()
	m := stdin.ReadInt()
	k := stdin.ReadInt()
	op := stdin.Read()
	a := []int{}
	b := []int{}
	for i := 0; i < m; i++ {
		b = append(b, stdin.ReadInt())
	}
	for i := 0; i < n; i++ {
		a = append(a, stdin.ReadInt())
	}

	ans := 0
	if op == "+" {
		c := NewCounter()
		for i := 0; i < n; i++ {
			c.Increment(a[i] % k)
		}

		for i := 0; i < m; i++ {
			mod := b[i] % k
			if mod == 0 {
				ans += c.Get(0)
			} else {
				ans += c.Get(k - mod)
			}
		}
	} else {
		c := NewCounter()
		d := NewCounter()
		for i := 0; i < n; i++ {
			c.Increment(Gcd(k, a[i]))
		}
		for i := 0; i < m; i++ {
			d.Increment(Gcd(k, b[i]))
		}

		for _, key1 := range c.Keys() {
			for _, key2 := range d.Keys() {
				gcd1, _ := key1.(int)
				gcd2, _ := key2.(int)
				if (gcd1*gcd2)%k == 0 {
					ans += c.Get(key1) * d.Get(key2)
				}
			}
		}
	}

	stdout.Println(ans)
}

func Gcd(x, y int) int {
	if x < y {
		x, y = y, x
	}

	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func main() {
	stdout := NewStdout()
	defer stdout.Flush()
	exec(NewStdin(bufio.ScanWords), stdout)
}

type Stdin struct {
	stdin *bufio.Scanner
}

func NewStdin(split bufio.SplitFunc) *Stdin {
	s := Stdin{bufio.NewScanner(os.Stdin)}
	s.stdin.Split(split)
	s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32))
	return &s
}

func (s *Stdin) Read() string {
	s.stdin.Scan()
	return s.stdin.Text()
}

func (s *Stdin) ReadInt() int {
	n, _ := strconv.Atoi(s.Read())
	return n
}

func (s *Stdin) ReadFloat64() float64 {
	n, _ := strconv.ParseFloat(s.Read(), 64)
	return n
}

type Stdout struct {
	stdout *bufio.Writer
}

func NewStdout() *Stdout {
	return &Stdout{bufio.NewWriter(os.Stdout)}
}

func (s *Stdout) Flush() {
	s.stdout.Flush()
}

func (s *Stdout) Println(a ...interface{}) {
	fmt.Fprintln(s.stdout, a...)
}

type Counter map[interface{}]int

func NewCounter() *Counter {
	return &Counter{}
}

func (c *Counter) Increment(key interface{}) {
	d := map[interface{}]int(*c)
	d[key] = c.Get(key) + 1
}

func (c *Counter) Get(key interface{}) int {
	d := map[interface{}]int(*c)
	if v, ok := d[key]; ok {
		return v
	} else {
		return 0
	}
}

func (c *Counter) Keys() []interface{} {
	d := map[interface{}]int(*c)
	keys := []interface{}{}
	for key := range d {
		keys = append(keys, key)
	}
	return keys
}
0