A | B | C | D | E | |||||
AC | AC |
D. Strange Fractions
数学:转一元一次方程+有理数求解 & 计算机:乘除运算特性
![](https://i-blog.csdnimg.cn/blog_migrate/403eda12d7b784a69494134f8cfab8da.png)
//#include <bits/stdc++.h>
#include<iostream>
#include <cmath>
using namespace std;
typedef long long ll;
void solve(){
ll p, q;
cin >> p >> q;
ll x = p*p - 4*q*q;
ll y = pow(x, 0.5);
if (y * y == x) {//转一元一次方程有意义:求解有理数
ll a, b = 2*q;
if (p >= y) a = p - y;
else a = p + y;
cout << a << " " << b << endl;
return;
}
cout << 0 << " " << 0 << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
E. Strange Integers
贪心+排序
![](https://i-blog.csdnimg.cn/blog_migrate/c09cd828fcfd69a27f9b16b91456e8f8.png)
#include<iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
void solve(){
ll n, k;
cin >> n >> k;
ll a[N];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll cnt = 1;
for (int i = 1, x = a[0]; i < n; i++) {
if (a[i] - x >= k) {//贪心+排序(已经排序,不用担心)
cnt++;
x = a[i];
}
}
cout << cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}