print 7 - a + 7 - b + 7 - c
because the sum of the numbers on opposite faces is 7.
Reverse the string & replace 6 with 9
and 9 with 6
.
For all b[i]
, find how many values are there in C
with value i
. Then for every a[i]
, just add those values you have computed before.
I solved this in python to avoid any overflow issues. The idea is for each index starting from 0
to len(a + b) - 1
, you try to see if it's possible to put b
here. How to check that? You need to find how many ways are there if we put a
at current index which will be choose(a + b - 1, a - 1)
and if this value exceeds k
then we put b
(also subtract this value from k
) else put a
.
In this your are required to find how many nodes are under subtree u
with distance d
from root. First we will need euler tour to define which nodes are under any subtree. While doing euler tour, keep track of at what depth we have which nodes. While querying, you can use binary search in the above depth vector at depth d
to find how many nodes are at depth d
from root.