Friday, April 24, 2015

UVa - 264 - Count on Cantor

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long term, x, y = 0, p, q, temp;
    while(cin >> term){
        x = (sqrt(1+8*term)-1)/2;
        temp = (x*(x+1))/2;
        if(temp == term){
            if(x%2)cout << "TERM " << term << " IS 1/" << x << endl;
            else cout << "TERM " << term << " IS " << x << "/1" << endl;
            continue;
        }
        x += 1;
        y = (x*(x+1))/2;
        if(x%2){
            p = 1 + abs(y-term);
            q = x - abs(y-term);
        }
        else{
            p = x - abs(y-term);
            q = 1 + abs(y-term);
        }
        cout << "TERM " << term << " IS " << p << "/" << q << endl;
    }
    return 0;
}

No comments:

Post a Comment