Skip to content

Commit 5ebdf4d

Browse files
authored
Merge pull request #62 from myoshi2891/dev/macbook_pro
Dev/macbook pro
2 parents f0bca8b + 04435fb commit 5ebdf4d

9 files changed

Lines changed: 871 additions & 19 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
この解法は、64ビット整数を使わずに、32ビット整数の範囲を超えないかを都度チェックしながら桁を逆にしていきます。
2+
3+
---
4+
5+
### ✅ TypeScriptコード(`fs`での入力読み取り対応)
6+
7+
```ts
8+
import * as fs from 'fs';
9+
10+
const input = fs.readFileSync('/dev/stdin', 'utf8').trim();
11+
const x = parseInt(input);
12+
13+
/**
14+
* 32ビット整数の範囲
15+
*/
16+
const INT_MIN = -(2 ** 31);
17+
const INT_MAX = 2 ** 31 - 1;
18+
19+
function reverse(x: number): number {
20+
let result = 0;
21+
let num = x;
22+
23+
while (num !== 0) {
24+
const digit = num % 10 | 0; // 小数点対策で |0 して整数化
25+
num = (num / 10) | 0;
26+
27+
// resultが次の桁を追加したときにオーバーフローしないか確認
28+
if (
29+
result > Math.floor(INT_MAX / 10) ||
30+
(result === Math.floor(INT_MAX / 10) && digit > 7)
31+
) {
32+
return 0;
33+
}
34+
if (
35+
result < Math.ceil(INT_MIN / 10) ||
36+
(result === Math.ceil(INT_MIN / 10) && digit < -8)
37+
) {
38+
return 0;
39+
}
40+
41+
result = result * 10 + digit;
42+
}
43+
44+
return result;
45+
}
46+
47+
console.log(reverse(x));
48+
```
49+
50+
---
51+
52+
### 🔍 処理概要(図付きで説明)
53+
54+
たとえば `x = -123` のとき:
55+
56+
```
57+
初期状態: result = 0, num = -123
58+
59+
ループ1:
60+
digit = -123 % 10 = -3
61+
num = -123 / 10 = -12
62+
result = 0 * 10 + (-3) = -3
63+
64+
ループ2:
65+
digit = -12 % 10 = -2
66+
num = -12 / 10 = -1
67+
result = -3 * 10 + (-2) = -32
68+
69+
ループ3:
70+
digit = -1 % 10 = -1
71+
num = -1 / 10 = 0
72+
result = -32 * 10 + (-1) = -321
73+
74+
終了(num === 0)→ return -321
75+
```
76+
77+
---
78+
79+
### 🛑 オーバーフローの例
80+
81+
例:`x = 1534236469`
82+
83+
```
84+
途中で result = 964632435 になり、
85+
次の桁(9)を追加すると 9646324350 + 9 = 9646324359 > INT_MAX
86+
→ return 0
87+
```
88+
89+
---
90+
91+
### ✅ テストケース例
92+
93+
```plaintext
94+
Input:
95+
123
96+
Output:
97+
321
98+
99+
Input:
100+
-123
101+
Output:
102+
-321
103+
104+
Input:
105+
120
106+
Output:
107+
21
108+
109+
Input:
110+
1534236469
111+
Output:
112+
0
113+
```
114+
115+
---
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as fs from 'fs';
2+
3+
const input = fs.readFileSync('/dev/stdin', 'utf8').trim();
4+
const x = parseInt(input);
5+
6+
/**
7+
* 32ビット整数の範囲
8+
*/
9+
const INT_MIN = -(2 ** 31);
10+
const INT_MAX = 2 ** 31 - 1;
11+
12+
function reverse(x: number): number {
13+
let result = 0;
14+
let num = x;
15+
16+
while (num !== 0) {
17+
const digit = num % 10 | 0; // 小数点対策で |0 して整数化
18+
num = (num / 10) | 0;
19+
20+
// resultが次の桁を追加したときにオーバーフローしないか確認
21+
if (
22+
result > Math.floor(INT_MAX / 10) ||
23+
(result === Math.floor(INT_MAX / 10) && digit > 7)
24+
) {
25+
return 0;
26+
}
27+
if (
28+
result < Math.ceil(INT_MIN / 10) ||
29+
(result === Math.ceil(INT_MIN / 10) && digit < -8)
30+
) {
31+
return 0;
32+
}
33+
34+
result = result * 10 + digit;
35+
}
36+
37+
return result;
38+
}
39+
40+
console.log(reverse(x));
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"sort"
8+
"strconv"
9+
)
10+
11+
const MOD = 1_000_000_007
12+
13+
type FenwickTree struct {
14+
n int
15+
data []int
16+
}
17+
18+
func NewFenwickTree(n int) *FenwickTree {
19+
return &FenwickTree{
20+
n: n,
21+
data: make([]int, n+2),
22+
}
23+
}
24+
25+
func (ft *FenwickTree) Add(i, x int) {
26+
i++
27+
for i <= ft.n+1 {
28+
ft.data[i] = (ft.data[i] + x) % MOD
29+
i += i & -i
30+
}
31+
}
32+
33+
func (ft *FenwickTree) Sum(i int) int {
34+
i++
35+
res := 0
36+
for i > 0 {
37+
res = (res + ft.data[i]) % MOD
38+
i -= i & -i
39+
}
40+
return res
41+
}
42+
43+
func (ft *FenwickTree) RangeSum(l, r int) int {
44+
return (ft.Sum(r) - ft.Sum(l-1) + MOD) % MOD
45+
}
46+
47+
func lowerBound(a []int, x int) int {
48+
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
49+
}
50+
51+
func upperBound(a []int, x int) int {
52+
return sort.Search(len(a), func(i int) bool { return a[i] > x })
53+
}
54+
55+
func main() {
56+
scanner := bufio.NewScanner(os.Stdin)
57+
scanner.Split(bufio.ScanWords) // 単語(スペース区切り)単位で読み取り
58+
59+
readInt := func() int {
60+
scanner.Scan()
61+
n, _ := strconv.Atoi(scanner.Text())
62+
return n
63+
}
64+
65+
N := readInt()
66+
W := readInt()
67+
L := readInt()
68+
R := readInt()
69+
70+
X := make([]int, N)
71+
for i := 0; i < N; i++ {
72+
X[i] = readInt()
73+
}
74+
75+
// 全地点(スタート0、足場、ゴールW)をリストアップ
76+
positions := append([]int{0}, X...)
77+
positions = append(positions, W)
78+
sort.Ints(positions)
79+
80+
posIndex := make(map[int]int)
81+
for i, v := range positions {
82+
posIndex[v] = i
83+
}
84+
85+
n := len(positions)
86+
dp := make([]int, n)
87+
dp[0] = 1
88+
89+
ft := NewFenwickTree(n)
90+
ft.Add(0, 1)
91+
92+
for i := 1; i < n; i++ {
93+
cur := positions[i]
94+
left := cur - R
95+
right := cur - L
96+
97+
li := lowerBound(positions, left)
98+
ri := upperBound(positions, right) - 1
99+
100+
if li <= ri {
101+
val := ft.RangeSum(li, ri)
102+
dp[i] = val
103+
ft.Add(i, val)
104+
}
105+
}
106+
107+
fmt.Println(dp[n-1])
108+
}
109+
110+
// Go 解法で 20件の入力で ランタイムエラー(panic) が発生するとのこと、原因として考えられるのは以下のいずれかです:
111+
112+
// ❗️ 原因候補
113+
// bufio.Scanner の 2行しか読んでいない
114+
// 実際には N が大きくなると 複数行に分かれて入力される 可能性がある。
115+
// つまり "5 65 7 37" の次に "5 15 30 50 55" でなく、改行を含む複数行に X が分割されている可能性がある。
116+
// Xstr := strings.Fields(scanner.Text()) だけで全 N 要素を取得できない。
117+
118+
// ✅ 修正方針
119+
// N 個の X[i] を読み切るまで ループで scanner.Scan() を繰り返す
120+
// X := make([]int, N) を安全に埋める
121+
122+
// ✅ 修正点まとめ
123+
// 修正箇所 内容
124+
// scanner.Split(...) 単語単位で int を逐次読み取り可能に
125+
// readInt() 関数 scanner.Scan() + strconv.Atoi() のラッパー
126+
// X[i] = readInt() 必ず N 件読み切るようループで読み取り

0 commit comments

Comments
 (0)