Menu Close

Check if Rubik’s cube of any dimensions can be assembled

Can you please tell whether Pathan will be able to build such a Rubik’s cube or not?

Pathan likes solving Rubik’s cube a lot. He spends a lot of time in getting expertise in solving not only the 3 * 3 * 3 cube, but also the cubes of higher dimensions like 4 * 4 * 4, 5 * 5 * 5 and so on.

ZoZo has a very famous toy shop which sells Rubik’s cubes. This shop has interesting rules. Each day it sells cubes of a fixed dimension.  Pathan has to buy new cubes daily primarily due to two reasons, one he handles the cube very roughly and the other that he solves the cube so many times during the day.

Today the shop is selling K * K * K size Rubik’s cubes. In the morning, Pathan bought a cube from the shop. He had just started playing with it, suddenly his cute little sisters asked him to give them C units of the cube.  Pathan’s did not want to disappoint his sisters, so he immediately disassembled the cube into K * K * K units and gave C of those to his sisters.

Now Pathan wants to solve the Rubik’s cube again, so he thought of going to market and buy some cubes so that he can create a Rubik’s cube from those. 

The newly created cube can be of any dimension. For achieving that, he can disassemble and reassemble the cubes in the way he wants, but he does not want to waste any units of the cubes.

Can you please tell whether Pathan will be able to build such a Rubik’s cube or not?

Input:
The first line of the input contains an integer T denoting the number of the test cases.

Each test case contains two space separated integers K, C as defined in the statement.


Output:
Print the output, containing "YES" or "NO" (without quotes) corresponding to the situation.

#include <stdio.h>
#include <limits.h>
#include <string.h>
#define ll long long int
long long int calc[101][1000001];
void Cube(){
  int k,c;
  scanf("%d %d",&k,&c);
  if(c==0 || calc[k][k*k*k-c]==1)
  printf("YES\n");
  else
  printf("NO\n");
}
int main(){
  ll t ,i,j,val,cubed;
  for(i=1;i<101;i++){
    cubed=i*i*i;
    for(j=0;j<cubed;j++){
      val=(j*j*j)%cubed;
      calc[i][val]=1;
    }
  }
  scanf("%lld",&t);
  while(t--){
    Cube();
  }
return 0;
}


INPUT_1:
3
6  2
5  3
3  1

OUTPUT:
NO
YES
YES


INPUT_2:
4
2  4
7  2
4  1
5  2

OUTPUT:
NO
NO
YES
YES


ILLUSTRATION

Executed using gcc

Morae Q!