题目描述

对于给出的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)的个数

输入输出样例

输入样例#1:
复制

2
2 5 1 5 1
1 5 1 5 2
输出样例#1: 复制

14
3

说明

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 数学的更多相关文章

  1. BZOJ 2302: [HAOI2011]Problem c(数学+DP)

    题面: bzoj_2302 题解: 令\(dp[i][j]\)表示编号 \(\leq i\)的人有j个的方案数: \(cnt[i]\)表示编号指定为\(i\)的人数,\(sum[i]\)表示编号可以\ ...

  2. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  3. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  4. bzoj 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...

  5. HAOI2011 problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1047  Solved: 434[Submit][ ...

  6. BZOJ 2298: [HAOI2011]problem a 动态规划

    2298: [HAOI2011]problem a Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  7. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1007  Solved: 415[Submit][ ...

  8. 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4164  Solved: 1888[Submit] ...

  9. BZOJ 2302: [HAOI2011]Problem c( dp )

    dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...

随机推荐

  1. Spring Cloud Eureka 5 (服务发现与消费-简单的robbin使用)

    通过上述介绍,我们已经有了服务注册中心和服务提供者 下面我们来尝试构建一个服务的消费者 它要完成两个功能,发现服务和消费服务,其中发现服务由eureka客户端完成,消费服务由ribbon完成. rib ...

  2. 【bzoj4296】再见Xor

    4269: 再见Xor Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 176  Solved: 107[Submit][Status][Discuss ...

  3. jQuery 对象和 json 之间的转换

    JSON.parse() json 转换成 对象 JSON.stringify() 对象转换成json 想问下广大网友大神 js 里面有数组转换或者转换成数组的函数么?小弟等--

  4. 公共技术点之 Java 反射 Reflection

    本文摘录地址: http://codekk.com/open-source-project-analysis/detail/Android/Mr.Simple/%E5%85%AC%E5%85%B1%E ...

  5. PHP 5.5环境配置

    php5.5 + apache2.4 安装配置 1 2 3 4 5 6 7 分步阅读 php5.5 做了大量的更新,在与apache搭配的时候如何选择也很有讲究,这里我们以64位 php5.6 和 A ...

  6. 258. Add Digits 数位相加到只剩一位数

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  7. Solidity oraclize 常用数据源

    1. 股票数据: https://blog.quandl.com/api-for-stock-data iextrading.com www.nowapi.com 中文 2. 外汇数据: https: ...

  8. Luogu 2573 [SCOI2012]滑雪

    BZOJ 2753 首先可以按照题目要求的把所有的有向边建出来,然后进去广搜就可以求出第一问的解,然后考虑如何求解第二问,我们把所有搜到的边按照到达的点的高度位第一关键字,边的长度为第二关键字排序之后 ...

  9. eclipse——执行Maven命令

    右键pom.xml文件 点击 m2 Maven build... 输入要执行的命令,点击Run 控制台会打印maven运行过程

  10. jQuery 选择器2

    jQuery 选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元 ...