結果

問題 No.187 中華風 (Hard)
ユーザー aru aruaru aru
提出日時 2020-09-06 16:45:50
言語 Go
(1.21.3)
結果
WA  
実行時間 -
コード長 2,310 bytes
コンパイル時間 12,703 ms
コンパイル使用メモリ 212,040 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 14:05:06
合計ジャッジ時間 13,952 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 WA -
testcase_19 AC 1 ms
4,380 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"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
}

func extGCD(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, y, x := extGCD(b, a%b)
	y -= a / b * x
	return d, x, y
}

func modInv(a, m int) int {
	d, x, _ := extGCD(a, m)
	if d != 1 {
		return -1
	}
	x %= m
	if x < 0 {
		x += m
	}
	return x
}

func garner(r, m []int) int {
	n := len(r)
	mProd := 1
	x := r[0] % m[0]
	for i := 1; i < n; i++ {
		mProd *= m[i-1]
		t := ((r[i] - x) * modInv(mProd, m[i])) % m[i]
		if t < 0 {
			t += m[i]
		}
		x += t * mProd
	}
	return x
}

// PfsMap : 素因数分解し、マップを作成
func PfsMap(n int) map[int]int {
	pfs := make(map[int]int)
	for n%2 == 0 {
		pfs[2] = pfs[2] + 1
		n = n / 2
	}

	for i := 3; i*i <= n; i = i + 2 {
		for n%i == 0 {
			pfs[i] = pfs[i] + 1
			n = n / i
		}
	}

	if n > 2 {
		pfs[n] = pfs[n] + 1
	}

	return pfs
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	x := make([]int, N)
	y := make([]int, N)
	m := make(map[int]int)
	pos := make(map[int]int)
	for i := 0; i < N; i++ {
		x[i], y[i] = getInt(), getInt()
		p := PfsMap(y[i])
		// out(p)
		for j, n := range p {
			t := m[j]
			if t < n {
				m[j] = n
				pos[j] = i
			}
		}
	}
	// out(y)
	for p := range m {
		for i := 0; i < N; i++ {
			if pos[p] == i {
				continue
			}
			for y[i]%p == 0 {
				y[i] /= p
			}
		}
	}
	// out(m, pos)
	// out(y)
	out(garner(x, y))
}
0