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
The first line contains two integers n and q (1≤n≤105, 1≤q≤106) — the number of multisets and the number of events.

Each of the following q lines describes next event in the format given in statement. It’s guaranteed that 1≤x,y,z≤n and 1≤v≤7000 always holds.

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 <bits/stdc++.h>
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<n;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<int> 
#define pi pair<int,int>
#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<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章