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点到每个点的最短路 ...
随机推荐
- 采用RPC方式和document方式 开发Axis2的WebService客户端
import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.ax ...
- fixed兼容IE6
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【第四篇】说说layer的iframe弹窗给里面的标签赋值的问题
说到这一篇,真的是颠覆了我的思维. 官方文档,没有介绍这一部分的操作,大致上提了一下. 我的思路是把页面的数据传过去,在iframe弹窗的页面拿到接收到的数据,然后赋值,但是这样就会有个问题, 怎么传 ...
- factory工厂模式
工厂方法模式 工厂方法模式概述 工厂方法模式中抽象工厂类负责定义创建对象的接口,具体对象的创建工作由继承抽象工厂的具体类实现 简单理解: 与简单工厂模式类似,简单工厂模式是一个工厂,用户将条件为 ...
- DelegatingFilterProxy
安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的. 它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc), 所以它 ...
- HDU 5003 Osu!
解题思路:水题,不多说. #include<cstdio> #include<cstring> #include<algorithm> #include<cm ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- WebView增加一个水平Progress,位置、长相随意
实际效果可以参看微信的web页面进度条 本质就是通过addView及对WebView 页面进度进行监听 先看看这个自定义的DrawableId,我们参照系统默认实现的方法写一个自己的 <la ...
- UIScrollView 不能滚动的问题
uiscrollview是开发sdk自带的控件, 在使用的时候,发现滚动不了, 最常山见的原因是 contentSize 这个属性,比uiscrollview的frame要小...所以无需滚动,自然就 ...
- Linux makefile教程之条件判断六[转]
使用条件判断 —————— 使用条件判断,可以让make根据运行时的不同情况选择不同的执行分支.条件表达式可以是比较变量的值,或是比较变量和常量的值. 一.示例 下面的例子,判断$(CC)变量是否“g ...