题目链接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. vue-cli3使用yarn run build打包找不到路径

    vue-cli3使用yarn run build打包项目部署到服务器上面,运行空白 解决办法非常方便,直接创建vue.config.js 在vue.config.js中添加即可 再打包项目即成功

  2. Python-数据类型内置方法(2)

    目录 元组(tuple) 内置方法: 字典(dict) 内置方法: 优先掌握: 需要掌握 集合(set) 优先掌握 深浅拷贝 拷贝(赋值) 浅拷贝 深拷贝 总结 存值个数 有序 or 无序 可变 or ...

  3. 报javax.servlet.ServletException: Servlet.init() for servlet [springmvc] threw exception的解决记录

    1.异常详情: 2.异常分析: 从异常的详情中看出:companyService未找到,出现这种情况的愿意可能是companyServiceImpl类没有交给IOC容器管理,但是经过我已经在该类上打了 ...

  4. P1985 [USACO07OPEN]翻转棋

    题目链接: 翻转棋 题目分析: 先状压/\(dfs\)枚举第一排状态,然后在每个\(1\)下面翻,即确定了第一排就确定了后面的状态 最后验证一下最后一排是不是全0即可 代码: #include< ...

  5. 深入浅出 Java Concurrency (37): 并发总结 part 1 死锁与活跃度[转]

    死锁与活跃度 前面谈了很多并发的特性和工具,但是大部分都是和锁有关的.我们使用锁来保证线程安全,但是这也会引起一些问题.   锁顺序死锁(lock-ordering deadlock):多个线程试图通 ...

  6. 深入浅出 Java Concurrency (24): 并发容器 part 9 双向队列集合 Deque[转]

    有一段时间没有更新了.接着上节继续吧. Queue除了前面介绍的实现外,还有一种双向的Queue实现Deque.这种队列允许在队列头和尾部进行入队出队操作,因此在功能上比Queue显然要更复杂.下图描 ...

  7. iOS之CGcontext.h方法和属性简介

    /* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...

  8. iOS之CATiledLayer的属性简介和使用

    1.CATiledLayer简介 CATiledLayer用于大型图片进行分割显示,需要显示的图片才会加载,直接上代码: - (void)viewDidLoad { [super viewDidLoa ...

  9. Python2.7版本:定义类时为什么要继承object类?

    ********此答案摘自知乎,且经过自己实际运行后得出******** 继承 object 类的是新式类,不继承 object 类的是经典类 例子: 新式类: 经典类: B.C 是 A 的子类,D ...

  10. Spring注解驱动(下)

    9.@PropertySource 加载配置文件 在xml中 我们加载property配置文件,是使用 <context:property-placeholder location=" ...