回答編集履歴

1

実際にやってみた

2018/08/14 10:25

投稿

maisumakun
maisumakun

スコア145184

test CHANGED
@@ -3,3 +3,69 @@
3
3
 
4
4
 
5
5
  三角数は`n(n+1)/2`となりますが、`n`と`n+1`は互いに素なので、「`n`と`n+1`のうち、奇数の方」と「`n`と`n+1`のうち、偶数を2で割ったもの」で分けて約数の個数を計算して、それをかければ、一気に計算量を減らせます。
6
+
7
+
8
+
9
+ ```java
10
+
11
+ static int dividors(int num) {
12
+
13
+ int divisors = 1;
14
+
15
+
16
+
17
+ for (int i = 2; i <= num; i++) {
18
+
19
+ if (num % i == 0) {
20
+
21
+ divisors++;
22
+
23
+ }
24
+
25
+ }
26
+
27
+ return divisors;
28
+
29
+ }
30
+
31
+
32
+
33
+ static void func(int num, int cnt) {
34
+
35
+ for(int n = cnt; ;++n) {
36
+
37
+ int x, y;
38
+
39
+ if(n % 2 == 1) {
40
+
41
+ x = n;
42
+
43
+ y = (n + 1) / 2;
44
+
45
+ } else {
46
+
47
+ x = n / 2;
48
+
49
+ y = n + 1;
50
+
51
+ }
52
+
53
+ if(dividors(x) * dividors(y) > 500) {
54
+
55
+ System.out.println("Answer:" + ((long)x * y));
56
+
57
+ return;
58
+
59
+ }
60
+
61
+ }
62
+
63
+ }
64
+
65
+
66
+
67
+ ```
68
+
69
+
70
+
71
+ [paiza.ioで実行した結果](https://paiza.io/projects/76lwN_RrsCe3G6p05dbxKA?language=java)