Codeforces 938C

题面

题目链接:传送门
Let’s denote a m-free matrix as a binary (that is, consisting of only 1’s and 0’s) matrix such that every square submatrix of size m × m of this matrix contains at least one zero.

Consider the following problem:

You are given two integers n and m. You have to construct an m-free square matrix of size n × n such that the number of 1’s in this matrix is maximum possible. Print the maximum possible number of 1’s in such matrix.

You don’t have to solve this problem. Instead, you have to construct a few tests for it.

You will be given t numbers x1, x2, …, xt. For every , find two integers ni and mi (ni ≥ mi) such that the answer for the aforementioned problem is exactly xi if we set n = ni and m = mi.

Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of tests you have to construct.

Then t lines follow, i-th line containing one integer xi (0 ≤ xi ≤ 109).

Note that in hacks you have to set t = 1.

Output
For each test you have to construct, output two positive numbers ni and mi (1 ≤ mi ≤ ni ≤ 109) such that the maximum number of 1’s in a mi-free ni × ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer  - 1.

题意:构造一个n×n的矩阵,只有1和0,要求每个大小为m×m的子矩阵中,至少有一个0,且1最多x个。求出合适的n和m。

思路

首先,看数据范围,确定算法的时间复杂度:x的上界是 109 10 9 ,最多有100组数据,故解题方法的时间复杂度要低于O(x)。
如果采用O( logx l o g x )的方法,此题大致可以确定有两种可能:二分和数据结构。
数据结构在此处基本无用武之地,故讨论是否用二分。
如果二分则需要找到一个有序的序列,而此处包含两个变量,二分显得有些无力,故可以放弃这种方法。
O(1)的方法似乎不太可能,故考虑O( x x )的方法。
分析题意,易得0的数量最少 n2(nm)2 n 2 − ( n m ) 2 个。
n2(nm)2=x n 2 − ( n m ) 2 = x ,即 (n+nm)(nnm)=x ( n + n m ) ( n − n m ) = x
此处可把x分解为两个数相乘(枚举较小数,范围是 [1,x] [ 1 , x ] ),然后解出n和m。
设较大数为i,则

{n+nm=in+nm=xi(193) (193) { n + n m = i n + n m = x i

解一下方程,可得
n=i2+x2im=i2+xi2x(194) (194) { n = i 2 + x 2 i m = i 2 + x i 2 − x

故枚举较小数,判断能否被x整除,再算出最大数,判断等式是否成立即可。
P.s.注意判断 i2x i 2 − x 是否为0。

代码

/*我相信暴力出奇迹*/
#include<stdio.h>
#include<cstring>
#include<math.h>
using namespace std;
int read() {
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9') {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') {
        x=10*x+ch-'0';
        ch=getchar();
    }
    return x*f;
}
const int Size=100001;
long long n,m,x;
void create() {         //构造满足条件的n和m 
    if(!x) {            //特判 
        n=m=1;
    } else if(x==1) {
        n=m=-1;
    } else {
        n=m=-1;         //初始值设为-1(表示无解)
        long long maxn=sqrt(x);
        for(int k=1; k<=maxn; k++) {    //枚举较小数 
            if(x%k==0) {
                long long i=x/k;        //算出较大数 
                if(i<=maxn) continue;   //i*i-x<=0就跳过 
                n=(i*i+x)/(i<<1);
                m=(i*i+x)/(i*i-x);
                if(m && n>=m && n+n/m==i && n-n/m==k) {
                    return;             //找到n和m就返回
                } else {
                    n=m=-1;             //不满足条件就均设为无解
                }
            }
        }
    }
}
int main() {
    int t=read();
    while(t--) {
        x=read();
        create();                   //构造n和m
        if(n==-1 && m==-1) {        //若无解则输出-1
            puts("-1");
        } else {
            printf("%I64d %I64d\n",n,m);
        }
    }
    return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值