Educational Codeforces Round 60 (Rated for Div. 2) 1117

C. Magic Ship

思路

  • 二分答案,看是否能在 m i d mid mid 时间内走完全程,能走完就更新 a n s ans ans,不能就接着二分,对于不可能有解的样例,是不可能有更新的
  • 就离谱,想到二分了,还是模拟做,离谱
  • d x [ i ] dx[i] dx[i] 表示的就是在这个时间点上,我在 x x x 上走的距离,每次都要更新,同理 d y [ i ] dy[i] dy[i]
AC(two point)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
char ch[N];
int x1, y1, x2, y2, n;
int dx[N], dy[N];
bool check(LL mid) {
	LL t = mid / n;
	LL sub = mid % n;
	LL x = dx[n] * t + dx[sub] + x1;
	LL y = dy[n] * t + dy[sub] + y1;
	if (abs(x2 - x) + abs(y2 - y) <= mid) return 1;
	else return 0;
}
int main() {
	scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
	scanf("%d", &n);
	scanf("%s", ch + 1);
	for (int i = 1; i <= n; i ++ ) {
		dx[i] = dx[i - 1];
		dy[i] = dy[i - 1];
		if (ch[i] == 'U') dy[i] ++;
		else if (ch[i] == 'D') dy[i] --;
		else if (ch[i] == 'L') dx[i] --;
		else dx[i] ++;
	}
	LL l = 0, r = 1e18, ans = -1;
	while (l < r) {
		LL mid = (l + r) / 2;
		if (check(mid)) {
			r = mid;
			ans = mid;
		} else l = mid + 1;
	}
	printf("%lld", ans);
	return 0;
}

D. Magic Gems

思路

AC(dp + qmi + matrices)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 110;
LL n, m;
struct mat{
	LL a[N][N];
	mat operator* (mat const &b) const {
		mat res;
		memset(res.a, 0, sizeof res.a);
		for (int i = 0; i < m; i ++ ) 
			for (int j = 0; j < m; j ++ )
				for (int k = 0; k < m; k ++ ) 
				res.a[i][j] = (res.a[i][j] + a[i][k] * b.a[k][j]) % mod;
	return res;
	}
};
void work(LL t) {
	mat c, res;
	memset(c.a, 0, sizeof c.a);
	memset(res.a, 0, sizeof res.a);
	c.a[0][0] = c.a[0][m - 1] = 1;
	for (int i = 1; i < m; i ++ ) c.a[i][i - 1] = 1;
	for (int i = 0; i < m; i ++ ) res.a[0][i] = 1;
	while (t) {
		if (t & 1) res = res * c;
		c = c * c;
		t >>= 1;
	}
	printf("%lld\n", res.a[0][0]);
}
int main() {
	scanf("%lld%lld", &n, &m);
	if (m > n) printf("1\n");
	else work(n - m + 1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值