AryamanVerma's blog

By AryamanVerma, history, 16 months ago, In English

The problem statement is:- Two sequences A and B are said to be compatible if A can be converted to B in any number of moves of the following type: - Replace any element of the sequence with -1*(S) where S is equal to the sum of all elements in the current sequence. Now, given two sequences X and Y. Count the number of subsequences of X that are compatible with Y . Print the answer modulo 109+7 Input Each test contains multiple test cases. The first line contains the number of test cases, T. The description of the test cases follows. The first line of each test case contains two integers n, m where n and m are the sizes of the sequence X and Y respectively. The second line of each test case contains n integers X1, X2,. , Xn The third line of each test case contains m integers Y1, Y2,. , Ym. Constraints: 1 <= T <= 500 1 <= m <= n <= 2*105 -109 <= Xi, Yi <= 109 The sum of n over all test cases is less than or equal to 2*105 Output For each test case print an integer, the number of subsequences of X which are compatible with Y modulo 109+7. Two subsequences are compatible if they are permutations of each other , since after replacing for the first time, we have as 'sum of the subsequence' -(the element replaced) and it can be exchanged with any other element thereby making it the new sum. And at last we can put an element of the subsequence at the place where the first sum was placed. Ex:- 7 4 2 sum of subsequence is 13 2 7 4 replacing 7 with -13 , -13 4 2 (new sum is -(the element replaced) i.e -7) replacing 4 with -(-7) , -13 7 2 (new sum is -4) replacing 2 with -(-4) , -13 7 4 (new sum is -2) replacing -13 with -(-2) , 2 7 4 (new sum is the first sum i.e 13) So, such permutations can be counted by storing the frequencies of the common elements of A and b and counting the no of combinations nCr. But what if -13 is actually an element of B. Ex:- A is 7 4 2 B is 2 7 4 -13 (7,4) (4,2) (7,2) (7,4,2) these will be counted but how will this compatible subsequence be counted. 7 4 2 2 4 -13 How can such cases where 'sum of some elements of A are elements of B' be handled?
  • Vote: I like it
  • 0
  • Vote: I do not like it