Codeforces548E:Mike and Foam
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 n. i-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.
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.
For each query, print the answer for that query in one line.
5 6
1 2 3 4 6
1
2
3
4
5
1
0
1
3
5
6
2
题意:
输入n,m,然后输入n个数,之后是m次操作,每次操作输入一个下标i。下标i第一次出现,代表把数组第i个数放进架子中,第二次出现,代表取出来,
每次操作完之后。输出架子中的数字钟。有几个是互质的
思路:
先取出全部质因子,这样能够大大降低计算时间,枚举质因子的过程时间消耗差点儿能够忽略不计
然后使用质因子得到其它因子,而且记录个数来计算最后的值
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x) LL n,q;
LL ans;
bool vis[N];
LL a[N],s[N];
LL num[1000005],tot,cnt;
//num[i]记录包括因子i的数的个数 void _set(LL n)//求出质因子
{
LL i;
tot = 0;
for(i = 2; i<=n/i; i++)
{
if(n%i==0)
{
s[tot++] = i;
while(n%i==0)
n/=i;
}
}
if(n!=1)
s[tot++] = n;
} LL _add()
{
LL i,j,k;
LL ret = 0;
for(i = 1; i<(1<<tot); i++)//枚举状态
{
LL mul = 1,c = 0;
for(j = 0; j<tot; j++)
{
if((1<<j)&i)
mul*=s[j],c++;
}
if(c%2) ret+=num[mul];//由于偶数个质数相乘得出的数量能依据奇数得到,所以採用奇数添加,偶数减去的方法来使得总数不变
else ret-=num[mul];
num[mul]++;
}
ans += (cnt-ret);//总数减去不互质的对数
cnt++;
return ans;
} LL _sub()
{
LL i,j,k;
LL ret = 0;
for(i = 1; i<(1<<tot); i++)
{
LL mul = 1,c = 0;
for(j = 0; j<tot; j++)
{
if((1<<j)&i)
mul*=s[j],c++;
}
num[mul]--;
if(c%2) ret+=num[mul];
else ret-=num[mul];
}
cnt--;
ans -= (cnt-ret);
return ans;
} int main()
{
LL i,j,k,x;
cnt = ans = 0;
scanf("%I64d%I64d",&n,&q);
for(i = 1; i<=n; i++)
scanf("%I64d",&a[i]);
while(q--)
{
scanf("%I64d",&x);
_set(a[x]);
if(vis[x])
{
vis[x] = false;
printf("%I64d\n",_sub());
}
else
{
vis[x] = true;
printf("%I64d\n",_add());
}
} return 0;
}
Codeforces548E:Mike and Foam的更多相关文章
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- 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 ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- 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 ...
- Mike and Foam(位运算)
English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...
- codeforces #305 C Mike and Foam
首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...
- codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)
题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...
- Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
随机推荐
- fork+exec 与system,popen区别
1.fork + exec fork用来创建一个子进程.一个程序一调用fork函数,系统就为一个新的进程准备了前述三个段,首先,系统让新的进程与旧的进程使用同一个代码段,因为它们的程序还是相同的,对于 ...
- mysql中数据类型的长度
这是一篇很全面的博客的网址 http://qimo601.iteye.com/blog/1622368 下图是其中一部分截图,在mysql数据库中新建数据表时有个长度的设置
- mysq 中 information_schema 库
information_schema这个库,这个在mysql安装时就有了,提供了访问数据库元数据的方式.那什么是元数据库呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时 ...
- html5进阶之canvas图像基础
1.首先还是使用之前讲过的Image()函数来预加载图像. 在调用图像之前,需创建一个事件监听器,为其保证图像已经正确的加载. 如下图: 监听图片已经正确加载 2.把图像显示在画布上面,这里将不再使用 ...
- [scrapy] exceptions.TypeError:XXX is not json serializable
原因是spider获取items.py中定义的字段的时候,忘记extract()了 def parseItem(self,response): sel = Selector(response) ite ...
- R 语言词云wordcloud
来源:http://blog.chinaunix.net/uid-25135004-id-4311592.html wordcloud函数--用于绘制词云图 用法: wordcloud(words,f ...
- ASP.NET webFrom
web窗体的后缀名.aspx 1.<% %> 标签内的代码在服务器上执行 <body> <form id="form1" runat="se ...
- JDBC二部曲之_事物、连接池
事务 事务概述 事务的四大特性(ACID) 事务的四大特性是: l 原子性(Atomicity):事务中所有操作是不可再分割的原子单位.事务中所有操作要么全部执行成功,要么全部执行失败. l 一致 ...
- centos 部署web项目
Linux下安装Tomcat服务器和部署Web应用 一.上传Tomcat服务器
- [sharepoint]文档库,文件夹授权
写在前面 在项目中用到了文档库授权的方法,这里将查询到的方式总结一下. 涉及到的方法 在逻辑中用到的方法. /// <summary> /// 获取sharepoint站点角色定义 res ...