2020沈阳ICPC H(背包+双指针) I(同余方程结论)

9 篇文章 0 订阅
6 篇文章 0 订阅

H.

 思路:

用dp解决这道题.我们以天数作为阶段,显然这是无后效性的,然后根据数据范围题意设计f[i]为处理前i次借车花费的最少的费用.然后这就是背包了.这里转移的时候我最开始是想到二分,但是会T.于是观察到转移有单调性,然后就用双指针优化dp了.

代码:

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
#define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 1e6 + 10;
int n, m, r, d[N], k[N], c[N], f[N], pre[N], a[N], cnt;
signed main() {
	qio >> n >> m >> r;
	for (int i = 1; i <= n; ++i) qio >> d[i] >> k[i] >> c[i];
	for (int i = 1; i <= m; ++i) {
		int p, q;
		qio >> p >> q;
		for (int j = 1; j <= q; ++j)
			a[++cnt] = p;
	}
	sort(a + 1, a + 1 + cnt);
	//f[i]表示处理前i次借车需要的最小的价格
	mem(f, 0x3f);
	f[0] = 0;
	for (int i = 1; i <= cnt; ++i) {
		f[i] = min(f[i], f[i - 1] + r);
		for (int j = 1; j <= n; ++j) {
			while (a[i] - d[j] >= a[pre[j] + 1]) ++pre[j];
			int x = max(pre[j], i - k[j]);
			f[i] = min(f[i], f[x] + c[j]);
		}
	}
	qio << f[cnt] << "\n";
}

I.

思路:

解方程可以得到HM - A\leq t(H - 1) \leq A(mod\ HM).

引理1:

a\equiv b(mod\ m), c | a, c|b\Rightarrow d =gcd(m, c), \frac{a}{d}\equiv \frac{b}{a}(mod\ \frac{m}{d}),反之亦然.正确性显然,读者可自证.

引理2:

若a与n互质,那么xax\equiv b(mod\ n)x\in [0, n-1]内有且仅有唯一的解.

证明:

使用反证法.假设ax_i\equiv ax_j(mod\ n), x_i, x_j\in [0, n-1],x_i \neq x_j,那么由于a,n互质,x_i\equiv x_j(mod\ n),此时x_i=x_j,显然错误,所以至多有一个解,此时我们把方程变换成ax-ny=b,由裴蜀定理,当且仅当b|gcd(a, n)时x,y有解,a,n互质,这个同余方程很显然有无穷多组解.证毕!

由引理1和引理2:

令d = gcd(H - 1, HM), 显然,\frac{t(H - 1)}{d} \leq \frac{A}{d}(mod\ \frac{HM}{d})不等号右边分别取为[0, A/d],共有A/d+1个解,\frac{t(H - 1)}{d} \geq \frac{HM - A}{d}(mod\ \frac{HM}{d}),不等号右边分别取[HM/d, (HM-A)/d],共有A/d个解.由于我们最开始除以d,所以我们要再乘以d,这就相当于把整个剩余系扩大了d倍,解的数量也会扩大d倍.所以最后的解是d * (A / d * 2 + 1).特别的,当A = HM / 2时,有HM个解.

代码:

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
#define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
int H, M, A;
signed main() {
	qio >> H >> M >> A;
	if (1.0 * A == 1.0 * H * M / 2) return qio << H * M << "\n", 0;
	int d = __gcd(H - 1, H * M);
	qio << d * (A / d * 2 + 1) << "\n";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值