SPOJ - Count on Cantor (CANTON) Problem Solution
Solution:-
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;
}
No comments:
Post a Comment