\(\mathscr{Description}\)

  Link.

  求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最大化 \(|S|\)。

  \(n\le10^6\)。

\(\mathscr{Solution}\)

  爆搜打出 \(20\) 以内的表,发现 \(|S|\approx n\)。先研究偶数 \(n=2k\):

\[\begin{aligned}
\prod_{i=1}^{2k} i! &= \left( \prod_{i=1}^k i! \right)^2 \prod_{i=1}^k 2i\\
&= \left( \prod_{i=1}^k i! \right)^2 2^k k!.
\end{aligned}
\]

那么若 \(2^kk!\) 是完全平方数,有 \(|S|=n\);否则若 \(2^k\) 是完全平方数,有 \(|S|=n-1\),删去 \(k!\) 即可;否则至少有 \(|S|=n-2\),只需要删去 \(2!\) 和 \(k!\)。继而,对于奇数 \(n=2k+1\),答案至少为 \(n-3\)。

  所以,我们只需要判断 \(|S|\) 能否取 \(n,n-1,n-2\)。这里有个 trick:异或哈希。对于每个素数 \(p\),独立随机生成 hash 值 \(h(p)\),并定义 \(h(ab)=h(a)\oplus h(b)\),这样就能对每个数建立 hash,若两数 \(x,y\) 的唯一分解中指数奇偶性完全一致,就应有 \(h(x)=h(y)\)。利用这个 trick,求出所有 \(h(i!)\) 后顶多拿 unordered map 判一判就能完成 \(S\) 的取值判断了。复杂度为 \(\mathcal O(n)\)(假定 std::unordered_map 为 \(\mathcal O(1)\) 操作)。

\(\mathscr{Code}\)​

/*+Rainybunny+*/

#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = l, rep##i = r; i <= rep##i; ++i)
#define per(i, r, l) for (int i = r, per##i = l; i >= per##i; --i) typedef unsigned long long ULL; const int MAXN = 1e6;
int n, pn, pr[MAXN + 5];
bool npr[MAXN + 5];
ULL hnum[MAXN + 5], hfac[MAXN + 5];
std::unordered_map<ULL, int> buc; inline void init() {
std::mt19937_64 emt(time(0) ^ 20120712);
rep (i, 2, n) {
if (!npr[i]) hnum[pr[++pn] = i] = emt();
for (int j = 1, t; j <= pn && (t = i * pr[j]) <= n; ++j) {
npr[t] = true, hnum[t] = hnum[i] ^ hnum[pr[j]];
if (!(i % pr[j])) break;
}
}
rep (i, 1, n) hfac[i] = hfac[i - 1] ^ hnum[i];
} int main() {
scanf("%d", &n), init(); ULL h = 0;
rep (i, 1, n) h ^= hfac[i]; if (!h) {
printf("%d\n", n);
rep (i, 1, n) printf("%d%c", i, i < n ? ' ' : '\n');
return 0;
} rep (i, 1, n) if (h == hfac[i]) {
printf("%d\n", n - 1);
rep (j, 1, n) if (i != j) printf("%d ", j);
return putchar('\n'), 0;
} rep (i, 1, n) buc[hfac[i]] = i;
rep (i, 1, n) if (buc.count(h ^ hfac[i])) {
printf("%d\n", n - 2); int tmp = buc[h ^ hfac[i]];
rep (j, 1, n) if (j != i && j != tmp) printf("%d ", j);
return putchar('\n'), 0;
} printf("%d\n", n - 3);
rep (i, 1, n - 1) if (i != 2 && i != n >> 1) printf("%d ", i);
return putchar('\n'), 0;
}

Solution -「CF 1622F」Quadratic Set的更多相关文章

  1. Solution -「CF 1342E」Placing Rooks

    \(\mathcal{Description}\)   Link.   在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...

  2. Solution -「CF 923F」Public Service

    \(\mathscr{Description}\)   Link.   给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...

  3. Solution -「CF 923E」Perpetual Subtraction

    \(\mathcal{Description}\)   Link.   有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...

  4. Solution -「CF 1586F」Defender of Childhood Dreams

    \(\mathcal{Description}\)   Link.   定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...

  5. Solution -「CF 1237E」Balanced Binary Search Trees

    \(\mathcal{Description}\)   Link.   定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...

  6. Solution -「CF 623E」Transforming Sequence

    题目 题意简述   link.   有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...

  7. Solution -「CF 1023F」Mobile Phone Network

    \(\mathcal{Description}\)   Link.   有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...

  8. Solution -「CF 599E」Sandy and Nuts

    \(\mathcal{Description}\)   Link.   指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...

  9. Solution -「CF 487E」Tourists

    \(\mathcal{Description}\)   Link.   维护一个 \(n\) 个点 \(m\) 条边的简单无向连通图,点有点权.\(q\) 次操作: 修改单点点权. 询问两点所有可能路 ...

随机推荐

  1. console.log(a)和console.log(window.a)的区别?

    console.log(window.l); //undefined console.log(l); //Uncaught ReferenceError: l is not defined js对于未 ...

  2. nefu120梅森素数

    #include<iostream> #include<cstdio> using namespace std; typedef long long ll; const int ...

  3. Easticsearch概述(ES、Lucene、Solr)一

    ES是在Lucene的基础上实现的 1.Lucene全文检索 lucene是一个全文搜索框架,而不是应用产品.因此它并不像http://www.baidu.com/或goolge Destop 那么拿 ...

  4. 【Java常用类】两个Date类

    两个Date类 java.util.Date类 两个构造器的使用 构造器一:Date():创建一个对应当前时间的Date对象 构造器二:创建指定毫秒数的Date对象 两个方法的使用 toString( ...

  5. GeoServer介绍

    GeoServer本质上是一个地图服务器,它是遵循OpenGIS Web 服务器规范的J2EE实现,通过它可以方便的将地图数据发布为地图服务,实现地理空间数据在用户之间的共享.另外,它也提供了相应的接 ...

  6. django框架--登录注册功能(ajax)

    注册 实现一个注册功能 编写 html 内容 input 标签 csrf_token ajax 路由 视图: 提供页面 负责处理业务,返回响应 接收到   post   请求传递的参数 写库 返回   ...

  7. 带你十天轻松搞定 Go 微服务系列(一)

    本文开始,我们会出一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建(本文) 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Au ...

  8. codeblocks中报错:'to_string' was not declared in this scope解决方案

    在windows下使用codeblocks(编译器采用MinGW)时,有时会遇到"'to_string' was not declared in this scope"的错误,这里 ...

  9. UML 有关用例图知识及用例关系

    原文链接:https://blog.csdn.net/mj_ww/article/details/53020080 1. 如何识别用例 任何用例都不能在缺少参与者的情况下独立存在.同样,任何参与者也必 ...

  10. 【解决了一个小问题】gin框架中出现如下错误:"[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 500"

    POST到数据到一条gin框架的接口后,客户端收到400错误,并且返回了业务中返回的"decode json fail". 关键代码是: func report(c *gin.Co ...