首页 > 其他分享 >AtCoder Regular Contest 060

AtCoder Regular Contest 060

时间:2022-10-30 00:33:43浏览次数:103  
标签:AtCoder le 060 int Contest sqrt i64 Regular

F 题有循环节,一看就有 KMP,比较难,太晚了,早上起来再补。

C - Tak and Cards

\(n\) 个整数 \(a_1\sim a_n\),问有多少种选数方案,使得选出来的数平均值为 \(A\)。

\(1\le n,a_i,A\le 50\)

背包即可。时间复杂度 \(\mathcal O(n^4)\)

Code

// Problem: C - Tak and Cards
// Contest: AtCoder - AtCoder Regular Contest 060
// URL: https://atcoder.jp/contests/arc060/tasks/arc060_a
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using i64 = long long;

const int maxn = 55;
const int maxm = 3005;
int n,m,a[maxn];
i64 dp[maxn][maxm];

int main() {
	scanf("%d %d",&n,&m);
	for(int i = 1;i <= n;++ i) {
		scanf("%d",&a[i]);
	}
	dp[0][0] = 1;
	for(int i = 1;i <= n;++ i) {
		for(int j = i;j >= 1;-- j) {
			for(int k = j * 50;k >= a[i];-- k) {
				dp[j][k] += dp[j - 1][k - a[i]];
			}
		}
	}
	i64 ans = 0;
	for(int i = 1;i <= n;++ i)
		ans += dp[i][i * m];
	printf("%lld",ans);
	return 0;
}

D - Digit Sum

定义 \(f(b,n)\):

  • \(f(b,n)=n(n\lt b)\)

  • \(f(b,n)=f(b,\lfloor\frac{n}{b} \rfloor)+n\bmod b(n\ge b)\)

给定 \(n,s\),求出最小的满足 \(f(b,n)=s(b\ge 2)\) 的 \(b\)

\(1\le n,s\le 10^{11}\)

非常简单的数论题。

直接求解显然不可能,我们可以枚举 \(f\) 的取值,当然,并不是暴力。

观察到若 \(b\gt \sqrt{n}\),那么 \(f(b,n)\) 可以直接求出来,考虑根号分治:

  • \(b\le \sqrt{n}\):直接暴力跑 \(f(b,n)\),时间复杂度 \(\mathcal O(\sqrt{n}\log \sqrt{n})\)

  • \(b\gt \sqrt{n}\):此时 \(f(b,n)=\lfloor\frac{n}{b} \rfloor+n\bmod b=\lfloor\frac{n}{b} \rfloor+n-b\times \lfloor\frac{n}{b} \rfloor\),用数论分块求出所有 \(\lfloor\frac{n}{b} \rfloor\) 及其范围 \([l,r]\),代入 \(f(b,n)=s\) 求解,判断解是否在 \([l,r]\) 间即可。时间复杂度 \(\mathcal O(\sqrt{n})\)

综上,时间复杂度为 \(\mathcal O(\sqrt{n}\log \sqrt{n})\)

Code

// Problem: D - Digit Sum
// Contest: AtCoder - AtCoder Regular Contest 060
// URL: https://atcoder.jp/contests/arc060/tasks/arc060_b
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using i64 = long long;

i64 n,s;

i64 f(i64 b,i64 n) {
	if(n < b)return n;
	return f(b , n / b) + (n % b);
}

int main() {
	scanf("%lld %lld",&n,&s);
	if(s > n) {
		puts("-1");
		return 0;
	}
	if(s == n) {
		printf("%lld\n",n + 1);
		return 0;
	}
	for(i64 i = 2;i <= 1e6;++ i) {
		if(f(i , n) == s) {
			printf("%lld\n",i);
			return 0;
		}
	}
	for(i64 l = 1e6 + 1,r = 0;l <= n;l = r + 1) {
		r = n / (n / l);
		i64 g = n / l;
		i64 x = (n - s) / g + 1;
		if(x >= l&&x <= r&&g + n - g * x == s) {
			printf("%lld\n",x);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

E - Tak and Hotels

一条数轴上有 \(n\) 个点,第 \(i\) 个点坐标为 \(a_i\)。

一个人从某个点出发,他一步能迈出的距离不超过 \(L\) 且终点必须为给出的点。

\(Q\) 次询问,每次询问他从 \(a_x\) 走到 \(a_y\) 最少需要多少步。

\(1\le n,Q\le 10^5,1\le a_i,L\le 10^9,1\le a_{i+1}-a_i\le L\)

很老的 trick。

显然,从一个点出发一定要尽可能走得更远,这样肯定不劣。

用双指针求出每个点最远能到的点,倍增预处理,每次回答询问即可。

时间复杂度 \(\mathcal O((n+Q)\log n)\)

Code

有点小坑,要特判 \(x=y\),并且因为我把倍增预处理循环边界打成了 ST 表,狂 WA 了好几发 QAQ

// Problem: E - Tak and Hotels
// Contest: AtCoder - AtCoder Regular Contest 060
// URL: https://atcoder.jp/contests/arc060/tasks/arc060_c
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

const int maxn = 1e5 + 5;
const int maxm = 20;
int n,a[maxn],m,f[maxn][20];

int main() {
	scanf("%d",&n);
	for(int i = 1;i <= n;++ i)scanf("%d",&a[i]);
	scanf("%d",&m);
	
	
	for(int l = n,r = n;l;-- l) {
		for(;r > l&&a[r] - a[l] > m;-- r);
		f[l][0] = r;
	}
	for(int k = 1;k < 20;++ k)
		for(int i = 1;i <= n;++ i)
			f[i][k] = f[f[i][k - 1]][k - 1];
	
	int Q;
	scanf("%d",&Q);
	while(Q --) {
		int x,y,ans = 0;
		scanf("%d %d",&x,&y);
		if(x == y) {
			puts("0");
			continue ;
		}
		if(x > y)std::swap(x , y);
		for(int k = 19;~ k;-- k)
			if(f[x][k] < y)x = f[x][k],ans += 1 << k;
		printf("%d\n",ans + 1);
	}
	return 0;
}

标签:AtCoder,le,060,int,Contest,sqrt,i64,Regular
From: https://www.cnblogs.com/Royaka/p/16840311.html

相关文章

  • 1060 爱丁顿数
    英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” E ,即满足有 E 天骑车超过 E 英里的最大整数 E。据说爱丁顿自己的 E 等于......
  • AtCoder Regular Contest 059
    EducationalDPRound(确信C-BeTogether给定\(n\)个数\(a_{1}\sima_n\),你至多可以对每个\(a_i\)操作一次,以\((a_i-y)^2\)的代价令\(a_i\getsy\),求\(a\)全......
  • Atcoder Grand Contest 003(A~F)
    赛时打了80分钟,后来因为要处理一些私事就没再打,过掉了ABC,推了DE没推出来,不能算很差,但也不算很好。总结一下吧。赛时A一眼题,统计四个方向是否出现过,如果相对的两......
  • AtCoder Beginner Contest 247 E - Max Min
    题目描述简要描述:给定一个长度为\(N\)的数组,求数组的子数组满足最大值为\(X\)且最小值为\(Y\)的子区间的个数。做法1.ST表+二分时间复杂度:\(O(n\logn)\)......
  • 060_索引及文档基本操作
    目录基本Rest命令关于索引的基本操作创建索引及文档创建索引,指定字段类型查询索引创建索引及文档查询索引及文档默认的信息扩展命令修改索引PUT覆盖POST修改删除索引关于文......
  • AtCoder Beginner Contest 266 Ex Snuke Panic (2D)
    AtCoder传送门洛谷传送门考虑dp,\(f_i\)设在\(t_i\)时刻到达第\(i\)个点,能获得的最大收益。转移:\(f_i=f_j+a_i\),其中\(j\)能转移到\(i\)的充要条件有:\(......
  • AtCoder Beginner Contest 201 题解
    vp情况:过了A到E,F没时间也不会。vp总结:ABC表现可以。D慢了一点,写之前大概考虑清楚每个变量或函数的意义,结构明晰才能更快的写出代码。E花的时间太长,原因......
  • D - Div Game -- ATCODER
    D-DivGamehttps://atcoder.jp/contests/abc169/tasks/abc169_d参考:https://blog.csdn.net/justidle/article/details/106474626 思路计算n中所有质数的幂,No......
  • Atcoder Grand Contest 002(A~F)
    这场打的还算舒服(虽然DE好像很久以前做过谔谔),VP赛时切掉了A~D,不过E依旧没写出来,还是太逊啦!赛时A简单分类讨论,求\(a\)到\(b\)之间数的乘积,第一眼看成了和,痛......
  • D - LRUD Instructions -- ATCODER
    D-LRUDInstructionshttps://atcoder.jp/contests/abc273/tasks/abc273_d 分析横坐标和纵坐标很大,不能采用二维数组形式,否则内存干爆,目标对象移动,按照指令进行移动......