跳到正文

个人记录

2022.9.3NOIP联训15爆零记

masonpop约 4 分钟
原文

这是 2026年1月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2022年9月3日
收录
2026年1月24日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

这次的经历非常特殊,值得记录。

话说tlecoderstlecoders怎么又炸了啊。。。

这次的T1T1不难,但花费了大量时间,错误原因是:longlonglong long溢出!

题目长这样:

可以看到,枚举kk,在二分答案就做完了.

快乐的提交!78pts???78pts???

源代码是这样的:

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int t,n;
int tot=0,fac[100010];//预处理阶乘 
int k;//(x+1)...(x+k)
int maxx;//k的上限 
int check(int x,int num)//(x+1)..(x+num)
{
	int res=1;
	for(int i=x+1;i<=x+num;i++)res*=i;
	return res;
}
int calc;//个数 
struct anss{
	int a,b;
	bool operator<(const anss &o)const{//按a排序 
		return a<o.a;
	}
}den[50];
signed main()
{
	//freopen("factorial2.in","r",stdin);
	//freopen("factorial2.out","w",stdout);
	int maxn=1e18;
	fac[0]=1;
	for(int i=1;i;i++)//预处理阶乘 
	{
		if(fac[i-1]>maxn/i)break;//超过了 
		tot++;
		fac[i]=fac[i-1]*i;
	}
	scanf("%lld",&t);
	while(t--)
	{
		calc=0;
		scanf("%lld",&n);
		if(n==1)//特判
		{
			printf("-1\n");
			continue;
		} 
		int k=lower_bound(fac+1,fac+tot+1,n)-fac;//(x+1)..(x+k)>=k!
		for(int i=1;i<=k;i++)//枚举个数
		{
			int l=1,r=((int)pow(n,1.0/i)-1);//(x+1)<=i次根号下n
			int ans=0;//x
			while(l<=r)
			{
				int mid=(l+r)>>1;
				int rep=check(mid,i);
				if(rep==n)
				{
					ans=mid;
					break;
				}
				else if(rep<n)l=mid+1;
				else r=mid-1;
			} 
			if(ans)//有答案
			{
				calc++;
				den[calc].a=ans+i,den[calc].b=ans;
			} 
		} 
		sort(den+1,den+calc+1);
		printf("%lld\n",calc);
		for(int i=1;i<=calc;i++)
		{
			printf("%lld %lld\n",den[i].a,den[i].b);
		}
	}
	return 0;
} 

问题有两点:

(1)(1)系统自带的powpow函数出了锅。

C++
int l=1,r=((int)pow(n,1.0/i)-1)

上面这个代码在i=1i=1时会出问题,所以要特判。 加上

C++
if(i==1)l=1,r=n;

后提交,结果是-------78pts.78pts.

(2)longlong(2) long long范围溢出。

我们的checkcheck函数长这样:

C++
int check(int x,int num)//(x+1)..(x+num)
{
	int res=1;
	for(int i=x+1;i<=x+num;i++)res*=i;
	return res;
}

但这个函数在num,xnum,x比较大时会溢出longlonglong long(比如极限数据129441336922779624129441336922779624)

这时候,我们就需要在checkcheck时判断,一旦超过nn就直接返回,返回值也是resres和nn的大小关系。

改完后的checkcheck函数:

C++
int check(int x,int num)//(x+1)..(x+num)
{
	int res=1;
	for(int i=x+1;i<=x+num;i++)
	{
		res*=i;
		if(res>n)return 1;
	}
	if(res<n)return 2;
	return 0;
}

提交,100pts!100pts!

总 结 一 下

在涉及较大数据的题目中,一定要考虑极限数据的溢出,必要时使用高精度。对stlstl函数的边界和运用条件的理解也不可忽视。