For each vertex the corresponding row in the adjacency matrix is saved as a bitset for efficiency. It could have been saved as an int array too but that would give TLE. Now for a pair of vertices i and j, the AND of their two bitsets will give a bitset that represents the vertices connected to both i and j. Let this bitset contain n such vertices. If we consider i and j the opposite vertices of the garden of 4 vertices, then we can pick any 2 vertices out of these n vertices to complete a loop of length 4. The number of ways to pick 2 elements out of n is nC2, which is (n*(n-1))/2. Summing up for all i,j pairs will give us twice the total, because each loop will be counted twice, once by each pair of opposite vertices. Hence the ans/2 at the end.
Hi, I do not have enough karma points to ask a separate question(sorry for posting the question here. But I badly want to solve this problem). Can someone please help me to decipher this question. I do not understand the sample output given.
Micro’s math teacher just taught him about Modular Arithmetic, and gave him an assignment to solve. Assignment is really large and will take a lot of his time. Micro wants to go out and play as soon as possible. The assignment consists of some decimal numbers and Micro has to find out the value of their modulo 10^9+7.
First line consists of T denoting the number of test cases. Each of the following T lines consists of a decimal number.
For each test case print the value of decimal number modulo 10^9, and if modulo does not exists print -1. Print a new line after each test case.
1≤Total number of digits in the number≤18
1≤Number of digits after decimal point≤9
If you consider a bitset as an array of bits that will make it easier to understand. So c is an array of bitsets, or a 2D array of bits. c[i] is a single bitset, or a 1D array of bits. And c[i][j] is the jth bit in the bitset c[i].
While taking input each bit needs to be set according to the input, so each bit is accessed as c[i][j]. And while calculation we just need to AND 2 bitsets, which are c[i] and c[j].
For more about bitsets see [here].
: C++ bitset and its application - GeeksforGeeks