結果

問題 No.187 中華風 (Hard)
ユーザー 👑 naipianaipia
提出日時 2020-09-26 00:04:27
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,748 bytes
コンパイル時間 14,659 ms
コンパイル使用メモリ 202,940 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 17:16:32
合計ジャッジ時間 16,131 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
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 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,380 KB
testcase_22 WA -
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func debug(a ...interface{}) {
	if os.Getenv("ONLINE_JUDGE") == "false" {
		fmt.Fprintln(os.Stderr, a...)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪( -ω-)ノ ( ・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1001), 1001001)

	n := scanInt()
	x, y := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		x[i], y[i] = scanInt(), scanInt()
	}
	r, m := crt(x, y)
	if m == 0 {
		fmt.Fprintln(wr, -1)
	} else {
		fmt.Fprintln(wr, r%mod)
	}

}

const mod = 1000000007

func crt(r []int, m []int) (int, int) {
	r0, m0 := 0, 1

	invGcd := func(a, p int) (int, int) {
		x, u := 1, 0
		for p != 0 {
			t := a / p
			a, p = p, a-t*p
			x, u = u, x-t*u
		}
		return a, x
	}

	for i := 0; i < len(r); i++ {
		g, im := invGcd(m0, m[i])
		if (r[i]-r0)%g != 0 {
			return 0, 0
		}
		x := (r[i] - r0) / g * im % (m[i] / g)
		r0 += m0 * x
		m0 *= m[i] / g
		if r0 < 0 {
			r0 += m0
		}
	}

	return r0, m0
}
0