社区讨论

求调70Pts,TLE,埃氏筛优化预处理,筛的范围1e7+7

P7960[NOIP2021] 报数参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mi1vb4y5
此快照首次捕获于
2025/11/16 23:25
3 个月前
此快照最后确认于
2025/11/18 10:32
3 个月前
查看原帖
Rt,不是范围问题,筛法是类似埃氏筛,优化后很像线性筛,样例能过。有正常注释。
CPP
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e7+7;
bool snum[mn];//snum=safenum,1为违禁数字(包含七的数字的整数倍) 
void shai()//筛 
{
	for(int j = 0;j < mn;j ++ )
	{
		int k=j;//不能动J 
		int temp = 1e7;//模数 
		for(int i = 7;i > 0;i--)
		{
			temp /= 10;
			if((int)(k / temp) == 7)//如果该数新的最高位为七 
			{
				if(!snum[j])for(int r = 2 * j;r < mn;r+=j) snum[r]=1;//筛的优化(重点怀疑对象):如果这个数之前就被标记过,说明他的倍数也被标记过,不再重复标记 
				snum[j]=1;//标记 
				break;//这个数字不必再检查 
			}
			k%=temp;//不断将最高位降低一位 
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	shai();
	int t;cin >> t;
	while(t--)
	{
		int n;cin >> n;
		if(snum[n]){cout << -1 << "\n";continue;}
		int i = n;
		while(snum[++i]) continue;//重复直到下一个数字安全 
		cout << i << "\n";//输出安全数字 
	}
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...