回答編集履歴
7
edit
answer
CHANGED
@@ -47,10 +47,13 @@
|
|
47
47
|
---
|
48
48
|
|
49
49
|
大変遅かったのでnC2にすることにしました。
|
50
|
+
可読性は落ちましたが、m倍早くなりました。
|
51
|
+
たぶんね。
|
52
|
+
|
50
53
|
```python
|
51
54
|
from itertools import combinations as comb
|
52
55
|
def get_ans(m, n):
|
53
|
-
ans = sum(1 for nums in comb(range(1, min(m+1,n-2)), 2) if ((n-sum(nums)>0) and (n-sum(nums)
|
56
|
+
ans = sum(1 for nums in comb(range(1, min(m+1,n-2)), 2) if ((n-sum(nums)>0) and (n-sum(nums)>max(nums)) and (n-sum(nums)<=m)))
|
54
57
|
return ans
|
55
58
|
|
56
59
|
while True:
|
6
修正
answer
CHANGED
@@ -42,4 +42,21 @@
|
|
42
42
|
quit()
|
43
43
|
ans = get_ans(m, n)
|
44
44
|
print(ans)
|
45
|
+
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
大変遅かったのでnC2にすることにしました。
|
50
|
+
```python
|
51
|
+
from itertools import combinations as comb
|
52
|
+
def get_ans(m, n):
|
53
|
+
ans = sum(1 for nums in comb(range(1, min(m+1,n-2)), 2) if ((n-sum(nums)>0) and (n-sum(nums) > max(nums)) and (n-sum(nums)<=m)))
|
54
|
+
return ans
|
55
|
+
|
56
|
+
while True:
|
57
|
+
m, n = (int(x) for x in input().split())
|
58
|
+
if m==0 and n==0:
|
59
|
+
quit()
|
60
|
+
ans = get_ans(m, n)
|
61
|
+
print(ans)
|
45
62
|
```
|
5
修正
answer
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
```python
|
34
34
|
from itertools import combinations as comb
|
35
35
|
def get_ans(m, n):
|
36
|
-
ans = sum(1 for nums in comb(
|
36
|
+
ans = sum(1 for nums in comb(range(1, min(m+1,n-2)), 3) if sum(nums)==n)
|
37
37
|
return ans
|
38
38
|
|
39
39
|
while True:
|
4
修正
answer
CHANGED
@@ -25,4 +25,21 @@
|
|
25
25
|
if m==0 and n==0:
|
26
26
|
quit()
|
27
27
|
get_ans(m, n)
|
28
|
+
```
|
29
|
+
|
30
|
+
---
|
31
|
+
|
32
|
+
python的最適化。
|
33
|
+
```python
|
34
|
+
from itertools import combinations as comb
|
35
|
+
def get_ans(m, n):
|
36
|
+
ans = sum(1 for nums in comb(list(range(1, min(m+1,n-2))), 3) if sum(nums)==n)
|
37
|
+
return ans
|
38
|
+
|
39
|
+
while True:
|
40
|
+
m, n = (int(x) for x in input().split())
|
41
|
+
if m==0 and n==0:
|
42
|
+
quit()
|
43
|
+
ans = get_ans(m, n)
|
44
|
+
print(ans)
|
28
45
|
```
|
3
edit
answer
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
たとえば、
|
5
5
|
https://docs.python.jp/3/library/itertools.html#itertools.combinations
|
6
6
|
コンビネーションを使えば、3つの数字が同じになるものは探索しなくてすみます。
|
7
|
+
(実際は探索空間が6分の1くらいに減るのですが)
|
7
8
|
|
8
9
|
それとinp[0]>inp[1]は探索しなくて良いはずです。
|
9
10
|
|
2
edit
answer
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
満たさないものを弾くのではなく、数え上げた方が探索空間が少ないです。
|
2
|
+
(nC3とn^3どっちが大きいのかという話です。)
|
2
3
|
|
3
4
|
たとえば、
|
4
5
|
https://docs.python.jp/3/library/itertools.html#itertools.combinations
|
1
Edit
answer
CHANGED
@@ -4,4 +4,23 @@
|
|
4
4
|
https://docs.python.jp/3/library/itertools.html#itertools.combinations
|
5
5
|
コンビネーションを使えば、3つの数字が同じになるものは探索しなくてすみます。
|
6
6
|
|
7
|
-
それとinp[0]>inp[1]は探索しなくて良いはずです。
|
7
|
+
それとinp[0]>inp[1]は探索しなくて良いはずです。
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
たとえばこんなやつ。
|
12
|
+
```python
|
13
|
+
from itertools import combinations as comb
|
14
|
+
def get_ans(m, n):
|
15
|
+
ans = 0
|
16
|
+
for nums in comb(list(range(1, min(m+1,n-2))), 3):
|
17
|
+
if sum(nums) == n:
|
18
|
+
ans += 1
|
19
|
+
print(ans)
|
20
|
+
|
21
|
+
while True:
|
22
|
+
m, n = (int(x) for x in input().split())
|
23
|
+
if m==0 and n==0:
|
24
|
+
quit()
|
25
|
+
get_ans(m, n)
|
26
|
+
```
|