解方程
正解设,,那么原方程就变成了,那么就成了二元一次方程组来判断有无实根的情况,判别式为,幼儿园数学可知大于等于0有实数根,小于0没有实数根,所以有解情况为,然后再根据求根公式得到,因此一定是个整数,进而可知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; }
选择任意一些数,使得任意两个数之间的差值的绝对值大于等于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; }
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
最新推荐文章于 2023-06-07 23:33:27 发布