Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
Problem Description
DreamGrid has an integer sequence a1,a2,...,an and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements ai and aj (1<=i<j<=n) in the sequence when DreamGrid wasn't at home. When DreamGrid comes back, he finds with dismay that his precious sequence has been changed into a1,a2,...,ai-1,aj,ai+1,...,aj-1,ai,aj+1,...an
What's worse is that DreamGrid cannot remember his precious sequence. What he only remembers are the two values
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
Given the sequence after swapping and the two values DreamGrid remembers, please help DreamGrid count the number of possible element pairs (ai,aj) BaoBao swaps.
Note that as DreamGrid is poor at memorizing numbers, the value of x or y might not match the sequence, and no possible element pair can be found in this situation.
Two element pairs (ai,aj) (1<=i<j<=n) and (ap,aq) (1<=p<q<=n) are considered different if i≠p or j≠q.
Input
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first line contains three integers n, x and y (2<=n<=10^5,1<=x,y<=10^18), indicating the length of the sequence and the two values DreamGrid remembers.
The second line contains n integers b1,b2,...,bn (1<=bi<=10^5), indicating the sequence after swapping. It's guaranteed that
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
and
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
.
It's guaranteed that the sum of n of all test cases will not exceed 2*10^6.
Output
For each test case output one line containing one integer, indicating the number of possible element pairs BaoBao swaps.
Sample Input
2
6 61 237
1 1 4 5 1 4
3 20190429 92409102
1 2 3
Sample Output
2
Hint
For the first sample test case, it’s possible that BaoBao swaps the 2nd and the 3rd element, or the 5th and the 6th element.
题意:t 组数据,每组给出 n 个数的序列和 x、y,现在这个序列中两个数可能被交换了, x、y 的值是交换前的值,并且计算方式已经给出,问原序列中可能交换的数的对数
思路:
假设交换了 (i,j),那么:i*a[i] -> i*a[j],j*a[j] -> j*a[i]
因此对于原来的 x、y 有:
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
可以得到:
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
将得到的两个式子相除,有:
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program
然后根据式子左端
Element Swapping(ZOJ-4101)Problem DescriptionInputOutputSample InputSample OutputHintSource Program