|
发表于 2017-9-4 14:33:03
|
显示全部楼层
#(x + y + z)^n = ∑(n!/(r!* s!* t!) * x^r * y^s * z^t) ,r+s+t=n
所以问题变成 n!/(r!* s!* t!) 里有多少10^12的倍数,r+s+t=n
10^12分解成因子就是12个2和12个5
问题变成n!/r!s!t!有几个的2和5的因子数都大于等于12
算法很简单
只所以采用pascal实在是因为python计算太慢了(也可能我的算法不够优化)
delphi7运行的话大概1分半,python目测起码10个小时。。。
- program p154;
- {$APPTYPE CONSOLE}
- uses
- SysUtils, Types, Windows,Math;
- const
- maxn = 200000;
- type
- nlist = array[0..maxn+5] of integer;
- var
- st: DWord;
- twolist, fivelist: nlist;
- cnt: integer;
- r,s,t: integer;
- function getXcountin1n(const x: word): nlist;
- var
- cnt: integer;
- i,i1: integer;
- begin
- cnt := 0;
- for i:=0 to x-1 do
- result[i] := 0;
- i := x;
- while i <= maxn do
- begin
- i1 := i;
- while (i1 > 0) and (i1 mod x = 0) do
- begin
- inc(cnt);
- i1 := i1 div x
- end;
- for i1:= i to i+x-1 do
- result[i1] := cnt;
- inc(i, x);
- end;
- end;
- begin
- st := gettickcount();
- twolist := getXcountin1n(2);
- fivelist := getXcountin1n(5);
- cnt := 0;
- for r:= 0 to maxn do
- begin
- for s:= 0 to (maxn-r) do
- begin
- t := maxn-r-s;
- if (twolist[maxn]-twolist[r]-twolist[s]-twolist[t] >= 12) and (fivelist[maxn]-fivelist[r]-fivelist[s]-fivelist[t] >= 12) then
- inc(cnt);
- end;
- end;
- writeln(cnt); //479742450 87.4s
- writeln(format('cost %n s', [(gettickcount-st)/1000]));
- readln; //wait Enter
- end.
复制代码 |
|