[Closed] Help Needed

Question : Utkarsh in Gardens

using namespace std;

int main(){

int v,n;
for(int i=0; i<v; i++){
	for(int j=0; j<v; j++){
		c[i][j] = n;
	long long int ans=0;
	for(int i=0; i<v; i++){
		for(int j=i+1; j<v; j++){
			n = (c[i]&c[j]).count();
			ans += (n*(n-1))/2;
	return 0;


Someone please explain the solution I will be very grateful. Thanks in advance.

A very elegant solution :smiley:

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.

Feel free to ask if something is not clear :slight_smile:

Sir , while entering the values we accessed the bitset using c[i][j] but in the loop c[i] and c[j]. Why?

Got it. Thanks

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






1 Like

@terriblecoder I have up voted you. Now you can ask questions separately. :slight_smile:

1 Like

Hi Deepansh, thank you very much :slight_smile:

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][1].
[1]: C++ bitset and its application - GeeksforGeeks

1 Like

No problem :slight_smile:

Can any one help on this please.

Can you please help me to understand on this?

Can any one help me solve this problem.

Hi Sachin, were you able to get the solution for this.