Solution of 11428 - Cubes

Problem Descriptio
source:https://uva.onlinejudge.org/external/114/11428.html

Given a positive integer N you will have to find two positive integers x and y such that:
                                                               N = x3 − y3

Input 

The input file contains at most 100 lines of inputs. Each line contains a positive integer N (0 < N ≤ 10000). Input is terminated by a line containing a single zero. This line should not be processed. 

Output 

For each line of input produce one or more lines of output. Each of these lines contains two positive integers x, y separated by a single space, such that N = x3 − y3 . If there is no such integer values of x and y then produce the line ‘No solution’ instead. If there is more than one solution then output the one with smallest value of y.

Sample Input 


37 
12 


Sample Output 

2 1
4 3 
No solution


Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    long int n, x, y, i, j,value,get;
    float p, l;
    int start,limit;
    while(scanf("%ld",&n)==1)
    {
        if(n==0)
            break;
        get=0;
        limit=(int)sqrt(n);
        p=(float)1/3;
        start=(int)pow(n,p);
        for(i=start;i<=limit;i++)
        {
            for(j=1;j<=i;j++)
            {
                value=(i*i*i)-(j*j*j);
                if(n==value)
                {
                    x=i;
                    y=j;
                    get=1;
                    break;

                }
            }
            if(get==1)
                break;
        }
        if(get==1)
            printf("%ld %ld\n",x,y);
        else
            printf("No solution\n");

    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience