Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered
from 1to ni-th
kind of beer has ai milliliters
of foam on it.

Maxim is Mike's boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives him a number x.
If beer number x is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf.

After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (i, j) of
glasses in the shelf such that i < j and  where  is
the greatest common divisor of numbers aand b.

Mike is tired. So he asked you to help him in performing these requests.

Input

The first line of input contains numbers n and q (1 ≤ n, q ≤ 2 × 105),
the number of different kinds of beer and number of queries.

The next line contains n space separated integers, a1, a2, ...
, an (1 ≤ ai ≤ 5 × 105),
the height of foam in top of each kind of beer.

The next q lines contain the queries. Each query consists of a single integer integer x (1 ≤ x ≤ n),
the index of a beer that should be added or removed from the shelf.

Output

For each query, print the answer for that query in one line.

Sample test(s)
input
  1. 5 6
  2. 1 2 3 4 6
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 1
output
  1. 0
  2. 1
  3. 3
  4. 5
  5. 6
  6. 2
  1.  
  1. 题意:
  1. 输入n,m,然后输入n个数,之后是m次操作,每次操作输入一个下标i。下标i第一次出现,代表把数组第i个数放进架子中,第二次出现,代表取出来,
  1. 每次操作完之后。输出架子中的数字钟。有几个是互质的
  1.  
  1. 思路:
  1. 先取出全部质因子,这样能够大大降低计算时间,枚举质因子的过程时间消耗差点儿能够忽略不计
  1. 然后使用质因子得到其它因子,而且记录个数来计算最后的值
  1.  
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stack>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <math.h>
  10. #include <bitset>
  11. #include <algorithm>
  12. #include <climits>
  13. using namespace std;
  14. #define LS 2*i
  15. #define RS 2*i+1
  16. #define UP(i,x,y) for(i=x;i<=y;i++)
  17. #define DOWN(i,x,y) for(i=x;i>=y;i--)
  18. #define MEM(a,x) memset(a,x,sizeof(a))
  19. #define W(a) while(a)
  20. #define gcd(a,b) __gcd(a,b)
  21. #define LL long long
  22. #define N 500005
  23. #define MOD 1000000007
  24. #define INF 0x3f3f3f3f
  25. #define EXP 1e-8
  26. #define lowbit(x) (x&-x)
  27. LL n,q;
  28. LL ans;
  29. bool vis[N];
  30. LL a[N],s[N];
  31. LL num[1000005],tot,cnt;
  32. //num[i]记录包括因子i的数的个数
  33. void _set(LL n)//求出质因子
  34. {
  35.     LL i;
  36.     tot = 0;
  37.     for(i = 2; i<=n/i; i++)
  38.     {
  39.         if(n%i==0)
  40.         {
  41.             s[tot++] = i;
  42.             while(n%i==0)
  43.                 n/=i;
  44.         }
  45.     }
  46.     if(n!=1)
  47.         s[tot++] = n;
  48. }
  49. LL _add()
  50. {
  51.     LL i,j,k;
  52.     LL ret = 0;
  53.     for(i = 1; i<(1<<tot); i++)//枚举状态
  54.     {
  55.         LL mul = 1,c = 0;
  56.         for(j = 0; j<tot; j++)
  57.         {
  58.             if((1<<j)&i)
  59.                 mul*=s[j],c++;
  60.         }
  61.         if(c%2) ret+=num[mul];//由于偶数个质数相乘得出的数量能依据奇数得到,所以採用奇数添加,偶数减去的方法来使得总数不变
  62.         else ret-=num[mul];
  63.         num[mul]++;
  64.     }
  65.     ans += (cnt-ret);//总数减去不互质的对数
  66.     cnt++;
  67.     return ans;
  68. }
  69. LL _sub()
  70. {
  71.     LL i,j,k;
  72.     LL ret = 0;
  73.     for(i = 1; i<(1<<tot); i++)
  74.     {
  75.         LL mul = 1,c = 0;
  76.         for(j = 0; j<tot; j++)
  77.         {
  78.             if((1<<j)&i)
  79.                 mul*=s[j],c++;
  80.         }
  81.         num[mul]--;
  82.         if(c%2) ret+=num[mul];
  83.         else ret-=num[mul];
  84.     }
  85.     cnt--;
  86.     ans -= (cnt-ret);
  87.     return ans;
  88. }
  89. int main()
  90. {
  91.     LL i,j,k,x;
  92.     cnt = ans = 0;
  93.     scanf("%I64d%I64d",&n,&q);
  94.     for(i = 1; i<=n; i++)
  95.         scanf("%I64d",&a[i]);
  96.     while(q--)
  97.     {
  98.         scanf("%I64d",&x);
  99.         _set(a[x]);
  100.         if(vis[x])
  101.         {
  102.             vis[x] = false;
  103.             printf("%I64d\n",_sub());
  104.         }
  105.         else
  106.         {
  107.             vis[x] = true;
  108.             printf("%I64d\n",_add());
  109.         }
  110.     }
  111.     return 0;
  112. }

Codeforces548E:Mike and Foam的更多相关文章

  1. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  3. E. Mike and Foam(容斥原理)

    E. Mike and Foam Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special sh ...

  4. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  5. Codeforces 548E Mike ans Foam (与质数相关的容斥多半会用到莫比乌斯函数)

    题面 链接:CF548E Description Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a sp ...

  6. Mike and Foam(位运算)

    English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...

  7. codeforces #305 C Mike and Foam

    首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...

  8. codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)

    题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...

  9. Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)

    题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...

随机推荐

  1. Mysql优化的方法

    一.表的优化: 1: 定长与变长分离 如 time.手机号等,每一单元值占的字节是固定的. 核心且常用字段,宜建成定长,放在一张表,查询速度会很快 而varchar, text,blob,这种变长字段 ...

  2. kibana-metric

    1. Visualize 新建图形 2. 选择图形类型 3. 选择索引 4. 设置metric参数 4.1 count 4.2 unique count 5. 保存图形

  3. Number Triangles

    题目描述 Consider the number triangle shown below. Write a program that calculates the highest sum of nu ...

  4. 收纳箱1号 | GitHub Pages部署静态网页的一点私货

    Static site 总结各种各有的 static site generator Jekyll 其实是一个 static site generator. 如果你去 Google 这个,会发现有很多总 ...

  5. 博客 | 基于Travis CI实现Hexo在Github和Coding的同步自动化部署

    文章目录 完成Hexo主题安装和配置 基于Travis CI实现同步部署 参考内容 相关链接 待补充 完成Hexo主题安装和配置 如果您还没有安装Hexo环境,请参考Hexo文档安装,也给出这样两篇博 ...

  6. mysql如何用jsp代码进行数据库备份

    mysql如何用jsp代码进行数据库备份 //导出 String mysql="mysqldump -uroot -proot --opt databasename > d:/test ...

  7. NDK安装教程 not a valid ndk directory -- Eclipse

    第一步:下载 Eclipse IDE for Java Developers http://www.eclipse.org/downloads/ 第二步:下载CDT http://www.eclips ...

  8. Python学习笔记——条件控制

    Python中的条件控制方式基本和C语言类似,主要有如下几种语法: If条件判断 Python的条件语句的语法是if…elseif…else,如下的一个简单的猜数字的示例演示了这一过程: number ...

  9. IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,

    最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想到其中碰到不少问题,在网上搜索很久一直没有想到 ...

  10. sql索引从入门到精通(十亿行数据测试报告)

    原文:sql索引从入门到精通(十亿行数据测试报告) 导读部分 --------------------------------------------------------------------- ...