The 2021 ICPC Asia Shanghai Regional Programming Contest D、E

Problem - D - Codeforces

解方程

正解设\frac{p}{q}=k,\frac{a}{b}=x,那么原方程就变成了x^{2}-k*x+1=0,那么就成了二元一次方程组来判断有无实根的情况,判别式为\Delta =k^{2}-4,幼儿园数学可知\Delta大于等于0有实数根,小于0没有实数根,所以有解情况为p^{2}\geq 4*q^{2},然后再根据求根公式得到x=\frac{p\pm \sqrt{p^{2}-4*q^{2}}}{2*q},因此\sqrt{p^{2}-4*q^{2}}一定是个整数,进而可知a、b的值,最后把a、b化简

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    rep (oo, 0, T) {
        LL p, q;
        cin >> p >> q;
        if (p * p < 4 * q * q) {
            cout << "0 0\n";
            continue;
        }
        LL tmp = p * p - 4 * q * q;
        LL z = sqrt(tmp);
        if (z * z != tmp) {
            cout << "0 0\n";
            continue;
        }
        function<LL(LL, LL)> gcd = [&](LL x, LL y) {
            return y == 0 ? x : gcd(y, x % y);
        };
        tmp = z;
        LL d = gcd(p + tmp, 2 * q);
        cout << (p + tmp) / d << " " << (2 * q) / d << '\n';
    }

    return 0;
}

E. Strange Integers

选择任意一些数,使得任意两个数之间的差值的绝对值大于等于k,求最多选多找个,直接贪心去选,排一下序从小到大能选即选

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    stack<int> st;
    st.push(a[0]);
    for (int i = 1; i < n; i++) {
        if (a[i] - st.top() >= k) {
            st.push(a[i]);
        }
    }
    cout << st.size() << '\n';

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值