Codeforces 938.C Constructing Tests
1 second
256 megabytes
standard input
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.
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.
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.
3
21
0
1
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的更多相关文章
- Codeforces 938C - Constructing Tests
传送门:http://codeforces.com/contest/938/problem/C 给定两个正整数n,m(m≤n),对于一个n阶0-1方阵,其任意m阶子方阵中至少有一个元素“0”,则可以求 ...
- Constructing Tests CodeForces - 938C
大意: 定义m-free矩阵: 所有$m*m$的子矩阵至少有一个$0$的$01$矩阵. 定义一个函数$f(n,m)=n*n$的m-free矩阵最大$1$的个数. 给出$t$个询问, 每个询问给出$x$ ...
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- 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 ...
- 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 ...
- Codeforces 938.A Word Correction
A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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= ...
- 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 ...
- 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 ...
随机推荐
- ORA-28000: the account is locked 查哪个具体ip地址造成
查系统默认的策略,连续验证10次错误帐户即会被锁 SQL> select resource_name, limit from dba_profiles where profile='DEFAUL ...
- ssd a
Alpha版本测试报告 (1)测试计划 测试人员 工作安排 瞿煌人 制定测试计划,撰写测试报告 周建峰 执行测试,撰写测试报告 注:测试结果Y表示通过测试,N表示未通过测试. 功能 描述 效果 测试结 ...
- HDU 5207 Greatest Greatest Common Divisor
题目链接: hdu:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=153598 bc(中文):http://bestco ...
- .NET Core使用EF分页查询数据报错:OFFSET语法错误问题
在Asp.Net Core MVC项目中使用EF分页查询数据时遇到一个比较麻烦的问题,系统会报如下错误: 分页查询代码: ) * condition.PageSize).Take(condition. ...
- jsonFormater之应用
html代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- 让VS2013支持 C# 6.0 语法
还未升级使用VS2015前,又想尝试使用C# 6.0的语言特性,可以用以下方法启用: VS2013中“工具”下选择“程序包管理器控制台”: 选中需要使用C# 6.0的项目,再敲入"Insta ...
- PHP简单模拟登录功能实例分享
1.curl实现模拟登录的代码,(只是实现服务器与服务器建立会话,其实并没有在客户端与服务器之间建立会话) <?php $cookie_jar = tempnam('./tmp','cookie ...
- Matlab里面.M文件不能运行,预期的图像也显示不出来的一个原因
matlab中function函数的函数名与保存的文件名需要一样: 函数名是GAconstrain,文件名保存成GAconstrain.m,不要使用复制时候产生副本GAconstrain(1).m.
- scss在ide的命令参数
%FileName% ../css/%FileBaseName%.css --sourcemap=none –style expanded
- Viewpoint Meta标签
移动web Viewpoint常用得设置方式: [布局viewpoint] = [设备宽度] = [度量viewpoint] <meta name="viewport" co ...