[hdu1695] GCD ——欧拉函数+容斥原理
题目
给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足:
- \(x \in [1, b]\), \(y \in [1, d]\) ;
- \(gcd(x, y) = k\)
HDU1695
题解
我们观察式子\(gcd(x,y)=k\)
很显然,\(gcd(x/k, y/k) = 1\)
我们令b < d,令x<y(避免重复计数)
分类讨论。
- y < b
可以看出答案就是\(\sum_{i \in [1, b]} \phi(i)\)
2)\(y \in [b, d]\)
可以看出答案就是calc(b, i),calc函数就是在区间[1,b]中与i互素的个数。
怎么计算calc函数呢?
首先我们计算出i的因数,运用容斥原理。
\]
具体计算见代码。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 100000;
int prime[maxn+5], phi[maxn+5], check[maxn+5];
int T, a, b, c, d, k;
int cnt = 0;
void get_phi(int n) {
memset(check, 0, sizeof(check));
cnt = 0;
phi[1] = 1;
for(int i = 2; i <= n; i++) {
if(!check[i]) {
phi[i] = i-1;
prime[cnt++] = i;
}
for(int j = 0; j < cnt; j++) {
if(i * prime[j] > n) break;
check[i*prime[j]] = 1;
if(i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
} else {
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
}
ll factor[maxn];
int ct = 0;
void get_factor(int x) {
ct = 0;
ll tmp = x;
for(int i = 0; prime[i] * prime[i] <= x; i++) {
if(tmp % prime[i] == 0) {
factor[ct] = prime[i];
while(tmp % prime[i] == 0) {
tmp /= prime[i];
}
ct++;
}
}
if(tmp != 1)
factor[ct++] = tmp;
}
int calc(int n, int m) {
get_factor(m);
int ans = 0;
for(int i = 1; i < (1 << ct); i++) {
int cnt = 0;
int tmp = 1;
for(int j = 0; j < ct; j++) {
if(i & (1 << j)) {
cnt++;
tmp *= factor[j];
}
}
if(cnt & 1) ans += n / tmp;
else ans -= n/tmp;
}
return n - ans;
}
int main() {
//freopen("input", "r", stdin);
scanf("%d", &T);
get_phi(maxn);
int kase = 0;
while(T--) {
kase++;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
if((k == 0) | (k > b) | (k > d)) {
printf("Case %d: 0\n", kase);
continue;
}
if(b > d) swap(b, d);
b /= k;
d /= k;
ll ans = 0;
for(int i = 1; i <= b; i++) ans += phi[i];
for(int i = b+1; i <= d; i++)
ans += calc(b, i);
printf("Case %d: %lld\n", kase, ans);
}
return 0;
}
[hdu1695] GCD ——欧拉函数+容斥原理的更多相关文章
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- hdu (欧拉函数+容斥原理) GCD
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...
随机推荐
- 手机连上同一个局域网的PC,修改项目的vhost配置
<VirtualHost 172.16.6.100:80> DocumentRoot "D:\phpStudy\WWW\mywork\many_school" Serv ...
- mysql8.0 忘记root密码
先打开一个cmd:net stop mysql //关闭mysql服务mysqld --shared-memory --skip-grant-tables//跳过登录密码在不关闭第一个CMD的情况下打 ...
- Triangular Sums 南阳acm122
Triangular Sums 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 The nth Triangular number, T(n) = 1 + … + n ...
- C# 中的正则简单例子
public static void Main() { Regex rgx = new Regex(@"[S|s]et-[C|c]ookie: (?<cookieName>\w+ ...
- 利用split方法计算字符串中出现字母最多的次数
最近练习一些简单的算法题,知道自己很不聪明,但却没想到用了这么久,划算不划算是个需要考虑的问题, 其中有个算法是:统计一个字符串出现最多的字母,网上很多自己的见解,但是才疏学浅,有些地方看的有点困难, ...
- 剑指Offer - 九度1508 - 把字符串转换成整数
剑指Offer - 九度1508 - 把字符串转换成整数2014-02-06 23:46 题目描述: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 输入: 输入可能包含多个测试样例 ...
- 《Cracking the Coding Interview》——第18章:难题——题目4
2014-04-29 01:05 题目:数数从0到n总共有多少个数字‘2’? 解法:数位动态规划,可以O(log10(n))时间内解决. 代码: // 18.4 Count the number of ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目2
2014-04-25 19:29 题目:对比一下哈希表和STL中的map的区别,哈希表如何实现?如果数据规模比较小,可以用什么来代替哈希表? 解法:哈希表可以理解为一堆桶,每个桶都有唯一的id,桶里可 ...
- 自己搭建php服务器(可接受表单提交,并返回页面)
0.概述 本demo实现以下功能: ①在html页面输入姓名和邮箱,点击提交(这里为get) ②服务器通过解析表单内容,返回对“姓名”和“邮箱”的一个欢迎页面 1.软件准备 ①xampp 作用:提供a ...
- 【APUE】Chapter11 Threads
看完了APUE第三版的Chapter11 Threads,跟着书上的demo走了一遍,并且参考了这个blog(http://www.cnblogs.com/chuyuhuashi/p/4447817. ...