标签:求和 clu bool \n turn [1] continue sha 区间
这道题其实要我们求的就是
\[\lceil 2*n/k \rceil + \lceil 5*n/k \rceil + \lceil 2*n/k \rceil\]
然后就做完了
# include <bits/stdc++.h>
# define ll long long
int main()
{
ll n, k;
scanf("%lld%lld", &n, &k);
ll ans = ((2 * n) / k) + ((5 * n) / k) + ((8 * n) / k) + bool((2 * n) % k != 0) + bool((5 * n) % k != 0) + bool((8 * n) % k != 0);
printf("%lld\n", ans);
return 0;
}
这道题其实求的是区间内偶数和减去奇数和
用等差数列求和公式即可
注意区间长度要\(+1\)
# include <bits/stdc++.h>
# define ll long long
int main()
{
int q;
scanf("%d", &q);
while(q--)
{
ll l, r;
ll ans;
scanf("%I64d%I64d", &l, &r);
if(l == r)
{
printf("%I64d\n", ((l % 2) ? -l : l));
continue;
}
ll x, y;
ll lodd = ((l % 2 == 1) ? l : l + 1), leven = ((l % 2 == 0) ? l : l + 1), rodd = ((r % 2 == 1) ? r : r - 1), reven = ((r % 2 == 0) ? r : r - 1);
x = ((reven - leven) / 2 + 1) * ((reven + leven) / 2);
y = ((rodd - lodd) / 2 + 1) * ((rodd + lodd) / 2);
//printf("%d %d %d %d\n", leven, lodd, reven, rodd);
ans = x - y;
printf("%I64d\n", ans);
}
return 0;
}
这道题要注意的细节超级多,是一道分类讨论好题
其实这道题要求的就是
白色:原白色面积\(+\)矩形\((x1, y1, x2, y2)\)中的黑色面积-矩形\((x3, y3, x4, y4)\)中白色面积-矩形\((x1, y1, x2, y2)\)与矩形\((x3, y3, x4, y4)\)交集中的白色面积(注:本处的黑/白色面积指原矩形中的黑/白面积)
黑色:总面积-白色
好了做完了
(注意:左下角为黑色的矩形中白色的个数为\(\lfloor \frac{n*m}2 \rfloor\),左下角为白色的矩形中白色的个数为\(\lceil \frac{n*m}2 \rceil\))
Code:
#include <bits/stdc++.h>
#define ll long long
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
ll n, m;
ll x[10], y[10];
scanf("%I64d%I64d", &n, &m);
for (int i = 1; i <= 4; i++)
scanf("%I64d%I64d", &x[i], &y[i]), std::swap(x[i], y[i]);
ll w = (n * m + 1) / 2, b = (n * m) - w;
ll c1 = (x[2] - x[1] + 1) * (y[2] - y[1] + 1) - (((x[2] - x[1] + 1) * (y[2] - y[1] + 1) + ((x[1] % 2) == (y[1] % 2))) / 2);
w += c1, b -= c1;
ll c2 = (((x[4] - x[3] + 1) * (y[4] - y[3] + 1) + ((x[3] % 2) == (y[3] % 2))) / 2);
w -= c2, b += c2;
ll c3 = 0;
if (((std::min(x[2], x[4]) >= std::max(x[1], x[3])) && ((std::min(y[2], y[4]) >= std::max(y[1], y[3])))))
c3 = ((std::min(x[2], x[4]) - std::max(x[1], x[3]) + 1) * (std::min(y[2], y[4]) - std::max(y[1], y[3]) + 1)) - ((((std::min(x[2], x[4]) - std::max(x[1], x[3]) + 1) * (std::min(y[2], y[4]) - std::max(y[1], y[3]) + 1))) + ((std::max(x[1], x[3]) % 2) == (std::max(y[1], y[3]) % 2))) / 2;
c3 = std::max(c3, 0ll);
w -= c3, b += c3;
printf("%I64d %I64d\n", w, b);
}
return 0;
}
Codeforces Round #524 (Div.2)题解
标签:求和 clu bool \n turn [1] continue sha 区间
原文地址:https://www.cnblogs.com/little-sun0331/p/10014591.html