Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/46a6f31d1848152943ec9399333e4bc0 to your computer and use it in GitHub Desktop.
Save completejavascript/46a6f31d1848152943ec9399333e4bc0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
#define ull unsigned long long
/* sum term id x
* 2 1 1 1
* 3 2 3 2 2
* 4 4 5 6 3 4
* 5 7 8 9 4 7
*
* 1,2,4,7,...=> x1 = 1, x_n = 1 + n(n-1)/2;
* we have term = a => 1 + n(n-1)/2 = a => n = (1 + sqrt(8a-7))/2
*/
int main()
{
//freopen("input.txt","r",stdin);
ull t = 0;
cin >> t;
for(int tc = 0; tc < t; tc++)
{
ull a = 0;
cin >> a;
ull id = (1 + (ull) sqrt(8*a-7))/2;
ull sum = id + 1;
ull start = 1 + (id * (id-1))/ 2;
ull delta = (a - start);
ull y = 1;
ull x = sum - y;
x -= delta;
y += delta;
if(sum%2 == 0)
{
// form: x/y
cout << "TERM " << a << " IS " << x << "/" << y << endl;
}
else
{
// form: y/x
cout << "TERM " << a << " IS " << y << "/" << x << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment