容斥原理学习(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 ...
随机推荐
- golang 高效字符串拼接
https://blog.csdn.net/u012210379/article/details/45110705 虽然方便,但是使用+=操作符并不是在一个循环中往字符串末尾追加字符串最有效的方式,一 ...
- Extjs & Ext.net中的一些属性
Extjs & Ext.Net 弹出整个浏览器对话框的方法 top.Ext.Msg.alert("值"); top.Ext.Msg.confirm("值" ...
- [Baltic2009]beetle【区间Dp】
Online Judge:Bzoj1761 Label:区间Dp 题目描述 在一条直线上有N个点,每个点M升水. 一个虫子在坐标轴0点上,它每个单位时间移动一格,每个点的水每单位时间消失1升. 问虫子 ...
- ie中报错---不能执行已释放 Script 的代码
我的原因:使用jquery.colorbox.js.在页面中使用弹框,对于父页面中的变量进行修改(弹框页面的js--window.parent.obj.arr= 数组;), 当弹框关闭之后,在父页面中 ...
- python相关软件安装流程图解————————python安装——————python-3.7.1-amd64
首先查看自己的系统版本 是32位的还是64位的 https://www.python.org/downloads/windows/ —————————python下载安装 开始———————————— ...
- 正则获取html标签字符串中图片地址
html标签字符串: var htmlStr = "<div class='testClass'><img=http://www.chinanews.com/part/ho ...
- 一个Js开发者学习Python的第一天
原文地址:小寒的博客 https://www.dodoblog.cn/blogs/5bf6b8fa0c09883d0f8aad13 作为一个有着足足两年半学习经验和一年半开发经验的js开发者,看着js ...
- Ionic跳转到外网地址
1.安装插件 https://github.com/apache/cordova-plugin-inappbrowser 执行命令:cordova plugin add org.apache.cord ...
- DBMS的四大特性
- JS的第七种语言类型--symbol
今天浏览网页的时候发现,JS中有七种语言类型.我的内心???百度一下哪里来的第七种!! 好吧跟着来回顾一下JS的前6种undefined null boolean string numver obje ...