关于时间复杂度
查看原帖
关于时间复杂度
562237
Remained_Test楼主2021/11/20 19:34

瓶颈是奇怪的筛法

// Per aspera ad astra.
// 1004535809
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define re register
#define br break
#define co continue
#define ll long long
#define DEBUG if (dbg)
#ifndef ABC
#define getchar() (_S == _T && (_T = (_S = _B) + fread(_B, 1, 1 << 15, stdin), _S == _T) ? EOF : *_S++)
#endif
char _B[1 << 15], *_S = _B, *_T = _B;
ll fr() {
    re ll s = 0, f = 1;
    re char ch;
    for (; (ch = getchar()) < '0' || ch > '9'; ch == '-' ? f = -f : 0);
    for (; ch >= '0' && ch <= '9'; s = s * 10 + ch - '0', ch = getchar());
    return s * f;
}
int dbg = 0;
using namespace std;
const int W = 1e7, T = 2e5; 
template <class T> T cmin(re T a, re T b) {return a < b ? a : b;}
template <class T> T cmax(re T a, re T b) {return a > b ? a : b;}
template <class T> void cswp(re T &a, re T &b) {T t = a;a = b, b = t;}
struct OI {
    int rp, score;
} CSPS2022, NOIP2021;
int cnt, mx, _7[W + 10], qry[T + 10];
int tot, ok[W + 10]; bool isok[W + 10];
inline bool cxk(re int x) {
	while (x) {
		if (x % 10 == 7) return 1;
		x = x / 10;
	}
	return 0;
} 
signed main() {
#ifndef ONLINE_JUDGE
    dbg = 1;
#endif
    ++CSPS2022.rp, ++CSPS2022.score, ++NOIP2021.rp, ++NOIP2021.score, 1004535809;
  	freopen("number.in", "r", stdin);
  	freopen("number.out", "w", stdout);
    re int _ = fr();
    for (re int i = 1; i <= _; ++i) mx = cmax(mx, qry[i] = fr());
    mx += 100;
    for (re int i = 1; i <= mx; ++i) if (cxk(i)) _7[++cnt] = i;
    for (re int i = 1; i <= cnt; ++i)
    	for (re int j = 1; _7[i] * j <= mx; ++j) isok[_7[i] * j] = 1;
    for (re int i = 1; i <= mx; ++i) if (!isok[i]) ok[++tot] = i;
    for (re int i = 1; i <= _; ++i) {
    	re int p = lower_bound(ok + 1, ok + tot + 1, qry[i]) - ok;
    	if (qry[i] ^ ok[p]) printf("-1\n");
    	else printf("%d\n", ok[p + 1]);
	}
    return 0;
}

2021/11/20 19:34
加载中...