跳到正文

题解

P6583 回首过去 题解

acmpGu约 4 分钟
原文
文章信息与版本

文章信息

发布
2022年11月11日
收录
2026年2月4日
更新
2026年11月14日
篇幅
约 4 分钟
题目
P6583

版本

  1. v2当前
  2. v1

如果一个分数化成小数是有限的,则这个分数约分后的分母只能含有质因子 22 和 55。

假设这个分数为:

bcac\frac{bc}{ac}

保证 a=2n×5ma = 2^n \times 5^m,(c,2)=(c,5)=1(c,2)=(c,5)=1,b∈N+b \in \mathbb{N}^{+}。

80 pts: \text{80 pts: }

我们可以枚举 cc,那么 aa 不能超过 ⌊nc⌋\left\lfloor \frac{n}{c} \right\rfloor,满足条件的 aa 的个数可以预处理求出来。bb 是不超过 ⌊nc⌋ \left\lfloor \frac{n}{c} \right\rfloor 的所有数字,所以我们可以枚举 cc 求出方案数,总复杂度 O(n)O(n),想 AC 还需要优化。

100 pts: \text{100 pts: }

⌊nc⌋\left\lfloor \frac{n}{c} \right\rfloor 在一段区间内的值是固定不变的,那么我们就可以整除分块来批量计算,但是 cc 并不能取到所有的值,只能 2∤c2 \nmid c 且 5∤c5 \nmid c,cc 的个数可以容斥原理计算,即总数量 −- (( 22 的倍数数量 ++ 55 的倍数数量 −- 1010 的倍数数量 ))。

预处理 Time Complexity: O(log⁡2n⋅log⁡5n)\text{Time Complexity: }O(\log_2n·\log_5n)。

计算答案 Time Complexity: O(n)\text{Time Complexity: }O(\sqrt{n})。

AC Code: \text{AC Code: }

C++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

long long _maxi = 0;
long long _res = 0;

int main () {

	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	cin >> _maxi;

	vector<long long> theList;

	// find all num that only 2|num or 5|num
	for (long long i = 1; i < _maxi + 1; i *= 2) {
		for (long long m = i; m < _maxi + 1; m *= 5) {
			theList.push_back(m);
		}
	}

	sort(theList.begin(), theList.end());

	long long numA = theList.size();

	for (long long left = 1, right = 0; left < _maxi + 1; left = right + 1) {
		long long num = _maxi / left; // floor(n / c)

		right = _maxi / num;

		// find a num that <= num
		while (theList[numA - 1] > num) {
			numA--;
		}

		long long numC = right - left + 1; // all [l, r]
		long long num2 = right / 2 - (left - 1) / 2; // n∈[l, r], 2 | n
		long long num5 = right / 5 - (left - 1) / 5; // n∈[l, r], 5 | n
		long long num10 = right / 10 - (left - 1) / 10; // n∈[l, r], 10 | n

		numC -= (num2 + num5 - num10); // c num
		_res += num * numA * numC; // update res
	}

	cout << _res << '\n';

	return 0;

}
原文
存档评论2 条抓取于