传送门: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. Same Tree 序列化二叉树

    https://oj.leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if the ...

  2. Python基础 — Pandas

    Pandas -- 简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.        Pandas ...

  3. [转]C语言文件操作函数大全(超详细)

    fopen(打开文件)相关函数 open,fclose表头文件 #include<stdio.h>定义函数 FILE * fopen(const char * path,const cha ...

  4. Android 性能优化(27)*zipalign让apk数据对齐,运行更快。

    1.zipalign 简介 zipalign is an archive alignment tool that provides important optimization to Android ...

  5. Android 性能优化(14)网络优化( 10)Determining and Monitoring the Connectivity Status

    Determining and Monitoring the Connectivity Status This lesson teaches you to Determine if you Have ...

  6. 【SpringMVC框架】非注解的处理器映射器和适配器

    参考来源:     http://blog.csdn.net/acmman/article/details/46968939 处理器映射器就是根据URL来找Handler,处理器适配器就是按照它要求的 ...

  7. canvas学写一个字

    第一步:画一个米字格,先画一个矩形,再画中间的米字. <script> window.onload = function(){ var canvas = document.getEleme ...

  8. 专题七:UDP编程补充——UDP广播程序的实现

    一.程序实现 UDP广播程序的实现代码: using System; using System.Net; using System.Net.Sockets; using System.Text; us ...

  9. Intellij使用心得(四) -- 导入Eclipse的代码格式化文件

    https://my.oschina.net/flashsword/blog/137598

  10. LN : JSON (利用C++实现JSON)

    Appreciation to our TA, 王毅峰, who designed this task. 问题描述 JSON, JavaScript Object Notation,is an fle ...