题目链接

求 $ 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. %1 不是有效的 Win32 应用程序

    客户环境windows server 2008,iis 6.0. 站点配置与以往并无差别,更新类库后出现“%1 不是有效的 Win32 应用程序”错误,如下图: 系统登录页面可以正常加载,登录过程中出 ...

  2. 几个Python oj的网站

    http://www.rqnoj.cn/ http://www.pythontip.com/

  3. HTML1网页三部份内容

    网页三部份内容:HTML CSS Javascript 路径:一般做网页的时候用的相对路径. images/aaa.jpg 网页同一个目录中找images文件夹,再在images里面找aaa.jpg ...

  4. python基础之 re(正则表达式)模块学习

    今天学习了Python中有关正则表达式的知识.关于正则表达式的语法,不作过多解释,网上有许多学习的资料.这里主要介绍Python中常用的正则表达式处理函数. re.match re.match 尝试从 ...

  5. 点击上下页,实现图片滚动的jquery代码

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  6. box-shadow 给图片添加内部阴影

    box-shadow 是css3中定义的设置元素阴影的属性,其语法结构如下: <shadow> = inset? && <length>{2,4} && ...

  7. js自定义类和对象及继承

    1.工厂方式 <script type="text/javascript"> function createObject(name){ var p = new Obje ...

  8. 为每个页面加上Session判断 转

    首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下:   using System; using System.Data; using System.Configu ...

  9. Delphi XE6调用javascript

    原文地址:Example of using JavaScript for Google maps in the Delphi XE6   XE6的TWebBrowser新增了EvaluateJavaS ...

  10. 学习Emacs系列教程

    emacs最简单入门,只要10分钟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...