codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)
题意:给出数组arr和一个空数组dst。从arr中取出一个元素到dst为一次操作。问每次操作后dst数组中gcd等于1的组合数。
由于数据都小于10^6,先将10^6以下的数分解质因数。具体来说从2开始,将2的倍数全部加2因子(用的vector),3的倍数加3因子。4不是质数,它的倍数不加因子。
还要一个cnt数组记录dst中有几个数是数组下标的倍数。
在放入元素x到dst数组,对于它的每个质因数及质因数间的乘积,看cnt中的量。组合数的增量为dst的sz(size)-(cnt[x的质因数])(即dst中和x都有x的质因数,因此要减)+(cnt[x的两个质因数的乘积])......然后再对x的素因子及成绩在cnt上加1.
乱码:
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <list>
using namespace std;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon;
const double EPS=1e-;
vector<lon> fen[SZ];
bool used[SZ];
lon cnt[SZ]; void init(lon n)
{
for(int i=;i<n;++i)
{
if(fen[i].empty())
for(int j=i;j<n;j+=i)
{
fen[j].push_back(i);
}
}
} void add(vector<lon> &vct,bool type)
{
lon sz=vct.size();
for(lon i=;i<(<<sz);++i)
{
lon res=;
for(lon j=;j<;++j)
{
if(i&(<<j))
{
res*=vct[j];
}
}
if(type)++cnt[res];
else --cnt[res];
}
} lon work(vector<lon> &vct)
{
lon ans=;
lon sz=vct.size();
//cout<<" "<<sz<<endl;
for(lon i=;i<(<<sz);++i)
{
lon res=;
lon co=;
for(lon j=;j<;++j)
{
if(i&(<<j))
{
res*=vct[j];
co*=-;
}
}
ans+=co*cnt[res];
}
return ans;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon n,m;
cin>>n>>m;
vector<lon> vct(n);
for(int i=;i<n;++i)
{
cin>>vct[i];
} init(5e5+);
lon num=;
lon last=;
for(int i=;i<m;++i)
{
lon id;
cin>>id;
--id;
lon res=;
if(!used[id])
{
res+=work(fen[vct[id]]);
//cout<<" "<<res<<endl;
add(fen[vct[id]],);
used[id]=;
res=last+res+num;
}
else
{
res=last;
add(fen[vct[id]],);
lon val=work(fen[vct[id]]);
//cout<<" "<<val<<endl;
res-=num-+work(fen[vct[id]]);
//cout<<" "<<res<<endl;
used[id]=;
}
cout<<res<<endl;
if(used[id])++num;
else --num;
last=res;
}
return ;
}
codeforces 547c// Mike and Foam// Codeforces Round #305(Div. 1)的更多相关文章
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- Codeforces Round# 305 (Div 1)
[Codeforces 547A] #include <bits/stdc++.h> #define maxn 1000010 using namespace std; typedef l ...
- Codeforces Round #305 (Div. 2)
C. Mike and Frog 题意:有一只青蛙和一朵花,分别高度为h1.h2,每浇一次水,h1=(x1*h1+y1)mod m,h2=(x2*h2+y2)mod m.求最少浇多少次后h1=a1,h ...
- Codeforces Round #305 (Div. 2) E题(数论+容斥原理)
E. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- MyBatis学习笔记(三)——优化MyBatis配置文件中的配置
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的 ...
- Python2 和Python3 的差异总结
一.基本语法差异 1.1 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3 ...
- ubuntu14.04无法安装Curl,需要先升级sudo apt-get update
ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...
- java多线程----拒绝策略
本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略介绍 ...
- Finalize和Dispose的区别
https://www.cnblogs.com/Jessy/articles/2552839.html
- xml声明中的standalone属性
晚上,在测试tinyxml的时候,发现其中声明了<?xml version="1.0" standalone="no" ?>,经查,其含义为stan ...
- java安全体系之JCA、JCE、JAAS、JSSE及其关系
首先.如果是运行在internet上的系统,并且如果是个涉及到利益性的系统,不可避免的会遭受各种攻击(我们公司的很多系统从OS到DB到webapp就实时有收到攻击和破解),所以尽可能保证安全性将不再是 ...
- 使用 Vue.js 结合bootstrap 实现的分页控件
原文链接:http://blog.csdn.net/qiuhaotc/article/details/53031884 源码下载: http://pan.baidu.com/s/1i4XgH6H 密码 ...
- yum第三方安装-软件包没签名及更新错误
yum安装时 后面加 --nogpgcheck 阿里云源文件:http://mirrors.aliyun.com/repo/Centos-7.repo epel repo源:http://mirror ...
- AtCoder Tenka1 Programmer Beginner Contest 解题报告
赛时写了ABC,D实在没啥思路,然后C又难调...然后就从写完AB时的32名掉到了150+名 T_T 码力不够,思维不行,我还是AFO吧 比赛链接 A - Measure sb模拟,奇数串倒着输出偶数 ...