結果
問題 | No.1782 ManyCoins |
ユーザー | 草苺奶昔 |
提出日時 | 2024-09-08 23:36:54 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 681 ms / 2,000 ms |
コード長 | 2,120 bytes |
コンパイル時間 | 13,379 ms |
コンパイル使用メモリ | 235,744 KB |
実行使用メモリ | 11,864 KB |
最終ジャッジ日時 | 2024-09-08 23:37:15 |
合計ジャッジ時間 | 18,040 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 33 ms
6,944 KB |
testcase_14 | AC | 21 ms
6,940 KB |
testcase_15 | AC | 19 ms
6,940 KB |
testcase_16 | AC | 50 ms
6,940 KB |
testcase_17 | AC | 63 ms
6,944 KB |
testcase_18 | AC | 53 ms
6,944 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 24 ms
6,944 KB |
testcase_21 | AC | 14 ms
6,944 KB |
testcase_22 | AC | 40 ms
6,944 KB |
testcase_23 | AC | 117 ms
6,944 KB |
testcase_24 | AC | 30 ms
11,860 KB |
testcase_25 | AC | 3 ms
6,940 KB |
testcase_26 | AC | 62 ms
6,940 KB |
testcase_27 | AC | 4 ms
6,940 KB |
testcase_28 | AC | 31 ms
11,860 KB |
testcase_29 | AC | 74 ms
11,864 KB |
testcase_30 | AC | 97 ms
11,864 KB |
testcase_31 | AC | 137 ms
11,864 KB |
testcase_32 | AC | 171 ms
11,864 KB |
testcase_33 | AC | 345 ms
11,864 KB |
testcase_34 | AC | 508 ms
11,860 KB |
testcase_35 | AC | 681 ms
11,864 KB |
testcase_36 | AC | 26 ms
6,940 KB |
testcase_37 | AC | 22 ms
6,940 KB |
testcase_38 | AC | 5 ms
6,944 KB |
testcase_39 | AC | 9 ms
6,944 KB |
testcase_40 | AC | 34 ms
6,944 KB |
testcase_41 | AC | 33 ms
6,940 KB |
testcase_42 | AC | 21 ms
6,944 KB |
testcase_43 | AC | 23 ms
9,808 KB |
testcase_44 | AC | 22 ms
6,944 KB |
testcase_45 | AC | 18 ms
6,940 KB |
testcase_46 | AC | 2 ms
6,940 KB |
testcase_47 | AC | 1 ms
6,944 KB |
testcase_48 | AC | 2 ms
6,940 KB |
testcase_49 | AC | 1 ms
6,944 KB |
testcase_50 | AC | 31 ms
11,860 KB |
ソースコード
package main import ( "bufio" "fmt" "os" ) func main() { P2371() } // P2371 [国家集训队] 墨墨的等式 // https://www.luogu.com.cn/problem/P2371 // No.1782 ManyCoins // https://yukicoder.me/submissions/1008663 // 给定n个系数coeffs和上下界lower,upper // !对于 lower<=k<=upper 求有多少个k能够满足 // !a0*x0+a1*x1+...+an*xn=k // n<=12 0<=ai<=5e5 1<=lower<=upper<=2^63-1 // !时间复杂度:O(n*ai) func P2371() { in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n, lower, upper int fmt.Fscan(in, &n, &upper) lower = 1 coeffs := make([]int, n) for i := 0; i < n; i++ { fmt.Fscan(in, &coeffs[i]) } retain(&coeffs, func(i int) bool { return coeffs[i] != 0 }) if len(coeffs) == 0 { fmt.Fprintln(out, 0) return } base, dp := ModShortestPath(coeffs) res := 0 for i := range base { if upper >= dp[i] { res += (upper-dp[i])/base + 1 } if lower > dp[i] { res -= (lower-1-dp[i])/base + 1 } } fmt.Fprintln(out, res) } const INF int = 1e18 func ModShortestPath(coeffs []int) (int, []int) { coeffs = append(coeffs[:0:0], coeffs...) retain(&coeffs, func(i int) bool { return coeffs[i] > 0 }) if len(coeffs) == 0 { return 0, nil } base := mins(coeffs...) dp := make([]int, base) for i := range dp { dp[i] = INF } dp[0] = 0 for _, v := range coeffs { cycle := gcd(base, v) for j := 0; j < cycle; j++ { for cur, count := j, 0; count < 2; { next := (cur + v) % base dp[next] = min(dp[next], dp[cur]+v) cur = next if cur == j { count++ } } } } return base, dp } func gcd(a, b int) int { for b != 0 { a, b = b, a%b } 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 } func mins(nums ...int) int { res := nums[0] for _, num := range nums { if num < res { res = num } } return res } func retain(arr *[]int, f func(index int) bool) { ptr := 0 n := len(*arr) for i := 0; i < n; i++ { if f(i) { (*arr)[ptr] = (*arr)[i] ptr++ } } *arr = (*arr)[:ptr:ptr] }