容斥原理学习(Hdu 4135,Hdu 1796)
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1412 Accepted Submission(s): 531Problem DescriptionGiven 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.InputThe 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).OutputFor 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 Input2
1 10 2
3 15 5Sample OutputCase #1: 5
Case #2: 10HintIn 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 ;
}
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): 1198Problem DescriptionNow 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.InputThere 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.OutputFor each case, output the number.Sample Input12 2
2 3Sample Output7
/*************************************************************************
> 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)的更多相关文章
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- [容斥原理] hdu 4135 Co-prime
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- hdu 4135 Co-prime (素数打表+容斥原理)
题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...
- HDU 4135 容斥原理
思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...
- HDU 4135
http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...
- 容斥 - HDU 4135 Co-prime
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...
- hdu 4135 a到b的范围中多少数与n互质(容斥)
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...
随机推荐
- cdq分治(偏序)
偏序问题: https://www.luogu.org/blog/Owencodeisking/post-xue-xi-bi-ji-cdq-fen-zhi-hu-zheng-ti-er-fen 优质题 ...
- [JZOJ4759] 【雅礼联考GDOI2017模拟9.4】石子游戏
题目 描述 题目大意 在一棵树上,每个节点都有些石子. 每次将mmm颗石子往上移,移到根节点就不能移了. 双方轮流操作,问先手声还是后手胜. 有三种操作: 1. 询问以某个节点为根的答案. 2. 改变 ...
- thinkphp 子查询
从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...
- Python-面向对象之高级进阶
目录 classmethod与staticmethod 面向对象高级 isinstance.issubclass 反射 魔法方法(类内置方法) 单例模式 classmethod与staticmetho ...
- DuiLib学习笔记3.颜色探究
在前面两篇日志已经能使用xml了.今天准备好好的折腾一番,结果在颜色上却掉坑里了. 起初我在ps里取颜色为0104ff 这里01为R,04为G,ff为B 在控件的属性里有这样一个属性bkcolor=& ...
- Java-MyBatis-MyBatis3-XML映射文件:缓存
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:缓存 1.返回顶部 1. 缓存 MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制. 为了使它更 ...
- 用区块链技术做一个 不可被修改的 恋爱记录 app 我叫<<誓言>>
区块链技术 具有不可篡改,去中心化,共识机制等优秀的特性, 都用来做 代币钱包什么的.我觉得完全是浪费. 我的想法是用哪个区块做一个dapp 1 里面写着每个人的恋爱记录,爱情宣言. 2 一个人一生 ...
- 力扣算法题—146LRU缓存机制
[题目] 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...
- Python学习day23-面向对象的编程
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- <随便写>创建文件批处理文件
创建一个text文档 写入想写的程序 将后缀改为bat 例如创建一个文件夹: 双击运行bat文件就可以创建文件夹 运行结果: 需要批量处理,就用for循环生成代码,粘贴上去就行了