跳到正文

剪贴板

剪贴板 q6kxzto1

原文
剪贴板信息

剪贴板信息

收录
更新

无平方因子数 (mu)

原理大概就是,实际上每个素数就是某一维的单位向量, 11 作为原点, 这样质因数分解 p1s1⋯pkskp_1^{s_1}\cdots p_k^{s_k} 就决定了每个数在这个无限维空间中的位置。

无平方因子数又是特殊的, 因为每一维的坐标只能是 00 或 11。

然后你还发现这个奇怪的乘法运算就是对两个坐标进行异或, 这样这个奇怪的结构就和线性基扯上了关系。

转化后,容易发现题目要求做的就是向线性基中插入一个数,和询问能否用这些基底表示当前的数。

但是直接套用传统线性基的做法并不是有效的。因为 VV 以内的素数个数大概为 O(Vlog⁡V)O\left(\dfrac{V}{\log V}\right) 个,线性基的位数是惊人的。

一个观察是,实际上我们只需要对 V\sqrt{V} 以内的素数进行操作即可。因为对于每个数,大于等于 V\sqrt{V} 的素数最多只有一个。而对于剩下的部分,直接大力 bitset 维护即可。(虽然每个数包含的素数是很少的, 但是异或几次就多了,这个还是需要整体维护)

时间复杂度是神秘的 O(t((Vlog⁡V)2/w+Vlog⁡V))=O(t(Vwlog⁡2V+Vlog⁡V))O\left(t\left(\left(\dfrac{\sqrt{V}}{\log V}\right)^2/w+\dfrac{\sqrt{V}}{\log V}\right)\right)=O\left(t\left(\dfrac{V}{w\log^2V}+\dfrac{\sqrt{V}}{\log V}\right)\right),算出来不到 2×1072\times 10^7,完全能过!

std:

C++
//by pjykk
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <bitset>
using namespace std;
inline int read()
{
	int ret=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){ret=ret*10+ch-'0';ch=getchar();}
	return ret*f;
}
//There are 168 primes in [2,1000] and 78498 primes in [2,1000000]
const int maxn=1000010,pr1=170,pr2=78500;
int t,pcnt,prime[maxn],vis[maxn],pos[maxn];
bool isbasis=1,haveone;
bitset<pr1> basis[pr2];
int bigp;
bitset<pr1> now;
inline void sieve()
{
	for(int i=2;i<=1000000;i++)
	{
		if(vis[i])continue;
		prime[++pcnt]=i;
		for(int j=1;i*j<=1000000;j++)vis[i*j]=1;
	}
	for(int i=1;i<=pcnt;i++)pos[prime[i]]=i;
}
inline void fac(int x)
{
	now.reset();bigp=0;
	int lim=sqrt(x);
	for(int i=1;;i++)
	{
		if(prime[i]>lim)break;
		if(x%prime[i]==0)
		{
			x/=prime[i];
			now[i]=1;
		}
	}
	if(x==1)return;
	if(x<1000)
	{
		now[pos[x]]=1;
		return;
	}
	bigp=x;
}
inline void insert(int x)
{
	if(x==1)
	{
		haveone=1;
		return;
	}
	fac(x);
	if(bigp)
	{
		int posp=pos[bigp];
		if(basis[posp].none())
		{
			basis[posp]=now;
			return;
		}
		now^=basis[posp];
	}
	if(now.none())
	{
		isbasis=0;
		return;
	}
	for(int pos=pr1-1;pos>=1;pos--)
	{
		if(now[pos])
		{
			if(basis[pos].none())
			{
				basis[pos]=now;
				return;
			}
			now^=basis[pos];
		}
		if(now.none())
		{
			isbasis=0;
			return;
		}
	}
}
inline bool represent(int k)
{
	if(k==1)return haveone||(!isbasis);
	fac(k);
	if(bigp)
	{
		int posp=pos[bigp];
		if(basis[posp].none())return 0;
		now^=basis[posp];
	}
	for(int pos=pr1-1;pos>=1;pos--)
	{
		if(now.none())return 1;
		if(now[pos])
		{
			if(basis[pos].none())return 0;
			now^=basis[pos];
		}
	}
	return 1;
}
int main()
{
	sieve();
	t=read();
	for(int i=1;i<=t;i++)
	{
		int opt=read(),k=read();
		if(opt==1)insert(k);
		else printf("%d\n",represent(k));
	}
	return 0;
}