题目:
明明觉得hash是个好算法,代码短、效率高。某天,他碰到了一个求正方形个数的问题,于是很淡定地枚举对角线然后用hash判存在,妥妥的搞定,但是提交后却wa了几个点。仔细观察其hash函数为: h=x*y+x+y 。为了让明明知道这个函数存在什么问题,对于给出一个h值,请你来告诉他有多少对(x,y)满足上述式子(max(x,y)<=h;h,x,y都为非负整数)。
分析:
推算,h=xy+x+y=(x+1)(y+1)-1;h+1=(x+1)(y+1),所以x+1=h+1/y+1,同理y+1=h+1/x+1,因为这个数是个非负整数,所以X+1,Y+1一定是H+1的因数,这样就是求H+1的因数个数了。
附上代码:
var n,h:longint; function work(num:longint):longint; var i:longint; begin work:=2; for i:=2 to trunc(sqrt(num)) do if num mod i=0 then if num div i=i then inc(work) else inc(work,2); end; procedure init; var i:longint; begin readln(n); for i:=1 to n do begin readln(h); writeln(work(h+1)); end; end; begin assign(input,'hash.in');reset(input); assign(output,'hash.out');rewrite(output); init; close(input);close(output); end.