Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Solution {
- private static int getSquareRoot(long a) {
- long f = 0;
- int ret = 0;
- for(long i = 0; i < 16; i++) {
- ret <<= 1;
- long kari_f = (f + 1) << ((15 - i) * 2);
- if(a >= kari_f) {
- f = f + 2;
- a -= kari_f;
- ret++;
- }
- f <<= 1;
- }
- return ret;
- }
- public static void main(String[] args) {
- int MAX_VALUE = 2 * 1000000;
- int[] roots = new int[MAX_VALUE + 1];
- for(int i = 1; i <= MAX_VALUE; i++) {
- roots[i] = getSquareRoot(i);
- }
- try(Scanner sc = new Scanner(System.in)) {
- int T = sc.nextInt();
- while(T-- > 0) {
- int N = sc.nextInt();
- int root = roots[N];
- int bestRectangles = 0;
- int bestArea = 0;
- for(int x = 1; x <= root + 1; x++) {
- int y = x;
- int rectangles;
- do {
- int area = x * y;
- rectangles = x * (x + 1) * y * (y + 1) / 4;
- int abs1 = bestRectangles - N;
- int abs2 = rectangles - N;
- if(abs1 < 0) {
- abs1 *= -1;
- }
- if(abs2 < 0) {
- abs2 *= -1;
- }
- if(abs1 > abs2) {
- bestRectangles = rectangles;
- bestArea = area;
- }
- if(abs1 == abs2 && bestArea < area) {
- bestArea = area;
- }
- y++;
- } while(rectangles < N);
- if(y == x + 1) {
- break;
- }
- }
- System.out.println(bestArea);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement