[HAOI2011]Problem b BZOJ2301 数学
题目描述
对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。
输入输出格式
输入格式:
第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k
输出格式:
共n行,每行一个整数表示满足要求的数对(x,y)的个数
输入输出样例
说明
100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 10000000005ll
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int a, b, c, d, K;
int mu[maxn], vis[maxn], sum[maxn + 10]; void init() {
for (int i = 1; i <= 50004; i++)mu[i] = 1, vis[i] = 0;
for (int i = 2; i <= 50004; i++) {
if (vis[i])continue;
mu[i] = -1;
for (int j = 2 * i; j <= 50004; j += i) {
vis[j] = 1;
if ((j / i) % i == 0)mu[j] = 0;
else mu[j] *= -1;
}
}
for (int i = 1; i <= 50004; i++)sum[i] = sum[i - 1] + mu[i];
}
int main()
{
// ios::sync_with_stdio(0);
init();
int T = rd();
while (T--) {
cin >> a >> b >> c >> d >> K;
ll ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
for (int l = 1, r; l <= (min(b, d) / K); l = r + 1) {
r = min((b / K) / (b / K / l), (d / K) / (d / K / l));
ans1 += 1ll * (sum[r] - sum[l - 1])*(b / K / l)*(d / K / l);
}
for (int l = 1, r; l <= (min(a - 1, c - 1) / K); l = r + 1) {
r = min((a - 1) / K / ((a - 1) / K / l), (c - 1) / K / ((c - 1) / K / l));
ans2 += 1ll * (sum[r] - sum[l - 1])*((a - 1) / K / l)*((c - 1) / K / l);
}
for (int l = 1, r; l <= (min(a - 1, d) / K); l = r + 1) {
r = min((a - 1) / K / ((a - 1) / K / l), (d) / K / ((d) / K / l));
ans3 += 1ll * (sum[r] - sum[l - 1])*((a - 1) / K / l)*((d) / K / l);
}
for (int l = 1, r; l <= (min(b, c - 1) / K); l = r + 1) {
r = min((b) / K / ((b) / K / l), (c - 1) / K / ((c - 1) / K / l));
ans4 += 1ll * (sum[r] - sum[l - 1])*((b) / K / l)*((c - 1) / K / l);
}
cout << (ll)(ans1 + ans2 - ans3 - ans4) << endl;
}
return 0;
}
[HAOI2011]Problem b BZOJ2301 数学的更多相关文章
- BZOJ 2302: [HAOI2011]Problem c(数学+DP)
题面: bzoj_2302 题解: 令\(dp[i][j]\)表示编号 \(\leq i\)的人有j个的方案数: \(cnt[i]\)表示编号指定为\(i\)的人数,\(sum[i]\)表示编号可以\ ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 436 Solved: 187[Submit][S ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- HAOI2011 problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1047 Solved: 434[Submit][ ...
- BZOJ 2298: [HAOI2011]problem a 动态规划
2298: [HAOI2011]problem a Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1007 Solved: 415[Submit][ ...
- 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4164 Solved: 1888[Submit] ...
- BZOJ 2302: [HAOI2011]Problem c( dp )
dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...
随机推荐
- .Net Core 迁移之坑二 《ToString("F") 输出与windows不一致问题》
大家都知道 ToString("F") 是干什么的 这里我还是介绍一下 格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的 ...
- 【原创】ubuntu14.04 LTS系统VMware虚拟机内Windows server 2008 r2系统的网络配置
主系统:ubuntu14.04 LTS 虚拟机:VMware 内系统:Windows server 2008 r2 主系统IP:任何IP 内系统IP:192.168.202.128(自动获取,如果不是 ...
- CSS中盒子垂直居中的常用方法
在前端开发过程中,盒子居中是常常用到的.其中 ,居中又可以分为水平居中和垂直居中.水平居中是比较容易的,直接设置元素的margin: 0 auto就可以实现.但是垂直居中相对来说是比较复杂一些的.下面 ...
- MySQL5.7 在CentOS 下的安装
尝试了在版本的CentOS6.6 和CentOS7.2 下安装,在6.6下比较复杂些.特地做下记录 在CentOS7.2 下安装,需要在官网下载 mysql-5.7.16-1.el7.x86_64.r ...
- android 命名规则
包名结构: 资源命名方式:
- PHP数据结构之四 一元多项式的相加PHP单链实现
<?php /** *一元多项式的表示和相加 *一元多项式的表示采用单链表的形式 **/ header("content-type:text/html;charset=gb2312&q ...
- 【HDU5861】Road
题意 有n个村庄排成一排,有n-1条路将他们连在一起.每条路开放一天都会花费一定数量的钱.你可以选择打开或者关上任意条路在任意一天,但是每条路只能打开和关闭一次.我们知道m天的运输计划.每天都有一辆马 ...
- MockWebServer使用指南
转载请标明出处:http://blog.csdn.net/shensky711/article/details/52771797 本文出自: [HansChen的博客] MockWebServer介绍 ...
- Docker学习之路(二)DockerFile详解
Dockerfile是一个镜像的表示,可以通过Dockerfile来描述构建镜像的步骤,并自动构建一个容器 所有的 Dockerfile 命令格式都是: INSTRUCTION arguments 虽 ...
- Hyperledger Fabric1.0 整体结构
整体结构 Hyperledger Fabric 在 1.0 中,架构已经解耦为三部分: fabric-peer:主要起到 peer 作用,包括 endorser.committer 两种角色: fab ...