constraints
- n取值範圍為1到1,000,000,000(含)
examples
0)
1returns: 0
1)
1729
returns: 1
1729=1^3+12^3
1729=9^3+10^3
2)
475574
returns: 27
因為題中大量使用3次方計算,這裡我們也可以減少重複計算。原理公式為:
(n + 1)^2 = n^2 + 2n + 2
(n + 1)^3 = n^3 + 3n^2 + 3n + 1
所以我的實現演算法如下:
code
class
program
watch.stop();
console.writeline(
"********************==");
foreach
(keyvaluepair
<
int, list
>>
pair
inret)
^3 + ^3 =
", val[
0], val[
1], pair.key);}}
}console.writeline(
"********************==");
console.writeline(twice.calccount(
1729
));console.writeline(twice.calccount(
475574
));console.writeline(
"用時:毫秒
", watch.elapsedmilliseconds);
console.writeline(
"********************==");
console.writeline(t2.count(
1729
));console.writeline(t2.count(
475574
));watch.start();
for(
inti =0
; i
<
1000
; i++
)watch.stop();
console.writeline(
"用時:毫秒
", watch.elapsedmilliseconds);
console.readkey();}}
//////
evlon演算法
///public
class
twicesupercubic
);private
static
list
<
int>
pow3
=new
list
<
int>
(new
int);
public
intcalccount(
intn)
public
intcalccount(
intn,
outdictionary
<
int, list
>>
debuginfo)
intcount =0
;for
(intx =
1; x
<
maxi;
++x)
}));
found
=true;}
else
);if
(val.count ==2
)count++;
else
count--;
}}else}if
(!found)
break;}
return
count;}}
//////
ealget的演算法
///class
t2_cubecountdict.clear();
//b <= n 的 3 次方根
intmax_b =(
int)math.pow((
double
)n, 1f /3
);//
構造整數三次方計算快取
for(
inti
=_cubecache.count; i
<=
max_b; i++)
intcount =0
;inta =
1; //正整數
intb =a;
intsumcube;
sumcube
=_cubecache[a]
+_cubecache[b];
do//
擊中超過1次,計數減1
//存在三組或以上組合相等的情況時走到這個分支
if(hit ==2
)//擊中數加一
_cubecountdict[sumcube]++;
}elseb++
;if(b >
max_b)
sumcube
=_cubecache[a]
+_cubecache[b];}a
++;b =
a;if
(b >
max_b)
sumcube
=_cubecache[a]
+_cubecache[b];
}while
(sumcube
<=
n);return
count;}}
有道難題 雙倍超立方數 的解答
昨天進行的有道難題資格賽group17中的 難題 problem statement 雙倍超立方數是指乙個正整數可以正好被拆分為兩種不同的a 3 b 3的方式,其中a,b均為整數且0 2 475574 returns 27 人太笨,我的程式輸入太大的數時執行時間總是2.016s,多出16毫秒,今天在...
我的有道第二題(不是雙倍超立方)
先說明一下,我遇到的第二題跟大家先前討論的第二題題目不同,不過最近演算法挺火,也就放上來,大家一起討論討論,而且我覺得有道這次比賽非常好,我看了下topcode平台,大家平時也可以進這個平台練習一下演算法,不過。有道的翻譯還真有待提高了 宣告一下,我演算法沒有學過,只是想到了解決的方法,當時理解題目...
超簡單的麻將演算法
麻將的演算法 提高篇 1.先說說,數值的構成。型別字 0 東南西北中發白 1 2,3 4,5 6,7 型別萬 1 1 萬2萬3萬4 萬5萬6 萬7萬8 萬9萬 11,12 13,14 15,16 17,18 19型別條 2 1條2 條3條4 條5條6 條7條8 條9條 21,22 23,24 25,...