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 we are given a tree and for given queries, we 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(their start time). Then for each query you need to find how many values are there in range start_time[u]
to end_time[u]
with value d
. So we can use binary search in depth vector above at given depth d
to find how many values it has from start_time[u]
to end_time[u]
.