結果

問題 No.206 数の積集合を求めるクエリ
ユーザー aru aruaru aru
提出日時 2020-09-12 15:36:19
言語 Go
(1.22.1)
結果
AC  
実行時間 332 ms / 7,000 ms
コード長 2,664 bytes
コンパイル時間 15,529 ms
コンパイル使用メモリ 210,348 KB
実行使用メモリ 24,676 KB
最終ジャッジ日時 2023-08-30 13:22:02
合計ジャッジ時間 28,008 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 307 ms
18,320 KB
testcase_01 AC 304 ms
18,316 KB
testcase_02 AC 309 ms
18,316 KB
testcase_03 AC 305 ms
18,320 KB
testcase_04 AC 302 ms
18,320 KB
testcase_05 AC 311 ms
18,316 KB
testcase_06 AC 303 ms
18,320 KB
testcase_07 AC 302 ms
18,320 KB
testcase_08 AC 310 ms
18,316 KB
testcase_09 AC 307 ms
18,316 KB
testcase_10 AC 309 ms
18,320 KB
testcase_11 AC 304 ms
18,316 KB
testcase_12 AC 303 ms
18,320 KB
testcase_13 AC 302 ms
18,316 KB
testcase_14 AC 308 ms
18,316 KB
testcase_15 AC 306 ms
18,316 KB
testcase_16 AC 304 ms
18,316 KB
testcase_17 AC 320 ms
18,432 KB
testcase_18 AC 313 ms
24,676 KB
testcase_19 AC 317 ms
18,428 KB
testcase_20 AC 311 ms
18,324 KB
testcase_21 AC 313 ms
18,320 KB
testcase_22 AC 312 ms
18,444 KB
testcase_23 AC 319 ms
18,432 KB
testcase_24 AC 332 ms
18,428 KB
testcase_25 AC 332 ms
18,428 KB
testcase_26 AC 324 ms
18,432 KB
testcase_27 AC 319 ms
18,432 KB
testcase_28 AC 327 ms
18,432 KB
testcase_29 AC 326 ms
24,676 KB
testcase_30 AC 323 ms
18,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/cmplx"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

const n = 1 << 18

func FFT(x []complex128, n int) []complex128 {
	y := fft(x, n)
	for i := 0; i < n; i++ {
		y[i] = y[i] / complex(float64(n), 0.0)
	}
	return y
}

func IFFT(x []complex128, n int) []complex128 {
	y := make([]complex128, n)
	for i := 0; i < n; i++ {
		y[i] = cmplx.Conj(x[i])
	}
	y = fft(y, n)
	for i := 0; i < n; i++ {
		y[i] = cmplx.Conj(y[i])
	}
	return y
}

func fft(a []complex128, n int) []complex128 {
	x := make([]complex128, n)
	copy(x, a)

	j := 0
	for i := 0; i < n; i++ {
		if i < j {
			x[i], x[j] = x[j], x[i]
		}
		m := n / 2
		for {
			if j < m {
				break
			}
			j = j - m
			m = m / 2
			if m < 2 {
				break
			}
		}
		j = j + m
	}
	kmax := 1
	for {
		if kmax >= n {
			return x
		}
		istep := kmax * 2
		for k := 0; k < kmax; k++ {
			theta := complex(0.0, -1.0*math.Pi*float64(k)/float64(kmax))
			for i := k; i < n; i += istep {
				j := i + kmax
				temp := x[j] * cmplx.Exp(theta)
				x[j] = x[i] - temp
				x[i] = x[i] + temp
			}
		}
		kmax = istep
	}
}

func multPoly(P, Q []complex128) []complex128 {
	P = FFT(P, n)
	Q = FFT(Q, n)
	for i := 0; i < len(P); i++ {
		P[i] = P[i] * Q[i]
	}
	return IFFT(P, n)
}

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

	ac := make([]complex128, n)
	bc := make([]complex128, n)

	L, M, N := getInt(), getInt(), getInt()
	for i := 0; i < L; i++ {
		x := getInt()
		ac[x] += 1
	}
	for i := 0; i < M; i++ {
		x := getInt()
		bc[N-x] += 1
	}
	cc := multPoly(ac, bc)

	for i := 0; i < n; i++ {
		cc[i] /= 1.0 / n
	}

	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	Q := getInt()
	for i := 0; i < Q; i++ {
		fmt.Fprintln(w, (int)(real(cc[i+N])+0.1))
	}
}
0