Modified LCS
Time limit1sMemory limit128 MB
Count the terms shared by two strictly increasing arithmetic progressions, which equals their longest common subsequence.
- Level
Medium5 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
LCS stands for longest common subsequence, a well known problem. A sequence in this problem is a list of integers, and a sequence is a subsequence of a sequence when you can obtain by deleting zero or more elements from and leaving the remaining elements in their original order.
You are given two sequences. Find the length of the longest sequence that is a subsequence of both of them.
The sequences themselves are not given. Each sequence is described by three integers , and , where is the length of the sequence and is its first element. Every element except the first is greater than the element before it by .
For example, , and describes the sequence .
At least one integer belongs to both sequences and is not greater than 1,000,000.
Input
The first line contains a single integer , the number of test cases (). Each of the next lines describes one test case and contains six integers separated by a single space, , , , , , (, ). They are the length of the first sequence, the first element of the first sequence, the increment of the first sequence, the length of the second sequence, the first element of the second sequence, and the increment of the second sequence, in that order.
Output
For each test case, print one line with a single integer, the length of the longest common subsequence of the two sequences.