阅读程序(3) 12-17题 组合题 #include #include #include 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 > n; init(); cout << solve() << endl; }
正确答案:判断题答案A。算法为筛法+排序+哈希,时间复杂度O(nlogn),正确。
试卷来源:2024 CSP-S提高级(第一轮)C++真题