938C - Constructing Tests - CodeForces Solution


binary search brute force constructive algorithms *1700

Please click on ads to support us..

C++ Code:

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

#define PI acos(-1.0)
typedef long long ll; 

const int maxx = 1e9;

int solve(ll n,ll x){
	if(n*n <= x) return -1;
	ll i,j,k1,k2,low,high,mid;
	i = n*n;
	low = 2,high = n;
	while(low<=high){
		mid = (low+high)/2;
		j = n/mid;
		k1 = i - j*j;
		//k2 = i - (mid*mid);
		if(k1 == x) return mid;
		//else if(k2 == x) return j;
		else if(k1 < x){
			low = mid+1;
		}
		else if(k1 > x){
			high = mid-1;
		}
	}
	return -1;
}

int main(){
	ll t,i,j,k,x,n,m;
	cin >> t;
	while(t--){
		scanf("%lld",&x);
		if(x == 0){
			n = m = 1;
		}
		else{
			for(i=sqrtl(x);i<1e5;i++){
				if((i*i - (i/2)*(i/2)) > maxx){
					n = m = -1;
					break;
				}
				m = solve(i,x);
				if(m != -1){
					n = i;
					break;
				}
			}
		}
		if(n == -1){
			printf("-1\n");
		}
		else{
			printf("%lld %lld\n",n,m);
		}
	}
return 0;		
}


Comments

Submit
0 Comments
More Questions

1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume