Search This Blog

Showing posts with label SPOJ Count on Cantor Solution. Show all posts
Showing posts with label SPOJ Count on Cantor Solution. Show all posts

Thursday, 18 August 2016

SPOJ - Count on Cantor Problem Solution

SPOJ - Count on Cantor (CANTON) Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 5005
#define lim 10000010

ll arr[MAX];

void init()
{
    int i=1;
    while(1)
    {
        arr[i]=i+arr[i-1];
        if(arr[i]>=lim)
            break;
        i++;
    }
}

int main()
{
    init();
    ll tc,num;
    cin>>tc;
    while(tc--)
    {
        cin>>num;
        int i=0;
        while(arr[i]<num)
            i++;

        ll diff=num-arr[i-1];

        cout<<"TERM "<<num<<" IS ";

        if(i%2==0)
            cout<<diff<<"/"<<i+1-diff;
        else
            cout<<i+1-diff<<"/"<<diff;

        cout<<endl;
    }
    return 0;

}