题目链接

求 $ 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. Android开发环境的搭建之(五)DDMS的测试

    启动DDMS(Dalvik Debug Monitor Service) 测试Voice通话功能 左图Device选择MyPhone[Emulator- 5554],右图选择Emulator Cont ...

  2. Android输入法 监听事件

    登录界面有一个输入用户名和密码的编辑框: private EditText et_userName;// 账户 private EditText et_password;// 密码 布局文件如下: & ...

  3. android 快速创建一个新的线程

    要给一个activity做成子线程的模式 第一种:直接创建子线程并启动 private Thread newThread; //声明一个子线程 new Thread() { @Override pub ...

  4. Ubuntu安装Microsoft Windows Fonts微软字体库

    ttf-mscorefonts-installer包是微软的字体包, 可以直接在软件中心中找到安装,也可以通过命令安装 sudo apt-get install ttf-mscorefonts-ins ...

  5. Python作业day2购物车

    流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现),查询余额. 撸了两天一夜的代码,不多说,直接上码,注释神马的后面再说 #!/usr/bin/env python # -*- co ...

  6. Criteria 和 DetachedCriteria的区别与使用(转)

    转自:http://javapub.iteye.com/blog/1149709 Criteria 和 DetachedCriteria 的主要区别在于创建的形式不一样, Criteria 是在线的, ...

  7. getHibernateTemplate()和getSession()的区别

    自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...

  8. JNDI support differences between Tibco EMS and ActiveMQ

    Introduction Recently our team was working on Veracity Quick Start sprint, when I was trying to migr ...

  9. 【自学php】第一天-macbook上配置php

    今天MacBook到手了,就正式开始学习php了.先搭个环境,由于MacBook自带了Apache和php所以只要修改下配置启动就可以了. 1.启用root用户(如果不启用root,下面的命令前都要加 ...

  10. H.264视频的RTP荷载格式

    Status of This Memo This document specifies an Internet standards track protocol for the   Internet ...