結果

問題 No.129 お年玉(2)
ユーザー yukirinyukirin
提出日時 2016-02-26 17:35:00
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,391 bytes
コンパイル時間 13,378 ms
コンパイル使用メモリ 217,992 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 17:33:42
合計ジャッジ時間 11,446 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,384 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func main() {
	sc.Split(bufio.ScanWords)
	n, m := nextInt(), nextInt()
	n -= n % 1000
	mod := (n/1000 - n/(1000*m)*m)
	fmt.Println(ncr(uint64(m), uint64(mod), 1000000000))
}

func ncr(n, r, mod uint64) uint64 {
	if n < 0 || r < 0 || r > n {
		return 0
	}

	if r > n/2 {
		r = n - r
	}

	a := make([]uint64, n)
	for i := range a {
		a[i] = n - uint64(i)
	}

	ps := eratosthenes(int(r))
	for _, p := range ps {
		for q := uint64(p); q <= r; q *= uint64(p) {
			m := n % q
			for i, j := m, uint64(0); j < r/q; i, j = i+q, j+1 {
				a[i] /= uint64(p)
			}
		}
	}

	mul := uint64(1)
	for i := uint64(0); i < r; i++ {
		mul = mul * a[i] % mod
	}
	return mul
}
func nextLine() string {
	sc.Scan()
	return sc.Text()
}

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

func eratosthenes(n int) []int {
	if n < 2 {
		return []int{}
	}

	r := int(math.Floor(math.Sqrt(float64(n))))
	list := make([]bool, n+1)
	list[0], list[1] = true, true

	for i := 2; i <= r; i++ {
		if !list[i] {
			for j := i * i; j <= n; j += i {
				list[j] = true
			}
		}
	}

	l := n / int(math.Ceil(math.Log(float64(n))))
	primes := make([]int, 0, l)
	for i, v := range list {
		if v {
			continue
		}
		primes = append(primes, i)
	}

	return primes
}
0