跳到正文

题解

P7586 [COCI 2012/2013 #1] SNAGA

内拉组里约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年9月4日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 5 分钟
题目
P7586

版本

  1. v1当前

小清新数学题一道

在 @djh0314 大佬和 D 教授 的帮助下终于过了这题。。

Thoughts:

思路和这篇题解差不多,大概就是令不能被 xx 整除最小正整数为 f(x)f(x),然后分类讨论:

  1. x=2x = 2,strength⁡(x)=1\operatorname{strength}(x) = 1,直接由题意得;

  2. 2∤x2 \nmid x,strength⁡(x)=2\operatorname{strength}(x) = 2,x→2x \rightarrow 2;

  3. 2∤f(x)2 \nmid f(x),strength⁡(x)=3\operatorname{strength}(x) = 3,x→f(x)→2x \rightarrow f(x) \rightarrow 2;

  4. 2∣f(x)2 \mid f(x),strength⁡(x)=4\operatorname{strength}(x) = 4,x→f(x)=2k→3→2x \rightarrow f(x) = 2^k \rightarrow 3 \rightarrow 2。

    重点证明一下这一条:

    假设 f(x)=2k×mf(x) = 2^k \times m,其中 2k∥f(x)2^k \parallel f(x),m≥3m \ge 3。

    对于所有 mm 的质因数 pp,一定有 2∤p2 \nmid p,

    此时令 aa 满足 pa∥mp^a \parallel m,那么就有 pa≤m≤f(x)2kp^a \le m \le \cfrac {f(x)}{2^k},

    又 ∵2∣f(x)\because 2 \mid f(x),

    ∴f(x)2k≤f(x)2<f(x)\therefore \cfrac{f(x)}{2^k} \le \cfrac{f(x)}2 \lt f(x),

    ∴1≤pa<f(x)\therefore 1 \le p^a \lt f(x),pa∣lcm⁡{1,2,3,⋯ ,f(x)−1}p^a \mid \operatorname{lcm}\{ 1,2,3,\cdots,f(x) - 1 \}。


    对于 2k2^k 部分,由于 m≥3m \ge 3,故有 2k≤f(x)3<f(x)2^k \le \cfrac{f(x)}3 \lt f(x)。

    同理,1≤2k<f(x)1 \le 2^k \lt f(x),2k∣lcm⁡{1,2,3,⋯ ,f(x)−1}2^k \mid \operatorname{lcm}\{ 1,2,3,\cdots,f(x) - 1 \}。

    综上,所有的 pap^a 和 2k2^k 均整除 lcm⁡{1,2,3,⋯ ,f(x)−1}\operatorname{lcm}\{ 1,2,3,\cdots,f(x) - 1 \},且两两互质,

    因此有 f(x)=2k×∏pa∣lcm⁡{1,2,3,⋯ ,f(x)−1}f(x) = 2^k \times \prod p^a \mid \operatorname{lcm}\{ 1,2,3,\cdots,f(x) - 1 \},

    故一定有 f(x)∣xf(x) \mid x,与定义矛盾。

    所以,

    一定有 m<3m \lt 3,而又因为 2k∥f(x)2^k \parallel f(x),故只有 m=1m = 1 可取。

    所以 f(x)=2kf(x) = 2^k,证毕。

剩下的统计答案就简单多了。

第一种直接特判即可。

先把所有的答案都设为 33,然后减去第二种的数量,加上第四种的数量就是最终答案了。

在统计第四种的数量时可以用一个简单的容斥,

枚举 2k2^k 部分,打表可得 lcm⁡{1,2,3,⋯ ,43}>1018\operatorname{lcm}\{ 1,2,3,\cdots,43 \} \gt 10^{18},故只需要枚举到 25=322^5 = 32 即可。

Analyses:

记阈值 λ=42\lambda = 42,

总时间复杂度 Θ(log⁡2λ)≈Θ(1)\Theta (\log^2{\lambda}) \approx \Theta (1)。

总空间复杂度 Θ(log⁡λ)≈Θ(1)\Theta (\log{\lambda}) \approx \Theta (1)。

Code:

C++
#include	<algorithm>
#include	<iostream>
#define		int		long long
using namespace std;

int a, b;
int lcm[8];

inline int calc (int x)
{
	int ans = (x << 1) + (x >> 1) - 4;
	for (int i = 2; i <= 5; i++)
		ans += x / lcm[i] - x / (lcm[i] / __gcd (lcm[i], 1ll << i) * (1 << i));
	return ans;
}

signed main (void)
{
	cin >> a >> b;
	lcm[0] = 1;
	for (int i = 1; i <= 5; i++)
	{
		lcm[i] = lcm[i - 1];
		for (int j = (1 << (i - 1)); j < (1 << i); j++)
			lcm[i] *= j / __gcd (lcm[i], j);
	}
	cout << calc (b) - calc (a - 1) << endl;
	return 0;
}
原文