1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000000 + 5; const int P1 = 998244353, P2 = 1000000007; const int B1 = 2, B2 = 31; const int K1 = 0, K2 = 13; typedef long long ll; int n; bool p[maxn]; int p1[maxn], p2[maxn]; struct H { int h1, h2, l; H(bool b = false) { h1 = b + K1; h2 = b + K2; l = 1; } H operator + (const H & h) const { H hh; hh.l = l + h.l; hh.h1 = (1ll * h1 * p1[h.l] + h.h1) % P1; hh.h2 = (1ll * h2 * p2[h.l] + h.h2) % P2; return hh; } bool operator == (const H & h) const { return l == h.l && h1 == h.h1 && h2 == h.h2; } bool operator < (const H & h) const { if (l != h.l) return l < h.l; else if (h1 != h.h1) return h1 < h.h1; else return h2 < h.h2; } } h[maxn]; void init() { memset(p, 1, sizeof(p)); p[0] = p[1] = false; p1[0] = p2[0] = 1; for (int i = 1; i <= n; ++i) { p1[i] = (1ll * B1 * p1[i-1]) % P1; p2[i] = (1ll * B2 * p2[i-1]) % P2; if (!p[i]) continue; for (int j = 2 * i; j <= n; j += i) { p[j] = false; } } } int solve() { for (int i = n; i; --i) { h[i] = H(p[i]); if (2 * i + 1 <= n) { h[i] = h[2 * i] + h[i] + h[2 * i + 1]; } else if (2 * i <= n) { h[i] = h[2 * i] + h[i]; } } cout << h[1].h1 << endl; sort(h + 1, h + n + 1); int m = unique(h + 1, h + n + 1) - (h + 1); return m; } int main() { cin >> n; init(); cout << solve() << endl; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
maxn
改为 n+1
,所实现的算法的时间复杂度是O(nlogn)。( )2、时间开销的瓶颈是 init()
函数。( )
B1
或 K1
的值,该程序可能会输出不同的结果。( )4、在 solve() 函数中,h[] 的合并顺序可以看作是( )?
4、输入 10,输出的第一行是?( )
6、输入 16,输出的第二行是?( )