hdu4135

求[L,R]范围内与N互质的数的个数。

分别求[1,L]和[1,R]和n互质的个数,求差。

利用容斥原理求解。

二进制枚举每一种质数的组合,奇加偶减。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int N = ; int fac[N], cnt; void factor(int n) {
cnt = ;
int limit = sqrt(n); for (int i = ; i <= limit; ++i) {
if (n % i == ) fac[cnt++] = i;
while (n % i == ) n /= i;
}
if (n > ) fac[cnt++] = n;
} ll solve(ll x, int n) { //1~x中与n互质的数
ll ans = ;
ll val;
int num;
//printf("cnt=%d\n", cnt); int st = <<cnt;
for (int i = ; i < st; ++i) {// 枚举所有情况,奇加偶减
val = ;
num = ;
for (int j = ; j < cnt; ++j) {
if (i & (<<j)) {
val *= fac[j];
num++;
}
}
//printf(">>%d %lld %d\n", i, val, num);
if (num & ) ans += x/val;
else ans -= x/val;
}
return x - ans;
} int main()
{
ll x, y;
int n;
int t, cas=; cin >> t;
while (t--) {
cin >> x >> y >> n;
cout << "Case #" << ++cas << ": ";
factor(n);
cout << solve(y,n) - solve(x-,n) << endl;
} return ;
}

Codeforces 547C Mike and Foam

差不多啦

求新加入的数和已经存在的数有多少是不互质的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath> using namespace std; const int N = ;
int a[N], vis[N], fac[N], tfac[N];
int n, m, q, tot; void getfactor(int x, int fac[], int &cnt) {
int limit = sqrt(x);
cnt = ;
for (int i = ; i <= limit; ++i) {
if (x % i == ) {
fac[cnt++] = i;
while (x % i == ) x /= i;
}
}
if (x > ) fac[cnt++] = x;
} int add(int x, int d) {
int cnt;
getfactor(x, tfac, cnt);
int st = <<cnt;
int ans = ;
for (int i = ; i < st; ++i) {
int num = ;
int tmp = ;
for (int j = ; j < cnt; ++j) {
if (i & (<<j)) {
num++;
tmp *= tfac[j];
}
}
if (d == -) fac[tmp]--;
if (num & ) ans += fac[tmp];
else ans -= fac[tmp];
if (d == ) fac[tmp]++;
}
if (d == -) return -tot+ans;
else return tot--ans;
} int main()
{
while (~scanf("%d%d", &n, &m)) {
memset(vis, , sizeof vis);
tot = ;
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
long long ans = ,tmp;
while (m--) {
scanf("%d", &q);
if (vis[q]) vis[q] = , tot--, tmp = add(a[q], -);
else vis[q] = , tot++, tmp = add(a[q], );
ans += tmp;
printf("%lld\n", ans);
}
}
return ;
}

hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)的更多相关文章

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

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

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

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

  3. 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 ...

  4. E. Mike and Foam 容斥原理

    http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] ,  a[id2]互质的对数. 询问 ...

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

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

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

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

  7. 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 ...

  8. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

  9. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

随机推荐

  1. JS 原型链学习总结

    废话篇: 在js的学习过程中有一大难点就是原型链.学习的时候一直对这一内容不是十分的明白.纠结的我简直难受.,幸好总算给他弄通了,哇咔咔,总算可以不用在睡梦中还想着他了. 正文篇: 要了解原型链我们首 ...

  2. hdu1051(LIS | Dilworth定理)

    这题根据的Dilworth定理,链的最小个数=反链的最大长度 , 然后就是排序LIS了 链-反链-Dilworth定理 hdu1051 #include <iostream> #inclu ...

  3. Linux多线程(三)(同步互斥)

    1. 线程的同步与互斥 1.1. 线程的互斥 在Posix Thread中定义了一套专门用于线程互斥的mutex函数.mutex是一种简单的加锁的方法来控制对共享资源的存取,这个互斥锁只有两种状态(上 ...

  4. Android Fragment 真正的完全解析(上) (转载)

    原处: http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragmen ...

  5. Uva 10382 (区间覆盖) Watering Grass

    和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理) 学习一下别人的代码,练习使用STL的vector容器 这里有个小技巧,用一个微小量EPS来弥补 ...

  6. hdu4604 deque

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 思路:就是模拟一下,求每一个开始的非上升和非下降序列.然后求重复的数,由于求出来可能不会是我们想 ...

  7. Phpstorm Xdebug Web程序调试

    平时调试php程序的时候,可以通过在代码中添加var_dump等函数来实现简单的断点调试. 下面介绍另一种方法,通过Phpstorm和Xdebug来进行调试. 1.下载Xdebug 这个是官网下载地址 ...

  8. BZOJ 1724 切割木板

    合并果子. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  9. Heritrix源码分析(十) Heritrix中的Http Status Code(Http状态码)(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/649737       本博客已迁移到本人独立博客: http://www.yun5u ...

  10. UIColor,CGColor,CIColor三者的区别和联系

    UIColor,CGColor,CIColor三者的区别和联系((转)) 最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清 ...