結果

問題 No.886 Direct
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-24 18:50:14
言語 Go
(1.22.1)
結果
AC  
実行時間 626 ms / 4,000 ms
コード長 1,331 bytes
コンパイル時間 13,726 ms
コンパイル使用メモリ 211,008 KB
実行使用メモリ 145,540 KB
最終ジャッジ日時 2023-10-11 03:48:58
合計ジャッジ時間 20,265 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 1 ms
4,372 KB
testcase_16 AC 1 ms
4,372 KB
testcase_17 AC 2 ms
4,368 KB
testcase_18 AC 3 ms
4,368 KB
testcase_19 AC 3 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 3 ms
4,372 KB
testcase_23 AC 260 ms
64,284 KB
testcase_24 AC 295 ms
74,752 KB
testcase_25 AC 150 ms
47,652 KB
testcase_26 AC 222 ms
56,400 KB
testcase_27 AC 569 ms
128,972 KB
testcase_28 AC 577 ms
125,156 KB
testcase_29 AC 613 ms
122,688 KB
testcase_30 AC 618 ms
125,056 KB
testcase_31 AC 626 ms
145,540 KB
testcase_32 AC 623 ms
145,540 KB
testcase_33 AC 623 ms
145,540 KB
testcase_34 AC 622 ms
145,540 KB
testcase_35 AC 623 ms
145,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var ROW, COL int
	fmt.Fscan(in, &ROW, &COL)

	res := ROW*(COL-1) + COL*(ROW-1) // 原来相邻的线段数
	n := max(ROW, COL)
	A, B := make([]int, n+2), make([]int, n+2)
	for i := 0; i < ROW; i++ {
		A[i] = ROW - i - 1
	}
	for i := 0; i < COL; i++ {
		B[i] = COL - i - 1
	}

	C := GcdConvolution(A, B)
	fmt.Fprintln(out, (res+C[0]*2)%MOD) // pair of gcd(row,col)=1

}

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

const MOD int = 1e9 + 7

// c[k] = ∑a[i]*b[j] mod MOD, gcd(i,j)=k
func GcdConvolution(nums1, nums2 []int) []int {
	n := len(nums1)
	pf := make([]int, n+1)
	copy1, copy2 := make([]int, n+1), make([]int, n+1)
	for i := 0; i < n; i++ {
		copy1[i+1] = nums1[i]
		copy2[i+1] = nums2[i]
	}

	for i := 2; i < n+1; i++ {
		if pf[i] == 0 {
			for j := n / i; j > 0; j-- {
				pf[j*i] = 1
				copy1[j] = (copy1[j] + copy1[j*i]) % MOD
				copy2[j] = (copy2[j] + copy2[j*i]) % MOD
			}
			pf[i] = 0
		}
	}

	res := make([]int, n+1)
	for i := 0; i < n+1; i++ {
		res[i] = copy1[i] * copy2[i] % MOD
	}

	for i := 2; i < n+1; i++ {
		if pf[i] == 0 {
			for j := 1; j < n/i+1; j++ {
				res[j] = (res[j] - res[j*i] + MOD) % MOD
			}
		}
	}

	return res[1:]
}
0