题目链接

求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数。

问题等价于$ x\in[1, a/k] , y \in [1, b/k] $ 内 \(gcd(x, y) = 1\) 的(x, y)的对数。

假设a < b, 那么[1, a/k]这部分可以用欧拉函数算。 设 \(i\in (a/k, b/k]\), (a/k, b/k]这部分可以用容斥算, 用a/k减去[1, a/k]里面和i不互质的数的个数。

具体看代码。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
ll phi[maxn];
vector <int> v[maxn];
void init() {
phi[1] = 1;
for(int i = 2; i<=100000; i++) {
if(!phi[i]) {
for(int j = i; j<=100000; j+=i ) {
if(!phi[j])
phi[j] = j;
phi[j] = phi[j]/i*(i-1);
v[j].pb(i); //筛出j的素因子
}
}
phi[i] += phi[i-1]; //维护前缀和
}
}
int cal(int n, int b) { //算[1, b]中和n互质的数的个数
int len = v[n].size();
int sum = 1<<len;
int ret = 0;
for(int i = 1; i < sum; i++) {
int tmp = 1, cnt = 0;
for(int j = 0; j<len; j++) {
if((1<<j)&i) {
cnt++;
tmp *= v[n][j];
}
}
if(cnt & 1)
ret += b/tmp;
else
ret -= b/tmp;
}
return b-ret;
}
int main()
{
int cnt = 1, t, n, m, a, b, k;
cin>>t;
init();
while(t--) {
scanf("%d%d%d%d%d", &a, &a, &b, &b, &k);
printf("Case %d: ", cnt++);
if(a>b)
swap(a, b);
if(k == 0 || k>a||k>b) {
puts("0");
continue;
}
a /= k;
b /= k;
ll ans = phi[a];
for(int i = a+1; i<=b; i++) {
ans += cal(i, a);
}
cout<<ans<<endl;
}
return 0;
}

hdu 1695 GCD 容斥+欧拉函数的更多相关文章

  1. HDU 1695 GCD (容斥原理+欧拉函数)

    题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...

  2. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  3. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  4. HDU - 1695 GCD (容斥+枚举)

    题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...

  5. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  6. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  7. HDU 4483 Lattice triangle(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...

  8. UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)

    题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...

  9. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

随机推荐

  1. asp.net 跨页面传值常用方法

    常用方法有以下: 1.queryString 2.form-post控件传递 3.cookie 4.application 5.session querystring: http://website. ...

  2. transactionscope报“此操作对该事务的状态无效”问题

    编写的保存方法里面有个transactionscope代码一直报“此操作对该事务的状态无效”,弄了半天,原来是超时问题(transactionscope默认超时时间是1分钟) 经过修改,设置了超时时间 ...

  3. 基于TcpDump和pcap文件分析的Android平台网络抓包程序设计与实现【随便】

    一.考虑使用Tcpdump,将抓到的包保存到cap文件中,然后手动分析.参考资料:1. http://www.cnblogs.com/tt-0411/archive/2012/09/23/269936 ...

  4. 初学swift笔记-数组、字典、元组(三)

    数组的使用.字典的使用.元组的使用 import Foundation //1.定义数组 //集合数据 数组.字典 ,,,]//常用定义 ,,,]//常用定义 ,,,]//范型定义 ,,,] arr_ ...

  5. Balsamiq Mockups registration code

    最近使用Mockups 进行页面原型设计,发现是未注册的,于是网上查询了下注册码,居然有效,在此记录下.    有需要的朋友也可以试试. Name:Rick Dong     Key:eNrzzU/O ...

  6. gridview外边距

    马上注册,结交更多好友,享用更多功能,让你轻松玩转社区. 您需要 登录 才可以下载或查看,没有帐号?注册  x 本帖最后由 同舟 于 2013-9-30 11:44 编辑 最新项目需要个单行显示功能键 ...

  7. Mac OS X 快捷键(完整篇)

    不少朋友提出要求,希望有个「高质量」的列表.其实这样的资源真是太多,平果官网就有 快捷键文档(多国语言版本).于是花了20分钟,浏览了一些网站,整理了点资源放过来供大家参考. 快捷键是通过按下键盘上的 ...

  8. 2014第11周四Eclipse开发问题记

    今天开发中eclipse工具使用上又学到几点: 1.去除代码空行:在Find输入框中输入:^\s*\n然后替换为空即可: 2.eclipse插件的加载:对于单一个jar文件的插件,直接放在plugin ...

  9. Bone Collector(01背包+记忆化搜索)

    Bone Collector Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  10. Codeforces 475C Kamal-ol-molk&#39;s Painting 模拟

    主题链接:点击打开链接 意甲冠军:特定n*m矩阵 X代表色 .代表无色 随着x*y形刷子去涂色. 刷子每次能够→或↓移动随意步. 若可以染出给定的矩阵,则输出最小的刷子的面积 若不能输出-1 思路: ...