-4

Let h(y) be the function defined as (a*y+b)mod m. So h(y) can take values from 0 to m-1. Now we are given 7 integers- a,b,x,n,c,d,m. Our task is to find the total count of h(x),h(x+1),h(x+2)...h(x+n) such that the value of h(x+i) falls in the range of [c,d].where 0<=i<=n Integer limits are:

1 ≤ m ≤ 10^15, c ≤ d < m, a,b < m, x+n ≤ 10^15, and a*(x+n) + b ≤ 10^15

For Example. for input set {1,0,0,8,0,8,9} the output should be 9. Please suggest an efficient algorithm. Thanks!!!

1 Answers1

0

This isn't a particularly strong hash. The only hard part about this problem is the obtuse notation with single-letter variables and specifying the problem as a 7-tuple.

Each increment of x increases h(x) by a. Therefore the total distance along x to get from c to d is simply (d-c)/a. Add one for the fencepost problem, or specify the problem with a half-open range for the sake of sanity.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421