hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
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 (容斥原理)的更多相关文章
- 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个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
- 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 ...
- E. Mike and Foam 容斥原理
http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] , a[id2]互质的对数. 询问 ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- 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 547E Mike and Friends
codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
随机推荐
- jsp之EL表达式
1.null值 null值会用""进行显示 2.隐式对象 1).pageScope.requestScope(相当于request).sessionScope(相当于session ...
- echarts地图点定位的问题
1,生成地图 2,如果需要产生地图上的点位,需要在配置中传入geoCoord具体数据为一下: { "海门": [121.15, 31.89], "鄂尔多斯": ...
- UVa 11019 (AC自动机 二维模式串匹配) Matrix Matcher
就向书上说得那样,如果模式串P的第i行出现在文本串T的第r行第c列,则cnt[r-i][c]++; 还有个很棘手的问题就是模式串中可能会有相同的串,所以用repr[i]来记录第i个模式串P[i]第一次 ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
- Android 源代码自动编译packages/apps
/*************************************************************************** * Android 源代码自动编译packag ...
- Android Traceroute 功能实现
经常在windows下开发网络功能的人 经常会使用的命令就是tracert .而实际上 在app开发中,我们也经常要碰到类似的情况.比如你的app 出现了问题,你总不能让用户想办法 去tracert吧 ...
- mysql创建/删除表的例子
创建表 简单的方式 CREATE TABLE person ( number INT(11), name VARCHAR(255), birthday DATE ); 或者是 CREATE TABLE ...
- MySQL与Oracle 差异比较之七其它
其它 编号 类别 ORACLE MYSQL 注释 1 内连接的更改 1.select a.*, b.*, c.*, d.* from a, b, c, d where a.id = b.id a ...
- Javascript中的Keycode值列表
关于如何得到一个键在Javascript中的Keycode值,可以参考: <body onkeypress=alert(event.keyCode)>请按任意键,你将得到该键的键值! ke ...
- socket基础实例(一个服务端对应一个客户端情形)
服务端处理1个客户端的例子 运行结果: (1) while(accept+if(recv)) 情形 执行服务端进程: [root@localhost single_link]# ./server [s ...