CodeForces - 938C Constructing Tests

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37129433/article/details/82391960

Constructing Tests
题 意:给你一个n*n的0,1矩阵,每个m*m的子矩阵都至少要填一个0,现在有q个询问,每次输入n*n的矩阵最多有多少个1.让你输出n m
数据范围:
1<=t<=100
0<=xi<=1e9

输入样例:

3
21
0
1

输出样例:

5 2
1 1
-1

思 路:一个n ,m最多有n*n - (n/m)^2个1然后就解方程,分解x。

#include<bits/stdc++.h>
using namespace std;
int main() {
    int t,x;
    scanf("%d",&t);
    while(t--) {
        int n,m;
        scanf("%d",&x);
        if(x == 1) {
            printf("-1\n");
            continue;
        } else if(x == 0) {
            printf("1 1\n");
            continue;
        }
        bool flag = false;
        for(int i=1; i*i<=x; i++) {
            flag = false;
            if(x%i == 0 && ((x/i)&1) == (i&1)) {
                n = (i+x/i)/2;
                int temp = n-i;
                if(temp == 0)continue;
                m = n/temp;
                if(n>=m && temp == n/m) {
                    printf("%d %d\n",n,m);
                    flag = true;
                    break;
                }
            }
        }
        if(!flag) {
            printf("-1\n");
            continue;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/82391960
今日推荐