Evolutionary Algorithms
시간 제한40초메모리 제한1024 MB
b가 a의 조상이지만 c의 조상이 아니고, S_b가 S_a와 S_c 각각의 K배보다 큰 순서 있는 삼중항 (a,b,c)의 개수를 센다.
문제
Ada is working on a science project for school. She is studying evolution and she would like to compare how different species of organisms would perform when trying to solve a coding competition problem.
The species are numbered with integers between and , inclusive. Species has no direct ancestor, and all other species have exactly one direct ancestor each, from which they directly evolved. A (not necessarily direct) ancestor of species is any other species such that can be reached from by moving one or more times to a species direct ancestor starting from . In this way, species is a (direct or indirect) ancestor of every other species.
Through complex genetic simulations, she calculated the average score each of the species would get in a particular coding competition. is that average score for species .
Ada is looking for interesting triplets to showcase in her presentation. An interesting triplet is defined as an ordered triplet of distinct species such that:
- Species is a (direct or indirect) ancestor of species .
- Species is not a (direct or indirect) ancestor of species .
- Species has an average score strictly more than times higher than both of those of and . That is, .
Given the species scores and ancestry relationships, help Ada by writing a program to count the total number of interesting triplets.
입력
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains two integers and , denoting the number of species and the factor which determines interesting triplets, respectively.
The second line of each test case contains integers , where denotes the average score of species .
The third line of each test case contains integers , meaning species is the direct ancestor of species .
출력
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the total number of interesting triplets according to Ada's definition.
제한
- .
- .
- , for all .
- , for all .
- Species is a (direct or indirect) ancestor of all other species.
힌트

In Sample Case #1, there is only one possible interesting triplet: . Indeed, we can verify that:
- Species is an ancestor of species .
- Species is not an ancestor of species .
- The score of species is more than times higher than the scores of both and : .

In Sample Case #2, there are seven interesting triplets: