跳到正文

题解

题解:CF2173C Kanade's Perfect Multiples

thousands_of_years约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月30日
更新
2025年12月30日
篇幅
约 7 分钟
题目
CF2173C

版本

  1. v1当前

solution

解法很简单,因为最小的数肯定在 bb 序列里,排个序,从小到大未标记的就是当前最小的数在 bb 序列里,枚举这个倍数判断,出现未出现的就失败,否则每个数都打上标记。

复杂度如何呢?我们从每个点被跳的次数来考虑,肯定只有从自己的因子处跳过来,于是 nn 个数的被跳总次数为 nd(109)n d(10^9) 次,算上优化跳的数据结构,总复杂度为 O(nd(109)log⁡n)O(n d(10^9) \log n),然而 d(n)d(n) 在 10910^9 范围下最大是 13441344,再加上远远到不了上界,所以可以通过。

code

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p111 = ibuf, *p222 = ibuf, *p333 = obuf;
#define getchar() ((p111==p222)and(p222=(p111=ibuf)+fread(ibuf,1,IOSIZE,stdin),p111==p222)?(EOF):(*p111++))
#define putchar(x) ((p333==obuf+IOSIZE)&&(fwrite(obuf,p333-obuf,1,stdout),p333=obuf),*p333++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T readdd() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool readdd(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void printtt(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) printtt(x / 10); putchar(x % 10 + 48); }
	inline bool readdd(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool readdd(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void printtt(char x) { putchar(x); }
	inline void printtt(char *x) { while (*x) putchar(*x++); }
	inline void printtt(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool readdd(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void printtt(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool readdd(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void printtt(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int readdd(T& a, T1&... other) { return readdd(a) + readdd(other...); }
	template<typename T, typename... T1> inline void printtt(T a, T1... other) { printtt(a), printtt(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p333 - obuf, 1, stdout); } } iooo;
	template<typename T> Fast_IO& operator >> (Fast_IO &iooo, T &b) { return readdd(b), iooo; }
	template<typename T> Fast_IO& operator << (Fast_IO &iooo, T b) { return printtt(b), iooo; }
#define cout iooo
#define cin iooo
#define endl '\n'
} using namespace fast_IO;
const int N=2e6+9,inf=1e9+7;
int a[N],n,k;
map<int,int> vis;
map<int,int> mm;
void solve()
{
	mm.clear();
	vis.clear();
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		mm[a[i]]++;
	}
	vector<int> V;
	for(int j=1;j<=n;j++)
	{
		int v=a[j];
		if(vis[v]) continue;
		if(v<k/(n+1))
		{
			cout<<-1<<endl;
			return ;
		}
		for(int i=2;;i++)
		{
			if(i*v>k) break;
			if(mm.find(v*i)==mm.end())
			{
//				cout<<v*i<<"!";
				cout<<-1<<endl;
				return ;
			}
			vis[v*i]=1;
		}
		vis[v]=1;
		V.push_back(v);
	}
	cout<<V.size()<<endl;
	for(int v:V)
		cout<<v<<" ";
	cout<<endl;
}
signed main()
{
	int T;
	cin>>T;
	while(T--)
	{
		solve();
	}
}
原文