题目链接Hdu4135

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1412    Accepted Submission(s): 531

Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
 
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
 
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
 
Sample Input
2
1 10 2
3 15 5
 
Sample Output
Case #1: 5
Case #2: 10

Hint

In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

题意:求区间[a, b]内与n互质的数的个数。

思路:如果某个数与n互质,那么这个数一定和n没有公共因子。所以题目就转化为有多少个数与n无公共因子。

可以通过求区间内有多少个数与n存在公共因子来得到答案。筛去2的倍数,3的倍数,5的倍数。。。容斥就可以啦。

Accepted Code:

 /*************************************************************************
> File Name: 4135.c
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月05日 星期五 16时33分45秒
> Propose:
************************************************************************/
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
/*Let's fight!!!*/ typedef long long LL; LL gcd(LL a, LL b) {
if (!b) return a;
return gcd(b, a % b);
} LL cal(const vector<int> &var, const LL &n) {
LL sz = var.size(), res = ;
for (LL i = ; i < (<<sz); i++) {
int num = ;
for (LL j = i; j != ; j >>= ) if (j & ) num++;
LL lcm = ;
for (LL j = ; j < sz; j++) {
if ((i >> j) & ) lcm = lcm / gcd(lcm, var[j]) * var[j];
if (lcm > n) break;
}
if (num % == ) res -= n / lcm;
else res += n / lcm;
} return res;
} int main(void) {
ios::sync_with_stdio(false);
int T, cas = ;
cin >> T;
while (T--) {
LL a, b, n, x;
cin >> a >> b >> n; vector<int> var;
x = n;
for (int i = ; i * i <= x; i++) {
if (x % i == ) {
var.push_back(i);
while (x % i == ) x /= i;
}
}
if (x > ) var.push_back(x); LL res = b - a + - cal(var, b) + cal(var, a - );
cout << "Case #" << cas++ << ": " << res << endl;
} return ;
}

题目链接Hdu1796

How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4205    Accepted Submission(s): 1198

Problem Description
  Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
Input
  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
Output
  For each case, output the number.
Sample Input
12 2
2 3
Sample Output
7
和上题一样。。。
Accepted Code:
/*************************************************************************
> File Name: 1796_dfs.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月06日 星期六 08时28分01秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ typedef long long LL;
int a[], n, m; LL gcd(LL a, LL b) {
if (!b) return a;
return gcd(b, a % b);
} void dfs(LL now, int num, LL lcm, LL &res) {
lcm = lcm / gcd(lcm, a[now]) * a[now];
if (num % == ) res -= n / lcm;
else res += n / lcm;
for (int i = now + ; i < m; i++)
dfs(i, num + , lcm, res);
} int main(void) {
ios::sync_with_stdio(false);
while (cin >> n >> m) {
int cnt = ;
for (int i = ; i < m; i++) {
int x;
cin >> x;
if (x > ) a[cnt++] = x;
}
m = cnt;
LL res = ;
n--;
for (int i = ; i < m; i++) {
dfs(i, , a[i], res);
} cout << res << endl;
}
} //位运算实现
/*************************************************************************
> File Name: 1796.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月05日 星期五 21时32分48秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ typedef long long LL;
int a[]; LL gcd(LL a, LL b) {
if (!b) return a;
return gcd(b, a % b);
} int main(void) {
ios::sync_with_stdio(false);
LL n, m;
while (cin >> n >> m) {
int d = ;
for (int i = ; i < m; i++) {
int x;
cin >> x;
if (x > && x <= n) a[d++] = x;
} n--;
LL res = ;
for (LL i = ; i < ( << d); i++) {
int num = ;
for (LL j = i; j != ; j >>= ) num += j & ;
LL lcm = ;
for (LL j = ; j < d; j++) {
if ((i >> j) & ) {
lcm = lcm / gcd(lcm, a[j]) * a[j];
if (lcm > n) break;
}
}
if (num % == ) res -= n / lcm;
else res += n / lcm;
} cout << res << endl;
}
return ;
}

容斥原理学习(Hdu 4135,Hdu 1796)的更多相关文章

  1. HDU 4135 容斥

    问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...

  2. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  3. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  4. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  5. hdu 4135 Co-prime (素数打表+容斥原理)

    题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...

  6. HDU 4135 容斥原理

    思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...

  7. HDU 4135

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...

  8. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

  9. hdu 4135 a到b的范围中多少数与n互质(容斥)

    Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...

随机推荐

  1. Luogu P2822 组合数问题(前缀和)

    P2822 组合数问题 题意 题目描述 组合数\(C_n^m\)表示的是从\(n\)个物品中选出\(m\)个物品的方案数.举个例子,从\((1,2,3)\)三个物品中选择两个物品可以有\((1,2), ...

  2. 开发函数计算的正确姿势 —— 使用 ROS 进行资源编排

    前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...

  3. 扩展IEnumerable<T> ForEach()方法

      相信很多人,在用Linq时,都会困惑为什么IEnumerabel<T>没有ForEach,虽然 我们一样可以这样写,很快读写 foreach(item in items) { Cons ...

  4. spring boot 中 rabbit mq基础例子

    1.先安装ELANG,再按照RabbitMQ 2.打开RabbitMQ控制台:rabbit command prompt 1.设置elang的路径:set ERLANG_HOME=D:\work_pr ...

  5. python 将值相同的key分组的方法

    方法一: 使用 itertools.groupby() rows = [ {'address': '5412 N CLARK ', 'date ': '07/12/2012 ’ }, {'addres ...

  6. 11-3-while

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. conan本地安装包

    在远程有的情况下,本地没有的情况下,执行命令 conan install package/版本号@usr/stable ,比如conan install opencv/3.4.3@seal/stabl ...

  8. NoSQL与关系数据库的比较

  9. 嘴巴题6 BZOJ3450JoyOI1952 Easy

    Time Limit: 10 Sec Memory Limit: 128 MB Submit: 936 Solved: 698 [Submit][Status][Discuss] Descriptio ...

  10. Elasticsearch 5.6.4 window 安装并简单使用head

    1.现在elasticsearch安装包 https://www.elastic.co/downloads/elasticsearch 2.解压elasticsearch-5.6.4.zip 到需要安 ...