CF 1097F(Alex and a TV Show-bitset+反演)

The rules of this TV show are as follows: there are n multisets numbered from 1 to n. Each of them is initially empty. Then, q events happen; each of them is in one of the four possible types:

1 x v — set the x-th multiset to a singleton {v}.
2 x y z — set the x-th multiset to a union of the y-th and the z-th multiset. For example: {1,3}∪{1,4,4}={1,1,3,4,4}.
3 x y z — set the x-th multiset to a product of the y-th and the z-th multiset. The product A×B of two multisets A, B is defined as {gcd(a,b)∣a∈A,b∈B}, where gcd(p,q) is the greatest common divisor of p and q. For example: {2,2,3}×{1,4,6}={1,2,2,1,2,2,1,1,3}.
4 x v — the participant is asked how many times number v occurs in the x-th multiset. As the quiz turned out to be too hard in the past, participants should now give the answers modulo 2 only.
Note, that x, y and z described above are not necessarily different. In events of types 2 and 3, the sum or the product is computed first, and then the assignment is performed.

Alex is confused by the complicated rules of the show. Can you help him answer the requests of the 4-th type?
Input
1 ≤ n ≤ 1 0 5 , 1 ≤ q ≤ 1 0 6 , 1 ≤ x , y , z ≤ n , 1 ≤ v ≤ 7000 1\le n \le 10^5, 1\le q\le 10^6,1\le x,y,z\le n,1\le v\le 7000 1n105,1q106,1x,y,zn,1v7000
It’s guaranteed that there will be at least one event of the 4-th type.

Output
Print a string which consists of digits 0 and 1 only, and has length equal to the number of events of the 4-th type. The i-th digit of the string should be equal to the answer for the i-th query of the 4-th type.

由于同一集合删除2相同元素不改变4的答案,故可用bitset记录每个数是否存在。
反演:记录每个数的倍数存在的个数。
这样操作3转化为点乘(and)操作。
然后就可以容斥了。


#include 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i
#define ForD(i,n) for(int i=n;i>0;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector 
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (101010) 

int n,q;
bitset<7001> g[7001],g2[7001],p;
bitset<7001> h[MAXN]; 
int wq[7001];
int main() {
	n=read(),q=read();
	int M=7000;
	
	for(int i=2;i*i<=M;i++) {
		int t=i*i;
		for(int j=1;j*t<=M;j++) wq[t*j]=1;
	}
	For(i,7000)	g[i].reset(),g2[i].reset();
	For(i,7000) {
		For(j,i) {
			if(i%j==0) {
				g[i].set(j);
				if(!wq[ i/j]) g2[j].set(i);
			}
		}
	}
	For(i,n) h[i].reset();
	For(i,q){
		int op=read();	
		if(op==1) {
			int x=read(),v=read();
			h[x]=g[v]; 
		}
		if(op==2) {
			int x=read(),y=read(),z=read();
			h[x]=h[y]^h[z];
		}else if (op==3) {
			int x=read(),y=read(),z=read();
			h[x]=h[y]&h[z];
		}else if (op==4) {
			int x=read(),v=read();
			p=h[x]&g2[v];
			int q=p.count()%2;
			printf("%d",q);
		}
	}
	return 0;
}

你可能感兴趣的:(反演,bitset)