跳到正文

题解

题解:P7586 [COCI 2012/2013 #1] SNAGA

hehaorui约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
1
题目
P7586

版本

  1. v2当前
  2. v1

这是蒟蒻考场上唯一会做的题 首先一眼就发现了一个东西就是

∑i=ABstrength⁡(i)=∑i=2Bstrength⁡(i)−∑i=2A−1strength⁡(i){\sum_{i=A}^{B}\operatorname{strength}(i)=\sum_{i=2}^{B}\operatorname{strength}(i)-\sum_{i=2}^{A-1}\operatorname{strength}(i)} 然后就可以把这个问题转换成求 给定 xx 求 ∑i=1xstrength⁡(i){\sum_{i=1}^{x}\operatorname{strength}(i)}

在此我们设数 xx 的最小不可被整除数为 f⁡(x)\operatorname{f}(x) 然后再在纸上浅浅的推一下就会发现 strength⁡(x)=strength⁡(f⁡(x))+1\operatorname{strength}(x)=\operatorname{strength}(\operatorname{f}(x))+1 因为这个 xx 全是小于等于 101710^{17} 的,然后你就会发现这个 xx 如果作为连续数的 lcm⁡\operatorname{lcm} 的话最大不会超过 lcm⁡[1,2,3...41]\operatorname{lcm}[1,2,3...41] 所以我们的 strength⁡\operatorname{strength} 的所有值一定不会超过 42!42!

然后就是怎么统计的了,这里提供一种新颖的思路,我们可以通过一次一次因数迭代的方法,对于一开始的这个全部的数,我们可以把含有2这个因数的先不管,那剩下没有2因数的答案一定就是 strength⁡(2)+1\operatorname{strength}(2)+1,然后我们现在把范围缩小至有 22 这个因数的范围,然后再来考虑3这个因数,对于没有 33 这个因数的我们直接算上答案,接着再考虑4这个因数,因为前面要是能走到这已经有 22 这个因数了,所以我们只要在这些里面考虑去掉之前 22 因数和 33 因数里再找含有 22 这个因数的的就好了,一次又一次的迭代就相当于一次又一次的去掉这个当前找到的因数,所以这个类似于从一堆连续的数里面找出给定 pp 的因数有几个。

每一次找到统计就好了。

Code

C++
#include<iostream>
#define int long long
using namespace std;
int n,m;
int s[105];
int st[105]={0,0,1,2,3,2,4,2,3,2,3,2,3,2,3,2
,3,2,4,2,3,2,3,2,3,2,3,2,3,2,4,2,3,2,3,2,3,2,3
,2,3,2,4,2,3,2,3,2,3,2,3};//打一下我们要求的因数的strength表
int oper(int a)
{
	int now=a;                      //当前有几个数
	int sum=0;                      //记录答案
	for(int i=2;i<=50;i++)          //依次枚举因数
	{
		if(!now)                    //没有数了就蹦出去
			break;
		int p=i;
		for(int j=2;j<i;j++)
			if(!(p%s[j]))
				p/=s[j];            //这里很重要,要去掉之前枚举过的因数,因为之前已经考虑过了
		sum+=(now-now/p)*(st[i]+1); //当前这些数减去符合条件的数就是不符合条件的数,再乘上就可以了
		now=now/p;                  //缩小范围
		s[i]=p;                     //记录当前考虑过的这个因数
	}
	return sum-4;//这里因为把strength(1)当2算了,strength(2)当3算了所以就减一下
}
signed main()
{
	cin>>n>>m;
	cout<<oper(m)-oper(n-1)<<"\n";
	return 0;
	//Thanks God!
}
原文