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点到每个点的最短路 ...
随机推荐
- wget 批量下载目录文件
wget -r -p -k -np http://源目录 ./本地目标目录
- [HIHO1322]树结构判定(并查集)
题目链接:http://hihocoder.com/problemset/problem/1322 给一个图,判断这个图是不是一棵树. 判定的方法:首先是连通图,其次所有点的入度都小于等于1. /* ...
- ssh传输出现encountered 1 errors during the transfer解决办法
以下方法简单排序,从简单到复杂: 法一:在SSH Secure File Transfer(上传文件那个),打开“Operation”菜单,打开“File Transfer Mode”子菜单,再选择“ ...
- HDU 1907 (博弈) John
参见上一篇博客,里面有分析和结论. #include <cstdio> int main() { int T; scanf("%d", &T); while(T ...
- Jquery源码中的Javascript基础知识(三)
这篇主要说一下在源码中jquery对象是怎样设计实现的,下面是相关代码的简化版本: (function( window, undefined ) { // code 定义变量 jQuery = fun ...
- smarty分页模板(用模板语法写分页)
分页是一个我们经常要用到的.比较基本的小功能,你可以通过定义一个方法或类来对它进行封装.重用.而本文则是通过利用smarty独有的语法,以模版的方式进行封装,从而达到同样的目的. 下面开始具体实现步骤 ...
- [Swift系列]002-基础语法
基础语法就那老几样,很快可以说完 [常量.变量] 1.变量用 var,系统自动去判断类型,但变量再次赋值需保持数据类型一致 var a=50 相信用过js/java/C#的,对这个var都不陌生 使 ...
- Jave 鼠标点击画太极 PaintTaiji (整理)
package demo; /** * Jave 鼠标点击画太极 PaintTaiji (整理) * 声明: * 又是一份没有注释的代码,而且时间已经久远了,不过代码很短,解读起来应该 * 不会很麻烦 ...
- HDU 1496 Train Problem I 火车问题1(桟,水)
题意: 给出两个串,串中的数字i 代表编号为i的火车进入车站的顺序,车站如桟一样,先进后出.第二个串是火车出站的顺序,问若按照第一个串那样进站,是否有可能如第二个串一样的出站顺序?火车顶多9辆,即1- ...
- Struts2的crud
struts2的crud引出的问题: 1.当Action里面有其他类的实例引用属性时,当要用请求参数为该对象的属性赋值时,如何将其压入栈顶. 2.当有的操作(list)不需要创建该属性的实例对象时,或 ...