Match the recorded pass times against milestone positions to count feasible speeds and list each possible first gap.
Easy3Brute forceMathInterviewNo attempts yetTime limit1sMemory limit256 MBDriving along an Irish country road you pass small grey stones set by the wayside, spaced about a mile apart. They are milestones, put there to mark distance. All of them are old, some went missing over the years, and only part of the original line still stands.
You drive at a constant speed and pass M of the remaining milestones in a row, writing down the time at which you pass each one. You do not know your own speed, but you do know where all N remaining milestones sit along the road. That record is enough to work out how fast you were going.
Write the recorded times as T1<T2<⋯<TM and the milestone positions as X1<X2<⋯<XN. A speed v is possible when some start index j satisfies
Xj+i−1−Xj=v(Ti−T1)
for every i=1,2,…,M. Speed is measured in miles per hour.
Find how many distinct speeds are possible, and for each of them the distance between the first milestone you passed and the second one.
The first line has two integers M and N (2≤M≤N≤103): the number of milestones you passed in a row, and the total number of milestones left along the road.
The second line has the times T1,T2,…,TM in hours at which you passed a milestone, in increasing order. The values are distinct and 0≤Ti≤1015.
The third line has the positions X1,X2,…,XN of the milestones in miles, in increasing order. The values are distinct and 0≤Xi≤1015.
Print two lines.
On the first line print the number of distinct speeds the car could have been travelling at.
On the second line print every possible distance between the first milestone you passed and the second one, in increasing order and separated by single spaces. Leave the second line empty when no speed is possible.