容斥原理学习(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 ...
随机推荐
- [NOIP2019模拟赛]序列(Sequence)
题目大意 有一个序列$A_i$ • 对于 i ≥ 1,如果有$ A_i > 0.A_{i+1}> 0$ 且存在 $A_{i+2}$,那么法老可以令$ Ai$ 和 $A_{i+1}$ 减一, ...
- Ubuntu GitHub操作——分支、合并与标签
分支 分支是用来将特性开发绝缘开来的.在你创建仓库的时候,master 是"默认的"分支.在其他分支上进行开发,完成后再将它们合并到主分支上. 创建一个叫做"featur ...
- 《你不知道的javascript》上卷笔记整理(一)
函数声明和变量声明都会被提升,但函数声明会被提升到普通变量前,而 var foo = function bar(){}; 赋值操作不会被提升. 闭包: 基于词法作用域(作用域是根据名称查找变量的一套规 ...
- 深入浅出 Java Concurrency (30): 线程池 part 3 Executor 生命周期[转]
我们知道线程是有多种执行状态的,同样管理线程的线程池也有多种状态.JVM会在所有线程(非后台daemon线程)全部终止后才退出,为了节省资源和有效释放资源关闭一个线程池就显得很重要.有时候无法正确的关 ...
- WhaleCTF之隐写-Find
WhaleCTF之隐写-Find 前往题目 图片保存到本地,用Stegsolve打开图片 找到二维码 用微信或qq扫描,得到flag~
- 分布式锁的Redis实现
当我们开始开发项目部署运行时,项目规模不大,只是在一个JVM实例中运行,对同一资源的并发访问用JDK自带的锁机制就可以解决资源同时访问的问题.而随着项目的不断发展,单体应用已经无法满足日益增长的访问需 ...
- java基础之DateFormat类
DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...
- python3-常用模块之time
import time time模块主要是处理各种类型的时间 常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行,单位为秒. 2.time.time() 获取当前时间戳 时间戳 ...
- sqlmap:入门(手工注入)
一. 联合查询注入union(less-1) 1. union操作符用于合并两个或多个select语句结果集: 2. union后的select语句必须拥有和最前的select语句拥有相同数量的字段, ...
- 2018-10-31-win10-uwp-使用-asp-dotnet-core-做图床服务器客户端
title author date CreateTime categories win10 uwp 使用 asp dotnet core 做图床服务器客户端 lindexi 2018-10-31 14 ...