codeforces #305 C Mike and Foam
首先我们注意到ai<=50w
因为2*3*5*7*11*13*17=510510
所以其最多含有6个质因子
我们将每个数的贡献分离,
添加就等于加上了跟这个数相关的互素对
删除就等于减去了跟这个数相关的互素对
问题转化为了求跟某个数相关的互素对的数目
我们可以用容斥来解决
即加上至少跟这个数有0个公共质因子的数
减去至少跟这个数有1个公共质因子的数
加上至少跟这个数又2个公共质因子的数……
这样我们就可以在2^6的时间算出答案了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std; typedef long long LL;
const int maxn=200010;
int n,m,x,tot;
int a[maxn];
bool check[maxn];
vector<int>V[maxn];
int p[500010],cnt=0;
bool vis[500010];
int Num[500010];
LL ans=0; void Get_Prime(){
for(int i=2;i<=500000;++i){
if(!vis[i])p[++cnt]=i;
for(int j=1;j<=cnt;++j){
if(1LL*p[j]*i>500000)break;
vis[p[j]*i]=true;
if(i%p[j]==0)break;
}
}return;
}
void push(int now){
int x=a[now],lim=(int)(sqrt(x));
for(int i=1;i<=cnt;++i){
if(p[i]>lim)break;
if(x%p[i]==0){
V[now].push_back(p[i]);
while(x%p[i]==0)x/=p[i];
if(x==1)break;
}
}
if(x>1)V[now].push_back(x);
}
void add(int now){
int k=V[now].size();
for(int i=0;i<(1<<k);++i){
int bit=0,o=1;
for(int j=0;j<k;++j)if(i>>j&1)bit++,o*=V[now][j];
if(bit&1)ans-=Num[o];
else ans+=Num[o];
Num[o]++;
}return;
}
void del(int now){
int k=V[now].size();
for(int i=0;i<(1<<k);++i){
int bit=0,o=1;
for(int j=0;j<k;++j)if(i>>j&1)bit++,o*=V[now][j];
Num[o]--;
if(bit&1)ans+=Num[o];
else ans-=Num[o];
}return;
} int main(){
scanf("%d%d",&n,&m);
Get_Prime();
for(int i=1;i<=n;++i)scanf("%d",&a[i]),push(i);
for(int i=1;i<=m;++i){
scanf("%d",&x);
if(check[x])del(x),check[x]=false;
else add(x),check[x]=true;
cout<<ans<<endl;
}return 0; }
codeforces #305 C Mike and Foam的更多相关文章
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- codeforces #305 A Mike and Frog
挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...
- codeforces #305 B Mike and Feet
跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...
- codeforces #305 D Mike and Fish
正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...
- codeforces #305 E Mike and friends
原问题可以转化为:给定第k个字符串,求它在L-R的字符串里作为子串出现了多少次 定义子串为字符串的某个前缀的某个后缀(废话) 等价于我们把一个字符串插入到trie里,其过程中每个经过的节点和其向上的f ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 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// Mike and Foam// Codeforces Round #305(Div. 1)
题意:给出数组arr和一个空数组dst.从arr中取出一个元素到dst为一次操作.问每次操作后dst数组中gcd等于1的组合数.由于数据都小于10^6,先将10^6以下的数分解质因数.具体来说从2开始 ...
随机推荐
- OSG相关扩展工程
https://blog.csdn.net/wang15061955806/article/details/51003803 OSG的相关扩展,OSG针对每个特定应用,也有很多的开发者进行开发和完善, ...
- Java不区分大小写的CaseInsensitiveMap
Java中对于键值对,我们习惯使用类HashMap,使用方式:Map<String, String> result=new HashMap<String,String>(); ...
- Python实现代码统计工具——终极加速篇
Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...
- Phoenix 5.0 hbase 2.0 org.apache.hadoop.security.authentication.util.KerberosUtil.hasKerberosKeyTab
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- CTF的一道安卓逆向
前几天打CTF时遇到的一道安卓逆向,这里简单的写一下思路 首先用jadx打开apk文件,找到simplecheck处(文件名是simplecheck),可以看到基本逻辑就是通过函数a对输入的内容进行判 ...
- js模拟发送 FormData数据
后台express需要connect-multiparty模块接收formData的数据类型 class ourFormData { constructor(data, rs) { return ne ...
- Node.js的进程管理
众所周知Node基于V8,而在V8中JavaScript是单线程运行的,这里的单线程不是指Node启动的时候就只有一个线程,而是说运行JavaScript代码是在单线程上,Node还有其他线程,比如进 ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- xilink 烧写flash
no 右键
- hdu 1045
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) Me ...