首页 > 其他分享 >AtCoder Regular Contest 123 E Training

AtCoder Regular Contest 123 E Training

时间:2023-04-28 17:47:29浏览次数:49  
标签:AtCoder return limits sum Regular long mid 123 ll

洛谷传送门

AtCoder 传送门

不妨假设 \(B_X \le B_Y\)。设 \(f(x) = A_X + \frac{x}{B_X}, g(x) = A_Y + \frac{x}{B_Y}, F(x) = \left\lfloor{f(x)}\right\rfloor, G(x) = \left\lfloor{g(x)}\right\rfloor\),题目即求 \(\sum\limits_{x=1}^n [F(x) = G(x)]\)。

我一开始尝试化简式子,然后发现不能合并两个下取整的分数。

然后尝试二分 \(F(x) - G(x) = 0\) 的区间,很快也发现不行,因为 \(F(x) - G(x)\) 不单调,有可能出现 \(0,1,0,1\) 反复横跳的情况。

其实与其考虑 \(F(x) - G(x)\),不如先考虑 \(f(x) - g(x)\),它是斜率 \(\ge 0\) 的直线。我们发现:

  • \(f(x) - g(x) \in (-1,0], F(x) - G(x) \in \{-1,0\}\);
  • \(f(x) - g(x) \in (0,1], F(x) - G(x) \in \{0,1\}\)。

而满足 \(f(x) - g(x) \in (-1,0]\) 和 \(f(x) - g(x) \in (0,1]\) 的区间可以二分得到。接下来以第一种情况为例(第二种情况是对称的),考虑如何计算。

现在问题仍然是求 \(\sum\limits_{x=l}^r [F(x) - G(x) = 0]\),但是 \(F(x) - G(x) \in \{-1,0\}\)。考虑计算 \(\sum\limits_{x=l}^r F(x) - G(x)\),设结果为 \(k\),发现 \(\sum\limits_{x=l}^r [F(x) - G(x) = -1] = -k\),这样 \(\sum\limits_{x=l}^r [F(x) - G(x) = 0] = r - l + 1 + k\),就把区间里求多少数的值等于某个数的问题转化成了区间求和

接下来问题变成了求 \(\sum\limits_{x=l}^r F(x) - G(x) = (\sum\limits_{x=l}^r A_X + \left\lfloor\frac{i}{B_X}\right\rfloor) - (\sum\limits_{x=l}^r A_Y + \left\lfloor\frac{i}{B_Y}\right\rfloor)\)。这个就爱怎么做怎么做了,虽然你闲得蛋疼也可以类欧(

code
// Problem: E - Training
// Contest: AtCoder - AtCoder Regular Contest 123
// URL: https://atcoder.jp/contests/arc123/tasks/arc123_e
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

ll dfs(ll a, ll b, ll c, ll n) {
	if (!a || !n) {
		return (n + 1) * (b / c);
	}
	if (n < 0) {
		return 0;
	}
	if (a >= c || b >= c) {
		return dfs(a % c, b % c, c, n) + (a / c) * (n * (n + 1) / 2) + (b / c) * (n + 1);
	}
	ll m = (a * n + b) / c;
	return m * n - dfs(c, c - b - 1, a, m - 1);
}

inline ll calc(ll a, ll b, ll c, ll l, ll r) {
	return dfs(a, b, c, r) - dfs(a, b, c, l - 1);
}

void solve() {
	ll n, a, b, c, d;
	scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d);
	if (b > d) {
		swap(a, c);
		swap(b, d);
	}
	ll l = 1, r = n, p1 = -1, p2 = n + 1, p3 = -1;
	while (l <= r) {
		ll mid = (l + r) >> 1;
		if (mid * (d - b) > b * d * (c - a - 1)) {
			p1 = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	if (p1 == -1) {
		puts("0");
		return;
	}
	l = p1;
	r = n;
	while (l <= r) {
		ll mid = (l + r) >> 1;
		if (mid * (d - b) > b * d * (c - a)) {
			p2 = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	l = p2;
	r = n;
	while (l <= r) {
		ll mid = (l + r) >> 1;
		if (mid * (d - b) <= b * d * (c - a + 1)) {
			p3 = mid;
			l = mid + 1;
		} else {
			r = mid - 1;
		}
	}
	if (p3 == -1) {
		p3 = p2 - 1;
	}
	ll ans = 0, k = calc(1, c * d, d, p1, p2 - 1) - calc(1, a * b, b, p1, p2 - 1);
	ans += p2 - p1 - k;
	k = calc(1, a * b, b, p2, p3) - calc(1, c * d, d, p2, p3);
	ans += p3 - p2 + 1 - k;
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:AtCoder,return,limits,sum,Regular,long,mid,123,ll
From: https://www.cnblogs.com/zltzlt-blog/p/17362798.html

相关文章

  • AtCoder Regular Contest 126 E Infinite Operations
    洛谷传送门AtCoder传送门算是对这篇博客的补充吧。设\(a_1\lea_2\le\cdots\lea_n\)。发现最优操作中一定是对相邻的数进行操作,因为如果\(a_j\)想把\(x\)给\(a_i\)(\(i<j\)),最优是依次操作\((j-1,j,x),(j-2,j-1,x),...,(i,i+1,x)\)。这样\(x\)就能造成\((j-i)......
  • 【AtCoder】Forbidden Pattern
    题目链接分析首先考虑哪些串能被删空。下面只考虑长度为偶数的串。考虑这样一个(错误的)算法:从左往右依次加入串中的字符,然后能删则删。这个算法对于结尾为A的串一定能删空。对称地,开头为B的串也一定能被删空。现在只需要考虑开头为A结尾为B的串。如果它能被删空,则一定存......
  • 解决MySQL数据库同步1236错误
    转载于:https://www.cnblogs.com/dukuan/p/8744295.html1、报错如下:Gotfatalerror1236frommasterwhenreadingdatafrombinarylog:'TheslaveisconnectingusingCHANGEMASTERTOMASTER_AUTO_POSITION=1,butthemasterhaspurgedbinarylogscontaining......
  • AtCoder Regular Contest 112 F Die Siedler
    洛谷传送门AtCoder传送门感觉太人类智慧了。设\(A=(c_1,c_2,...,c_n)\)表示当前每种牌的数量,\(f(A)\)为状态\(A\)只进行换牌操作最终最少剩下几张牌。\(f(A)\)是可以贪心求出的,因为策略必然是能换则换。并且我们发现依次换\(2,3,...,n,1\),最后\(c_2\simc_n\)都......
  • Linux shell regular expression All In One
    LinuxshellregularexpressionAllInOneLinuxshell正则表达式demos(......
  • AtCoder Regular Contest 123 C 1, 2, 3 - Decomposition
    洛谷传送门AtCoder传送门从低位往高位考虑。设当前个位为\(k\),暴力搜索这一位向上进\(i\)位,设\(\left\lfloor\frac{n}{10}\right\rfloor-i\)的答案为\(t\)。若\(t>10i+k\)显然就不可行,因为就算个位全部填\(1\)也不能补齐;否则\(n\)的答案就是\(\max(t,\l......
  • AtCoder Regular Contest 120 F Wine Thief
    洛谷传送门AtCoder传送门Hint如果是一个环怎么做?Answer由于是一个环,因此环上每个点对最终答案造成的贡献都相同。设$f_{i,j}$为长度为$i$的序列选$j$个不相邻的点的方案数,则$f_{i,j}=\binom{i-j+1}{j}$。应该很好理解,考虑一个长度为$i-j+1$的链,链头、链尾和两......
  • AtCoder Regular Contest 125 E Snack
    洛谷传送门AtCoder传送门很棒的flow题,考虑建二分图。源点向每种零食连边权为\(a_i\)的边,每种零食向每个孩子连边权为\(b_j\)的边,每个孩子向汇点连边权为\(c_j\)的边,这个图的最大流就是答案。直接跑最大流肯定T,考虑最大流等价于求这个图的最小割,因此转而求最小割。......
  • AtCoder Regular Contest 126 D Pure Straight
    洛谷传送门AtCoder传送门很不错的状压。考虑先把最后作为答案的数聚到一起,再算它们的逆序对个数。设\(f_S\)为当前选的数集合为\(S\)的答案。有转移:选\(a_i\),答案加上之前选的比它大的数;不选\(a_i\),此时需要把左边的数或者右边的数往中间挪一个,答案加上左右两端的最......
  • Converting a regular DB2 DMS tablespace to LARGE
    ConvertingaregularDB2DMStablespacetoLARGEhttps://www.ibm.com/support/pages/converting-regular-db2-dms-tablespace-large#:~:text=Convert%20the%20tablespace%20to%20LARGE%20by,running%3A%20alter%20tablespace%20tbspace_name%20CONVERT%20TO%20......