problem's Link

mean

给定五个数a,b,c,d,k,从1~a中选一个数x,1~b中选一个数y,使得gcd(x,y)=k.

求满足条件的pair(x,y)数.

analyse

由于b,d,k都是1e5数量级的,普通枚举必定超时.

首先可以把b,d都同时除以k,问题就转化成了求1~b/k和1~d/k中的gcd(i,j)=k的对数.

证明如下:

令Ai∈{1,2,3...b},Bi∈{1,2,3...d}.

如果有:GCD(Ai,Bi)=k

则有:GCD(Ai/k,Bi/k)=1

而对于不能够被k整除的数,不可能有GCD(Ai,Bi)=k.

也就是说,除以K所剔除掉的数都是不满足条件的数,对最终答案没有影响.

这样就大大优化了时间复杂度.

然后就是对1e5以内的数进行质因数分解,使用质因数来构造容斥表.

再枚举1~b/k之间的每一个数,利用容斥原理算出1~d/k中有多少个数与之互质即可.

time complexity

O(N*logN)

code

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-10-08-21.45
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-); const int NN=;
bool v[NN];
int p[NN];
void makePrime()
{
int num=-,i,j;
for(i=; i<NN; ++i)
{
if(!v[i]) { p[++num]=i; }
for(j=; j<=num && i*p[j]<NN; ++j)
{
v[i*p[j]]=true;
if(i%p[j]==) { break; }
}
}
} struct node
{
int fac;
bool ti;
node() {}
node(int a,bool b):fac(a),ti(b) {}
};
vector<node> pa[NN]; void pre()
{
int i,j,a,cnt,si;
for(i=; i<=; ++i)
{
a=i;
cnt=;
for(j=; j<=; ++j)
{
if(!(a%p[j]))
{
pa[i].push_back(node(p[j],false));
si=pa[i].size();
for(int k=; k<si-; ++k)
{
pa[i].push_back(node(pa[i][si-].fac*pa[i][k].fac,!pa[i][k].ti));
}
while(!(a%p[j]))
a/=p[j];
}
if(p[j]>a || a<=) break;
}
}
} int main()
{
makePrime();
pre();
ios_base::sync_with_stdio(false);
cin.tie();
int t;
scanf("%d",&t);
for(int Cas=; Cas<=t; ++Cas)
{
int a,b,c,d,k,si;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&k);
if(k==)
{
printf("Case %d: 0\n",Cas);
continue;
}
a=b/k;
b=d/k;
if(a>b) swap(a,b);
LL ans=b;
if(a==) ans=;
for(int i=; i<=a; ++i)
{
si=pa[i].size();
for(int j=; j<si; ++j)
{
if(!(pa[i][j].ti))
{
ans+=((b-i+)-b/pa[i][j].fac+(i-)/pa[i][j].fac);
}
else
{
ans-=((b-i+)-b/pa[i][j].fac+(i-)/pa[i][j].fac);
}
}
}
printf("Case %d: %I64d\n", Cas, ans);
}
return ;
}

数论 + 容斥 - HDU 1695 GCD的更多相关文章

  1. 数论 + 容斥 - HDU 4059 The Boss on Mars

    The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  2. POJ 1150 The Last Non-zero Digit 数论+容斥

    POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...

  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 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. hdu 1695 GCD 容斥+欧拉函数

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

  7. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. HDU - 2204 Eddy's爱好 (数论+容斥)

    题意:求\(1 - N(1\le N \le 1e18)\)中,能表示成\(M^k(M>0,k>1)\)的数的个数 分析:正整数p可以表示成\(p = m^k = m^{r*k'}\)的形 ...

  9. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

随机推荐

  1. sed 命令编辑文本

    1.sed 概述 sed 是一个非交互式文本编辑器.它能够对文本文件和标准输入进行编辑,标准输入能够是来自键盘输入.文件重定向.字符串.变量.甚至来自于管道文本. 2.sed工作流程简述 sed在处理 ...

  2. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  3. SQL语法 之 基本查询

    一.语法结构 SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY ...

  4. xcode 5 使用 XCTest 做单元测试

    xcode 5 使用 XCTest 做单元测试 什么是单元测试,请看 百度百科 单元测试 一:在xcode5 之前,我们新建项目时,可以选择是否集成单元测试:如今在xcode5,我们新建立的项目默认就 ...

  5. 使用Jmeter对应用程序进行测试

    JMeter是Apache组织的开放源代码项目,它是功能和性能测试的工具,100%的用java实现,最新的版本是1.9.1,大家可以到 http://jakarta.apache.org/jmeter ...

  6. 编写的windows程序,崩溃时产生crash dump文件的办法

    一.引言 dump文件是C++程序发生异常时,保存当时程序运行状态的文件,是调试异常程序重要的方法,所以程序崩溃时,除了日志文件,dump文件便成了我们查找错误的最后一根救命的稻草.windows程序 ...

  7. win10 家庭中文版打开本地组策略编辑器

      win10 家庭中文版打开本地组策略编辑器 CreateTime--2018年5月14日09:01:25 Author:Marydon 1.问题描述 2.问题解析 win10家庭版没有访问本地组策 ...

  8. centos增加软连接

    #增加软连接 ln -s /usr/local/git/bin/* /usr/bin/

  9. STS IDE 个性化修改

    JDK: Eclipse或MyEclipse文件系统不同步的解决方法 STS汉化: 1.解压STS中的language文件夹 以我的安装目录为例,我的STS的安装在D:盘下.将解压后的“languag ...

  10. C++ 资源管理之 RAII

    RAII,它是“Resource Acquisition Is Initialization”的首字母缩写.也称为“资源获取就是初始化”,是c++等编程语言常用的管理资源.避免内存泄露的方法.它保证在 ...