結果

問題 No.187 中華風 (Hard)
ユーザー aru aruaru aru
提出日時 2020-09-06 17:17:48
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 3,065 bytes
コンパイル時間 11,483 ms
コンパイル使用メモリ 209,280 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-19 14:07:40
合計ジャッジ時間 16,407 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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 invmod(a, m int) int {
	s := a % m
	t := m
	sx, sy, tx, ty := 1, 0, 0, 1
	for s%t != 0 {
		f := s / t
		u := s - t*f
		ux := sx - tx*f
		uy := sy - ty*f
		s = t
		sx = tx
		sy = ty
		t = u
		tx = ux
		ty = uy
	}
	if tx < 0 {
		tx += m
	}
	return tx
}

func garner(x []int, m []int, mod int) int {
	if len(x) == 0 {
		return 0
	}
	v := make([]int, len(x))
	v[0] = x[0]
	for i := 1; i < len(x); i++ {
		X := x[i]
		M := 1
		for j := 0; j < i; j++ {
			X -= v[j] * M
			X %= m[i]
			M *= m[j]
			M %= m[i]
		}
		if X < 0 {
			X += m[i]
		}
		v[i] = X * invmod(M, m[i]) % m[i]
	}
	ret := v[0]
	p := 1
	if mod == 0 {
		for i := 1; i < len(x); i++ {
			p *= m[i-1]
			ret += p * v[i]
		}
	} else {
		ret %= mod
		for i := 1; i < len(x); i++ {
			p *= m[i-1]
			p %= mod
			ret += p * v[i]
			ret %= mod
		}
	}
	return ret
}

type pair struct {
	first, second int
}

func garner_helper(A, B []int, mod int, zero bool) int {
	m := make(map[int]int)
	for i := 0; i < len(A); i++ {
		b := B[i]
		for j := 2; j*j <= b; j++ {
			if b%j == 0 {
				m[j]++
				for b%j == 0 {
					b /= j
				}
			}
		}
		if b > 1 {
			m[b]++
		}
	}
	primes := make([]int, 0)
	for i := range m {
		primes = append(primes, i)
	}
	sort.Ints(primes)
	a := make([]int, 0)
	b := make([]int, 0)
	flag := !zero
	ret := 1
	for _, p := range primes {
		ma := 1
		res := 0
		x := make([]pair, 0)
		for i := 0; i < len(A); i++ {
			if B[i]%p == 0 {
				t := 1
				for B[i]%p == 0 {
					B[i] /= p
					t *= p
				}
				x := append(x, pair{t, A[i] % t})
				if ma < t {
					ma = t
					res = x[len(x)-1].second
				}
				A[i] %= B[i]
			}
		}
		for _, q := range x {
			if res%q.first != q.second {
				return -1
			}
		}
		a = append(a, res)
		b = append(b, ma)
		flag = flag && (res == 0)
		if flag {
			ret *= ma
			if mod != 0 {
				ret %= mod
			}
		}
	}
	if !flag {
		ret = garner(a, b, mod)
	}
	return ret
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	x := make([]int, N)
	y := make([]int, N)
	for i := 0; i < N; i++ {
		x[i], y[i] = getInt(), getInt()
	}
	out(garner_helper(x, y, 1e9+7, false))
}
0