[CF938C] Constructing Tests - 构造

时间:2020-04-28
本文章向大家介绍[CF938C] Constructing Tests - 构造,主要包括[CF938C] Constructing Tests - 构造使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Description

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

Solution

设法让每个 \(0\) 被充分地利用

于是 \(n,m,x\) 满足关系

\[n^2-\lfloor\frac n m \rfloor^2=x \]

\(t=\lfloor n/m \rfloor\),则

\[(n+t)(n-t)=x \]

枚举 \(x\) 的所有分解 \(x=ab\),那么 \(x\) 可以分解为平方差当且仅当 \(a,b\) 的奇偶性相同,此时

\[n=\frac{a+b} 2, \ t=\frac{a-b} 2 \]

如果我们找到了这个分解 \(n,t\),则只需要存在一个 \(m\) 使得 \(t=\lfloor n/m \rfloor\) 即可,我们只需要假设 \(m=n/t\) 判断一下是否可行即可

#include <bits/stdc++.h>
using namespace std;

#define int long long
int t,x;

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>x;
        if(x==0) {
            cout<<1<<" "<<1<<endl;
        }
        else if(x==1) {
            cout<<-1<<endl;
        }
        else {
            for(int a=1;a*a<x;a++) {
                int b=x/a;
                if(a*b!=x) continue;
                if((a^b)&1) continue;
                int n=(a+b)/2,t=(b-a)/2;
                int m=n/t;
                if(t==n/m) {
                    cout<<n<<" "<<m<<endl;
                    goto ok;
                }
            }
            cout<<-1<<endl;
            ok:cout<<"";
        }
    }
}

原文地址:https://www.cnblogs.com/mollnn/p/12795400.html