A. Exciting Bets
思维题
给定两个数a, b 通过进行若干个二者都加一或者减一操作来使得a和b的gcd最大
我们可以知道 max_gcd肯定是 a+k, b+k 的 gcd 又a+k = t1 * max_gcd b + k = t2 * max_gcd
所以我们可以得到 b - a = t3 * max_gcd 当t3取1时 max_gcd最大
所以我们就得到了a, b操作后最大公因数 再去看加或减哪个操作数比较少即可
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
return b ? gcd(b, a % b) : a;
}
ll a, b;
void solve()
{
cin >> a >> b;
if (a > b) swap(a, b);
ll tmp = b - a;
if (a == b) {puts("0 0"); return;}
else printf("%lld %lld\n", tmp, min(a % tmp, tmp - (a % tmp)));
}
int main()
{
int t;
scanf("%d", &t);
while (t -- )
{
solve();
}
return 0;
}
B. Customising the Track
水题 把所有数加起来平均一下 不能平均的尽量平均 最后求值即可
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n;
ll a[N];
void solve()
{
scanf("%lld", &n); ll sum = 0;
for (int i = 1; i <= n; i ++ ) {scanf("%lld", &a[i]); sum += a[i];}
sum %= n;
ll ans = sum * (n - sum);
printf("%lld\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while (t -- )
{
solve();
}
return 0;
}
C - Need for Pink Slips
阅读题 题意为给定一个c, m, p 每次可以从中选择一个来操作 直到选择到p 问最终轮数的期望值为多少
每次进行操作 都要将它的概率和波动值v比较 比较后有两种操作
数据很小 暴力搜索即可
代码
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-12;
double s[10], v, ans;
void dfs(double val, int tmp)
{
if (val < eps || val * s[3] < eps) return;
else ans += val * s[3] * tmp;
double a = s[1], b = s[2], c = s[3];
if (s[1] > 0)
{
double res = val * a;
if (a <= v)
{
s[1] = 0;
if (s[2] < eps) s[3] += a;
else s[2] += a / 2, s[3] += a / 2;
dfs(res, tmp + 1);
}
else
{
s[1] = a - v;
if (s[2] < eps) s[3] += v;
else s[2] += v / 2, s[3] += v / 2;
dfs(res, tmp + 1);
}
s[1] = a, s[2] = b, s[3] = c;
}
if (b > 0)
{
double res = val * b;
if (b <= v)
{
s[2] = 0;
if (s[1] < eps) s[3] = b + s[3];
else s[1] = s[1] + b / 2, s[3] = s[3] + b / 2;
dfs(val * b, tmp + 1);
}
else
{
s[2] = b - v;
if (s[1] < eps) s[3] += v;
else s[1] += v / 2, s[3] += v / 2;
dfs(val * b, tmp + 1);
}
s[1] = a, s[2] = b, s[3] = c;
}
}
void solve()
{
ans = 0;
for (int i = 1; i <= 3; i ++ ) cin >> s[i];
cin >> v;
dfs(1, 1);
printf("%.12lf\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while (t -- )
{
solve();
}
return 0;
}