Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!". After perfectly answering the questions "How is a pseudonym commonly referred to in the Internet?" ("Um... a nick?"), "After which famous inventor we name the unit of the magnetic field strength?" ("Um... Nikola Tesla?") and "Which rock band performs "How You Remind Me"?" ("Um... Nickelback?"), he decided to apply to a little bit more difficult TV show: "What's in This Multiset?!".

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.

Example

Input
4 13
1 1 1
1 2 4
1 3 6
4 4 4
1 4 4
2 2 1 2
2 3 3 4
4 4 4
3 2 2 3
4 2 1
4 2 2
4 2 3
4 2 4
Output
010101

Note

Here is how the multisets look in the example test after each of the events; i

is the number of queries processed so far:

题意:

n个可重集,有Q次操作
   1 u v 表示将第u个可重集的元素置为1个v
   2 u a b 表示将第u个可重集置为第a个可重集和第b个可重集的并集
   3 u a b 表示将第u个可重集置为第a个可重集的每个元素和第b个可重集的每个元素的gcd的并集
   4 u v 表示求在第u个可重集中元素v的出现次数是奇数还是偶数
   n<=1e5 Q<=1e6 1<=v<=7000

思路:由于是只要求奇数还是偶数,我们整个过程只需要保存0和1即可,我们用莫比乌斯来求是否存在一个gcd,即保存当前集合是因子的奇偶性。那么对于2和3,我们可以直接操作(分别是^ &)了。

假设我们知道了因子的数量的奇偶性,假设保存在s[]里面。  vis[gcd]=mu(d/gcd)*s[d];所以对于每个gcd,我们预处理出mu(d/gcd)!=0的位置d,保存到b[]里面。

由于只求奇偶,1和-1的效果等效,结果和s[x]*b[y]的1的数量奇偶相同;

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int maxm=;
bitset<maxm>s[maxn],b[maxm];
int mu[maxm],p[maxm],cnt;bool vis[maxm];
vector<int>G[maxm];
void init()
{
mu[]=;
rep(i,,maxm-){
if(!vis[i]) p[++cnt]=i,mu[i]=-;
rep(j,,cnt){
if(i*p[j]>=maxm) break;
vis[i*p[j]]=;
if(!(i%p[j])) {mu[i*p[j]]=; break;}
mu[i*p[j]]=-mu[i];
}
}
rep(i,,maxm-)
for(int j=i,k=;j<=maxm-;j+=i,k++){
G[j].push_back(i);
if(mu[k]!=) b[i][j]=;
}
}
int main()
{
int N,M,opt,x,y,z;
init();
scanf("%d%d",&N,&M);
while(M--){
scanf("%d",&opt);
if(opt==){
scanf("%d%d",&x,&y);
s[x].reset();
rep(i,,G[y].size()-)
s[x][G[y][i]]=s[x][G[y][i]]^;
}
else if(opt==){
scanf("%d%d%d",&x,&y,&z);
s[x]=s[y]^s[z];
}
else if(opt==){
scanf("%d%d%d",&x,&y,&z);
s[x]=s[y]&s[z];
}
else {
scanf("%d%d",&x,&y);
if((s[x]&b[y]).count()&) putchar('');
else putchar('');
}
}
return ;
}

CodeForces - 1097F:Alex and a TV Show (bitset & 莫比乌斯容斥)的更多相关文章

  1. Codeforces 1097F Alex and a TV Show (莫比乌斯反演)

    题意:有n个可重集合,有四种操作: 1:把一个集合设置为单个元素v. 2:两个集合求并集. 3:两个集合中的元素两两求gcd,然后这些gcd形成一个集合. 4:问某个可重复集合的元素v的个数取模2之后 ...

  2. Codeforces 1097F. Alex and a TV Show

    传送门 由于只要考虑 $\mod 2$ 意义下的答案,所以我们只要维护一堆的 $01$ 容易想到用 $bitset$ 瞎搞...,发现当复杂度 $qv/32$ 是可以过的... 一开始容易想到对每个集 ...

  3. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  4. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  5. Codeforces 1097 Alex and a TV Show

    传送门 除了操作 \(3\) 都可以 \(bitset\) 现在要维护 \[C_i=\sum_{gcd(j,k)=i}A_jB_k\] 类比 \(FWT\),只要求出 \(A'_i=\sum_{i|d ...

  6. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

  7. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  8. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  9. Codeforces Round #330 (Div. 2)B. Pasha and Phone 容斥

    B. Pasha and Phone   Pasha has recently bought a new phone jPager and started adding his friends' ph ...

随机推荐

  1. Let's Encrypt 免费通配符 SSL 证书申请教程——但是也需要email,域名所有权等,如果是黑产用的话会这样用吗?会不会暴露自己身份???

    Let's Encrypt 免费通配符 SSL 证书申请教程 from:https://blog.csdn.net/English0523/article/details/79608464 2018 ...

  2. [转]find+xargs+sed批量替换

    写代码时经常遇到要把 .c  和 .h的文件中的某些内容全部替换的情况,用sourceinsight 进行全局的查找是一个方法,但是sourceinsight只能替换一个文件中的字符串,不能同时替换多 ...

  3. js--事件冒泡-捕获

    什么是事件流: 事件流描述的是从页面中接受事件的顺序,但有意思的是,微软(IE)和网景(Netscape)开发团队居然提出了两个截然相反的事件流概念, IE的事件流是事件冒泡流(event bubbl ...

  4. windbg 定位崩溃问题

    三板斧如下: 使用windbg打开dump文件,设置好对应进程的 pdb 文件(这个很关键.为了避免releas优化导致符号文件错乱,我发布的所有                      relea ...

  5. Win10系列:VC++ Direct3D图形绘制1

    通过前面的介绍,相信读者已经了解了如何新建一个用于开发Direct3D应用程序的项目模版,以及这个项目模版中用于绘制立体图形的主要函数.在本小节中,将通过一个具体的示例来介绍如何使用Visual St ...

  6. django搭建博客

    https://andrew-liu.gitbooks.io/django-blog/content/index.html

  7. 7.6 C++基本序列式容器效率比较

    参考:http://www.weixueyuan.net/view/6403.html 总结: 对于vector而言,它只是一个可以伸缩长度的数组 对于deque而言,它是一个可以操作头部和尾部的并且 ...

  8. Office 365 - For security reasons DTD is prohibited in this XML document

    博客地址:http://blog.csdn.net/FoxDave 今天在测试东西的时候发现在本机运行CSOM代码或使用Office 365 PowerShell时,出现了如下错误: Connec ...

  9. C#中三层架构UI、BLL、DAL、Model实际操作

    三层架构分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL)再加上实体类库(Model) 转载请注明出自朱朱家园https://blog.csdn.net/zhgl7688 1.实体类库( ...

  10. MongoDB的安装及安装为windows服务

    首先在网上找了一篇教程,于是按着做,下载了最新版的安装包,解压以后目录是这样的: 没有bin目录啊 原因:zip没有经过编译 于是下载安装包,安装包是msi 一直按照next提示就可以了,但是记得选择 ...