传送门:http://codeforces.com/contest/938/problem/C

给定两个正整数n,m(m≤n),对于一个n阶0-1方阵,其任意m阶子方阵中至少有一个元素“0”,则可以求解这个方阵中的“1”的最大数目。现求解这个问题的逆向问题:已知这个最大数目为X,求相应的nm

由原问题可以得到方程:$n^2-\left\lfloor\frac{n}{m}\right\rfloor^2=X\cdots(1)$,于是,对于给定的X,求解不定方程(1)。

令$k=\left\lfloor\frac{n}{m}\right\rfloor$,则$\left\lfloor\frac{n}{k+1}\right\rfloor<\left\lfloor\frac{n}{k}\right\rfloor$时,$\left\lfloor\frac{n}{m}\right\rfloor=k\Rightarrow k\le\frac{n}{m}<k+1\Rightarrow \frac{n}{k+1}<m\le\frac{n}{k}\Rightarrow m=\left\lfloor\frac{n}{k}\right\rfloor$;于是,原方程化为$n^2-k^2=X\Rightarrow (n+k)(n-k)=X\cdots(2)$。

①若X=0,显然n=k,其中的一组解为n=1,m=1;

②若X≠0,则将X分解,设X=a·b(b<a)。

则解不定方程:$u^2-v^2=ab\Rightarrow u=\frac{a+b}{2},v=\frac{a-b}{2}$。

于是,方程(2)可能有解n=u,k=v

于是,当uv均为正整数,且$\left\lfloor\frac{u}{v+1}\right\rfloor<\left\lfloor\frac{u}{v}\right\rfloor$时,方程(1)有解:$n=u,m=\left\lfloor\frac{u}{v}\right\rfloor$。

参考程序如下:

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while (t--) {
int x;
int n = -, m = -;
scanf("%d", &x);
if (x == ) {
n = ;
m = ;
}
else {
for (int i = ; i * i < x; i++) {
int j = x / i;
if (x == i * j && !((i ^ j) & )) {
int u = (j + i) / ;
int v = (j - i) / ;
if (u / v - u / (v + ) > ) {
n = u;
m = u / v;
break;
}
}
}
}
if (n == -) printf("-1\n");
else printf("%d %d\n", n, m);
}
return ;
}

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

  1. Codeforces 938.C Constructing Tests

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

  2. Constructing Tests CodeForces - 938C

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

  3. 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 ...

  4. Educational Codeforces Round 38 (Rated for Div. 2)

    这场打了小号 A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  6. Tree Constructing CodeForces - 1003E(构造)

    题意: 就是让构造一个直径为d的树  每个结点的度数不能超过k 解析: 先构造出一条直径为d的树枝 然后去遍历这条树枝上的每个点  为每个点在不超过度数和直径的条件下添加子嗣即可 #include & ...

  7. 【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration

    题意:有一堆数据,某些是样例数据(假设X个),某些是大数据(假设Y个),但这些数据文件的命名非常混乱.要你给它们一个一个地重命名,保证任意时刻没有重名文件的前提之下,使得样例数据命名为1~X,大数据命 ...

  8. 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= ...

  9. Codeforces Round #642 (Div. 3) D. Constructing the Array (优先队列)

    题意:有一个长度为\(n\)元素均为\(0\)的序列,进行\(n\)次操作构造出一个新序列\(a\):每次选择最长的连续为\(0\)的区间\([l,r]\),使得第\(i\)次操作时,\(a[\fra ...

随机推荐

  1. Oracle强杀进程

      1.找到sid,serial#: SELECT /*+ rule */ s.username, l.type, decode(l.type,'TM','TABLE LOCK',           ...

  2. gitlab gerrit jenkins CI/CD环境集成

    http://blog.csdn.net/williamwanglei/article/details/38498465

  3. Shell脚本下条件测试(eq.ne.....)(转载)

    转载:http://cxj632840815.blog.51cto.com/3511863/1168709 Shell编程中的条件测试 在Linux编程中经常会用到判断数值的大小,字符串是否为空这样或 ...

  4. NestedPreb

    屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...

  5. SP1557 GSS2 - Can you answer these queries II(线段树)

    传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...

  6. Codefoces 828C

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Jsp四个作用域page、request、session和application的区别

    1.简单说 page指当前页面.在一个jsp页面里有效  2.request 指从http请求到服务器处理结束,返回响应的整个过程.在这个过程中使用forward方式跳转多个jsp.在这些页面里你都可 ...

  8. EditText(7)EditText输入事件监听

    EditText.addTextChangedListener(TextWatcher watcher); void initSearch(){ search = (EditText) findVie ...

  9. c语言数据读入---sscanf、fscanf

    #include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> ...

  10. C. Coin Troubles 有依赖的背包 + 完全背包变形

    http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...