考场40pts代码
#include <cstdio>
#include <time.h>
using namespace std;
inline int read() {
int res=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+ch-'0',ch=getchar();
return res*f;
}
const int MAXN=10000005;
bool vis[MAXN];
int nxt[MAXN];
int x[MAXN];
int mx=0;
inline bool check(int x) {
if(x%7==0) return true;
while(x) {
if(x%10==7) return true;
x/=10;
}
return false;
}
inline void change(int x) {
for(int i=x;i<=mx;i+=x)
vis[i]=1;
}
signed main() {
int T=read();
for(int i=1;i<=T;++i) {
x[i]=read();
mx=mx<x[i]?x[i]:mx;
}
int last=0;
for(int i=1;i<=mx;++i) {
if(vis[i]) continue;
if(check(i)) {
vis[i]=1;
change(i);
}
}
for(int i=1;i<=mx;++i)
if(!vis[i]) {
nxt[last]=i;
last=i;
}
for(int i=mx+1;;++i)
if(!check(i)) {
nxt[last]=i;
break;
}
for(int i=1;i<=T;++i) {
if(vis[x[i]]) puts("-1");
else printf("%d\n",nxt[x[i]]);
}
return 0;
}
100pts代码
#include <cstdio>
#include <time.h>
using namespace std;
inline int read() {
int res=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+ch-'0',ch=getchar();
return res*f;
}
const int MAXN=10000005;
bool vis[MAXN];
int nxt[MAXN];
int x[MAXN];
int mx=0;
inline bool check(int x) {
if(x%7==0) return true;
while(x) {
if(x%10==7) return true;
x/=10;
}
return false;
}
inline void change(int x) {
for(int i=x;i<=mx;i+=x)
vis[i]=1;
}
signed main() {
int T=read();
for(int i=1;i<=T;++i) {
x[i]=read();
}
mx=10000000;
int last=0;
for(int i=1;i<=mx;++i) {
if(vis[i]) continue;
if(check(i)) {
vis[i]=1;
change(i);
}
}
for(int i=1;i<=mx;++i)
if(!vis[i]) {
nxt[last]=i;
last=i;
}
for(int i=mx+1;;++i)
if(!check(i)) {
nxt[last]=i;
break;
}
for(int i=1;i<=T;++i) {
if(vis[x[i]]) puts("-1");
else printf("%d\n",nxt[x[i]]);
}
return 0;
}
唯一的改动是我考场代码没有筛到 1e7 而是筛到了询问中的最大值,并且对最大值作出了处理,但是这样一搞就变40了,我想知道为什么。
考场上担心1e7埃氏筛预处理会t就写了个这鬼玩意儿结果挂了