Problem:
Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>

int min(int a, int b);

int sub_child(int c1, int m1, int c2, int m2);

int gcd(int a, int b);

int main(void){
  int N, h, i, j, tmp1, tmp2, tmp3, c1, m1, n=0, w=0, not_found = 1;

  scanf("%d", &N);

  if(!(N%2)) printf("%d %d %d\n", N, N, N/2);
  else{
    
    if(N%4 == 3){
      h = (N+1)/4;
      printf("%d %d %d\n", h, 2*N*h, 2*N*h);
    }
    else{
      h = (N+3)/4;
      if(!(h%2)) printf("%d %d %d\n", h, N*h, N*h/2);
      else{
	for(i=h;i<min(N*3/4 + 1, 3500) && !w && not_found;i++){
	  tmp1 = sub_child(1, N, 1, i);
	  tmp2 = N*i;
	  if(!(tmp2%tmp1)){
	    h=i;
	    n=2*tmp2/tmp3;
	    w=n;
	    break;
	  }
	  else if(!(tmp1%2) && !(tmp2%(tmp1/2))){
	    h=i;
	    n=tmp2/(tmp1/2);
	    w=n;
	    break;
	  }
	  else{
	    for(j=tmp2/tmp1 + 1; j<min(2*tmp2/tmp1 + 1, 3500);j++){
	      //if tmp1/tmp2 - 1/j is in the form of 1/k 
	      if(!(tmp2*j % (tmp3=sub_child(tmp1, tmp2, 1, j)))){
		h=i;
		n=j;
		w=tmp2*j/tmp3;
		not_found=0;
		break;
	      }
	    }
	  }
	}
      }
    }
  }
  if(n) printf("%d %d %d\n", h, n, w);

  return 0;
}

  int min(int a, int b){
    if(a < b) return a;
    else return b;
  }

  /*c1/m1 - c2/m2を計算する。
    =(c1m2 - c2m1) / m1m2 */
  
int sub_child(int c1, int m1, int c2, int m2){
    return c1*m2 - c2*m1;
  }