跳到正文

题解

题解 P6583【回首过去】

fsy_juruo约 7 分钟
原文
文章信息与版本

文章信息

发布
2020年5月30日
收录
2026年7月27日
更新
2026年11月14日
篇幅
约 7 分钟
获赞
3
题目
P6583

版本

  1. v1当前

upd:时间复杂度写错,已经修正。

40 pts

枚举 xx,yy,看 xy\frac{x}{y} 是否为有限小数。

时间复杂度 O(n2log⁡n)O(n^2 \log n),预计得分 4040 分。

80 pts

由学龄前数学可知:一个最简分数 xy\frac{x}{y},当且仅当 y=2i5j(i,j≥0)y = 2^i5^j (i,j \geq 0) 时,该分数为有限小数。

故考虑枚举分母 yy,将其表示为 2i5j×g2^i5^j \times g 的形式,所以只有当分子 xx 是 gg 的倍数时,原分数是有限小数。又因为 x≤nx \leq n,故符合条件的 xx 有 ⌊ng⌋\lfloor \frac{n}{g} \rfloor 个。

故答案为 ∑i=1n⌊ng⌋\sum_{i=1}^{n} \lfloor \frac{n}{g} \rfloor,O(n)O(n) 计算即可。预计得分 8080 分。

100 pts

即使 nn 很大,但是 ⌊ng⌋\lfloor \frac{n}{g} \rfloor 的可能取值只有 2n2 \sqrt{n} 种。

为什么?我们取 s=ns = \sqrt{n},则对于 i≤si \leq s,总共只有 ss 个数,故此部分最多有 ss 种取值。而对于 i>si > s,有 ni<n\frac{n}{i} < \sqrt{n},故此部分最多有 ss 种取值。综上,⌊ng⌋\lfloor \frac{n}{g} \rfloor 的可能取值只有 2n2 \sqrt{n} 种。

考虑分类处理。

对于 g≤ng \leq \sqrt{n},可以考虑枚举 gg,算出除尽所有 22,55 后,正好为 gg 的数字的个数 cntcnt,故这个 gg 对答案的贡献为 cnt×⌊ng⌋cnt \times \lfloor \frac{n}{g} \rfloor。至于 cntcnt 的计算,由于 240>10122^{40} > 10^{12},518>10125^{18} > 10^{12},可以暴力枚举二者的指数,也可以枚举其中一个数的指数,用 log⁡\log 算出第二个指数的取值范围。

对于 g>ng > \sqrt{n},考虑枚举 ⌊ng⌋\lfloor \frac{n}{g} \rfloor 的值,算出 gg 的取值范围后,再算出这个范围中,完全不含质因子 22,55 的数字数量 NumNum,再算出除尽所有 22,55 后,正好为 gg 的数字的个数 cntcnt,则此部分对答案的贡献为 Num×cnt×⌊ng⌋Num \times cnt \times \lfloor \frac{n}{g} \rfloor。

啥,你问我 gg 不同怎么算 cntcnt?其实对于同一个区间的 gg,其 cntcnt 都相同。

为什么?因为我们本质上是要算出满足 2i5j×g≤n2^i5^j \times g \leq n 的 (i,j)(i,j) 数对的个数。移项变成 2i5j≤ng2^i5^j \leq \frac{n}{g}。左边是整数,故 2i5j≤⌊ng⌋2^i5^j \leq \lfloor \frac{n}{g} \rfloor。同一段区间中的 ⌊ng⌋\lfloor \frac{n}{g} \rfloor 相等,故同一个区间的 gg,其 cntcnt 都相同。

综上,两部分时间复杂度均为 O(n)O(\sqrt{n}),总时间复杂度为 O(n)O(\sqrt{n}),预计得分 100100 分。

代码:

C++
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define byfakioi true
#define srftxdy true
#define _rep(i, x, y) for(int i = x; i <= y; i++)
#define _per(i, x, y) for(int i = x; i >= y; i--)
#define LL long long
using namespace std;
template <typename T>
inline void read(T &x) {
    x = 0; T f = (T) 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar())
        if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
    x *= f;
}
template <typename T>
inline void write(T x) {
	if(x < 0) {putchar('-'); x = -x;}
	if(x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T>
inline void writesp(T x, char sp = '\n') {
	write(x); putchar(sp);
}
LL gcd(LL x, LL y) {
	if(!x || !y) return x + y;
	return gcd(y, x % y);
}
int flg[3000010];
int log5(LL x) { // 浮点数的 log 太慢了所以暴力写了个 log
	if(x < 5) return 0;
	if(x < 25) return 1;
	if(x < 125) return 2;
	if(x < 625) return 3;
	if(x < 3125) return 4;
	if(x < 15625) return 5;
	if(x < 78125) return 6;
	if(x < 390625) return 7;
	if(x < 1953125) return 8;
	if(x < 9765625) return 9;
	if(x < 48828125ll) return 10;
	if(x < 244140625ll) return 11;
	if(x < 1220703125ll) return 12;
	if(x < 6103515625ll) return 13;
	if(x < 30517578125ll) return 14;
	if(x < 152587890625ll) return 15;
	if(x < 762939453125ll) return 16; 
	else return 17;
}
int main() {
	LL n; read(n);
	int Q = (int)sqrt(n);
	LL ans = 0;
	_rep(i, 1, Q) {
		LL y = i;
		while(!(y & 1ll)) y >>= 1ll;
		while(!(y % 5ll)) y /= 5ll;
		if(flg[y]) continue;
		flg[i] = 1;
		LL Pos = 0ll;
		LL P = n / i;
		_rep(j, 0, 39) {
			if(j) P >>= 1ll;
			if(P == 0) break;
			int q = log5(P) + 1;
			Pos += q;	
		}
		ans += (Pos) * (n / i);
	}
	_per(i, n / (Q + 1), 1) {
		LL Lb = n / (i + 1) + 1;
		LL Rb = n / i;
		LL Len = Rb - Lb + 1;
		LL Div2 = Rb / 2 - (Lb - 1) / 2;
		LL Div5 = Rb / 5 - (Lb - 1) / 5;
		LL Div10 = Rb / 10 - (Lb - 1) / 10;
		LL Pos = 0; LL P = i; 
		_rep(j, 0, 39) {
			if(j) P >>= 1ll;
			if(P == 0) break;
			Pos += log5(P) + 1;
		}
		ans += 1ll * Pos * i * (Rb - Lb + 1 - Div2 - Div5 + Div10); // 容斥原理计算 num
	}
	writesp(ans);
	return 0;
}
原文
存档评论4 条抓取于