結果

問題 No.1966 Median of Divisors
ユーザー HimaHima
提出日時 2022-06-26 15:44:42
言語 Go
(1.22.1)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 13,130 ms
コンパイル使用メモリ 238,448 KB
実行使用メモリ 9,884 KB
最終ジャッジ日時 2024-04-28 08:24:52
合計ジャッジ時間 15,430 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 65 ms
9,876 KB
testcase_05 AC 92 ms
9,868 KB
testcase_06 AC 73 ms
9,876 KB
testcase_07 AC 9 ms
6,940 KB
testcase_08 AC 47 ms
7,768 KB
testcase_09 AC 63 ms
9,884 KB
testcase_10 AC 62 ms
9,876 KB
testcase_11 AC 35 ms
7,636 KB
testcase_12 AC 45 ms
7,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

func divide(x int) []int {
	m := make(map[int]struct{})
	for i := 1; i*i <= x; i++ {
		if x%i == 0 {
			m[i] = struct{}{}
			m[x/i] = struct{}{}
		}
	}
	var res []int
	for k := range m {
		res = append(res, k)
	}
	sort.Ints(res)
	return res
}

func solveHonestly(t int, n, m []int) (ans []int) {
	const p = int(1e9) + 7

	nm := Pow(n[0], m[0], p)
	var s int
	for i := 1; i <= nm; i++ {
		d := divide(i)
		if len(d)%2 == 0 {
			idx := len(d)/2 - 1
			mid := d[idx] + d[idx+1]
			if mid*mid > i*4 {
				s += i
			} else {
				fmt.Println(len(d), i)
			}
		} else {
			mid := d[(len(d)+1)/2-1]
			if mid*mid > i {
				s += i
			} else {
				fmt.Println(len(d), i)
			}
		}
	}
	ans = append(ans, s)
	return ans
}

func solve(t int, n, m []int) (ans []int) {
	const p = int(1e9) + 7

	inv2 := Inv(2, p)
	inv6 := Inv(6, p)
	for i := 0; i < t; i++ {

		nm := Pow(n[i], m[i], p)
		v := nm * (nm + 1) % p
		v = v * inv2 % p

		nm2 := Pow(n[i], m[i]/2, p)
		mi := nm2 * (nm2 + 1) % p
		mi = mi * (2*nm2 + 1) % p
		mi = mi * inv6 % p
		v = (v - mi + p) % p
		ans = append(ans, v)
	}
	return ans
}

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	t := nextInt()
	var n, m []int
	for i := 0; i < t; i++ {
		n = append(n, nextInt())
		m = append(m, nextInt())
	}
	ans := solve(t, n, m)
	//ans := solveHonestly(t, n, m)
	PrintVertically(ans)
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func PrintInt(x int) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func PrintVertically(x []int) {
	defer out.Flush()
	for _, v := range x {
		fmt.Fprintln(out, v)
	}
}

func Pow(x, y, p int) int {
	ret := 1
	for y > 0 {
		if y%2 == 1 {
			ret = ret * x % p
		}
		y >>= 1
		x = x * x % p
	}
	return ret
}

func Inv(x, p int) int {
	return Pow(x, p-2, p)
}
0