C. Constructing Tests
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Example
input

Copy
3
21
0
1
output
5 2
1 1
-1

题目大意:一个n*n的01矩阵,要求每个m*m的矩阵都至少要有一个0,求最大的1的数量. 现在给定1的数量,构造出一组n,m满足要求.

分析:先想一想怎么让1的数量最多?肯定是让一个0在尽可能多的m*m的矩阵的重叠区域里.对于一个n*n的矩阵,每一行最少放n/m个0就能满足要求,每一列最少放n/m个0也能满足要求,那么1的最多数量就是n^2 - (n/m)^2 = x.现在问题就变成一个解方程了.

   这个方式是可以利用平方差公式化简的:(n + (n/ m))(n - (n / m)) = x,x可以表示成两个乘积的形式,那么可以把x在根号时间内分解成两个数相乘的形式.并由此解出n和n/m.m也能解出来. 最后判断一下n是否≥m以及n/m是不是我们要求的那个值即可.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll x,t,n,m; int main()
{
cin >> t;
while (t--)
{
bool flag = false;
cin >> x;
if (x == )
cout << << " " << << endl;
else
if (x == )
cout << - << endl;
else
{
for (ll i = ; i * i <= x; i++)
{
if ((x % i == ) && ((i & ) == ((x / i) & )))
{
n = (i + x / i) / ;
ll temp = n - i;
if (temp == )
continue;
m = n / temp;
if(n >= m && temp == (n / m))
{
cout << n << " " << m << endl;
flag = ;
break;
}
}
}
if (!flag)
cout << - << endl;
}
} return ;
}

Codeforces 938.C Constructing Tests的更多相关文章

  1. Codeforces 938C - Constructing Tests

    传送门:http://codeforces.com/contest/938/problem/C 给定两个正整数n,m(m≤n),对于一个n阶0-1方阵,其任意m阶子方阵中至少有一个元素“0”,则可以求 ...

  2. Constructing Tests CodeForces - 938C

    大意: 定义m-free矩阵: 所有$m*m$的子矩阵至少有一个$0$的$01$矩阵. 定义一个函数$f(n,m)=n*n$的m-free矩阵最大$1$的个数. 给出$t$个询问, 每个询问给出$x$ ...

  3. Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

    题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...

  4. Codeforces 938.D Buy a Ticket

    D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. Codeforces 938.B Run For Your Prize

    B. Run For Your Prize time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. Codeforces 938.A Word Correction

    A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. CodeForces - 1003-B-Binary String Constructing (规律+模拟)

    You are given three integers aa, bb and xx. Your task is to construct a binary string ssof length n= ...

  8. Codeforces 938 正方形方格最多0/1 足球赛dijkstra建图

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  9. Educational Codeforces Round 38 (Rated for Div. 2) C

    C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. spring-boot 项目整合logback

    使用spring-boot项目中添加日志输出,java的日志输出一共有两个大的方案log4j/log4j2 ,logback.log4j2算是对log4j的一个升级版本. 常规做法是引入slf4j作为 ...

  2. 【ZABBIX】ZABBIX3.2升级3.4

    小贴士 1.停止zabbix服务 service zabbix_server stop service zabbix_agentd stop /usr/local/zabbix/sbin/zabbix ...

  3. Python最简编码规范

    前言 本文是阅读<Python Coding Rule>之后总结的最为精华及简单的编码规范,根据每个人不同喜好有些地方会有不同的选择,我只是做了对自己来说最简单易行的选择,仅供大家参考. ...

  4. Python中import的as语法

    在Python中,如果import的语句比较长,导致后续引用不方便,可以使用as语法,比如: import dir1.dir2.mod # 那么,后续对mod的引用,都必须是dir1.dir2.mod ...

  5. struts2--文件上传大小

    Struts2文件上传的大小限制问题 问题:上传大文件报错-- 解决:修改struts.xml文件中的参数如下 <constant name="struts.multipart.max ...

  6. java读取xls和xlsx数据作为数据驱动来用

    java读取Excle代码 拿来可以直接使用 :针对xls 和 xlsx package dataProvider; import java.io.File; import java.io.FileI ...

  7. 第76天:jQuery中的宽高

    Window对象和document对象的区别 1.window对象表示浏览器中打开的窗口 2.window对象可以省略,比如alert()也可以写成window.alert() Document对象是 ...

  8. 【.Net】vs2017 自带发布工具 ClickOnce发布包遇到的问题

    一.遇到的问题 在安装了vs2017 社区版(Community)之后  想打包安装程序(winform) 还是想用之前的 installshield来打包  发现居然打不了,在官网查了    ins ...

  9. ICPCCamp 2017 I Coprime Queries

    给出一个长度为\(n\)的正整数序列\(a\),\(m\)次询问\(l,r,x\),问\(max\{i|i\in[l,r],gcd(a_i,x)=1\}\). \(n,m,a_i\le 10^5\). ...

  10. BZOJ 1566 管道取珠(DP)

    求方案数的平方之和.这个看起来很难解决.如果转化为求方案数的有序对的个数.那么就相当于求A和B同时取,最后序列一样的种数. 令dp[i][j][k]表示A在上管道取了i个,下管道取了j个,B在上管道取 ...