【容斥原理】Codeforces547C[Mike and Foam]题解
来源:互联网 发布:韩国网络电视成人直播 编辑:程序博客网 时间:2024/06/05 08:37
题目概述
给出一个序列
解题报告
2017.10.30UPD:经过ChesterKing大神的指导,我发现这篇博客有一堆漏洞,十分抱歉Orz。
好像是不难的容斥题,但是我从来没有用过容斥原理QAQ,这里简单说明一下:
有
ps:这里的公式之前打错了,将下面的
证明的话画图yy一下就知道了:P……总之只要记住奇加偶减就行了:P。
我们需要求出的是容器中与
ps:这里的因子
继续观察我们会发现每个数的素因子个数最多只有
ps:这里的素因子个数之前打成了
解题报告
把
#include<cstdio>#include<cmath>using namespace std;const int maxn=200000,maxa=500000,maxp=6;typedef long long LL;int n,te,a[maxn+5],p[maxa+5];int lnk[maxn+5][maxp+5],Tot[maxa+5];bool pri[maxa+5],vis[maxn+5];LL ans;inline bool Eoln(char ch) {return ch==10||ch==13||ch==EOF;}inline char readc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); if (l==r) return EOF; else return *l++;}inline int readi(int &x){ int tot=0,f=1;char ch=readc(),lst='+'; while ('9'<ch||ch<'0') {if (ch==EOF) return EOF;lst=ch;ch=readc();} if (lst=='-') f=-f; while ('0'<=ch&&ch<='9') tot=(tot<<3)+(tot<<1)+ch-48,ch=readc(); return x=tot*f,Eoln(ch);}inline void writel(LL x){ static int len,buf[20];len=0;do buf[len++]=x%10,x/=10; while (x); for (int i=len-1;i>=0;i--) putchar(buf[i]+48);}void make_p(int n){ pri[0]=pri[1]=true;p[0]=0; for (int i=2;i<=n;i++) { if (!pri[i]) p[++p[0]]=i; for (int j=1;j<=p[0]&&i*p[j]<=n;j++) { pri[i*p[j]]=true; if (i%p[j]==0) break; } }}void Ask(int x,int fl){ for (int i=0;i<(1<<lnk[x][0]);i++) { int now=1,num=0; for (int j=1;j<=lnk[x][0];j++) if (i&(1<<j-1)) now*=lnk[x][j],num++; if (num&1) ans-=fl*Tot[now]; else ans+=fl*Tot[now]; }}void Update(int x,int fl){ for (int i=0;i<(1<<lnk[x][0]);i++) { int now=1; for (int j=1;j<=lnk[x][0];j++) if (i&(1<<j-1)) now*=lnk[x][j]; Tot[now]+=fl; }}int main(){ freopen("program.in","r",stdin); freopen("program.out","w",stdout); readi(n);readi(te);make_p(maxa); for (int i=1,x;i<=n;i++) { readi(x); for (int j=1,S=sqrt(x);j<=p[0]&&p[j]<=S;j++) if (x%p[j]==0) { lnk[i][++lnk[i][0]]=p[j]; while (x%p[j]==0) x/=p[j]; } if (x>1) lnk[i][++lnk[i][0]]=x; } while (te--) { int x;readi(x); if (!vis[x]) Ask(x,1),Update(x,1); else Update(x,-1),Ask(x,-1);vis[x]^=1; writel(ans);putchar('\n'); } return 0;}
阅读全文
0 0
- 【容斥原理】Codeforces547C[Mike and Foam]题解
- Mike and Foam - CodeForces #547 C 容斥原理
- CF 547 C Mike and Foam(容斥原理)
- CF 305 div2 E. Mike and Foam (容斥原理)
- E. Mike and Foam(容斥原理)
- codeforces548E Mike and Foam -- 容斥
- Codeforces Round #305 (Div. 2) E. Mike and Foam 容斥原理
- *Codeforces Round #305 (Div. 1) C. Mike and Foam(容斥原理)
- Codeforces #305 div2 E. Mike and Foam 数论 容斥原理
- Codeforces 547C Mike and Foam 容斥
- codeforces/#305 Div1/547/C Mike and Foam 【容斥】
- Codeforces 548E Mike and Foam(容斥)
- CodeForces 548E Mike and Foam (容斥+数论)
- 【Codeforces】547C Mike and Foam 容斥
- [Codeforces547C]Mike and Foam(莫比乌斯反演+组合数学)
- Codeforces 548E Mike and Foam (容斥+莫比乌斯反演)
- Code Forces 547 C. Mike and Foam(素因子分解+容斥)
- Codeforces Round #305 (Div. 1)C. Mike and Foam(素数+容斥)
- 职业生涯系列
- 图的基本存储的基本方式一
- 数组中出现次数超过一半的数字
- hdu--6153(A Secret)
- [转]写给Krpano小白们的最最最入门级教程(二)
- 【容斥原理】Codeforces547C[Mike and Foam]题解
- 8.22最短路径讲解
- spark(5)--spark模型中FIFO的实现
- hdu 2896 ac自动机
- lintcode——买卖股票的最佳时机
- 2017.08.22 今日头条笔试题
- MyBatis(1)--MyBatis入门示例
- Buggy Robot Gym
- Guava Cache源码解析