BZOJ 2301: [HAOI2011]Problem b( 数论 )

和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html

只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) 

------------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
 
using namespace std;
 
const int maxn = 50009;
 
bool F[maxn];
int mu[maxn], p[maxn], pn, D;
 
void Init() {
memset(F, 0, sizeof F);
pn = 0;
for(int i = 2; i < maxn; i++) {
if(!F[i]) {
p[pn++] = i;
mu[i] = -1;
}
for(int j = 0; j < pn && i * p[j] < maxn; j++) {
F[i * p[j]] = true;
if(i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
} else
mu[i * p[j]] = -mu[i];
}
}
mu[0] = 0;
mu[1] = 1;
for(int i = 1; i < maxn; i++)
mu[i] += mu[i - 1];
}
 
int f(int x, int y) {
if((y /= D) > (x /= D)) swap(x, y);
int ret = 0;
for(int L = 1, R; L <= y; L = R + 1) {
R = min(x / (x / L), y / (y / L));
ret += (mu[R] - mu[L - 1]) * (x / L) * (y / L);
}
return ret;
}
 
void Work() {
int T;
scanf("%d", &T);
while(T--) {
int a, b, c, d;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &D);
printf("%d\n", f(b, d) + f(a - 1, c - 1) - f(a - 1, d) - f(b, c - 1));
}
}
 
int main() {
Init();
Work();
return 0;
}

------------------------------------------------------------------------------

2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 2475  Solved: 1054
[Submit][Status][Discuss]

Description

对于给出的n个询问,每次求有多少个数对(x,y),满足axbcyd,且gcd(x,y) = kgcd(x,y)函数为xy的最大公约数。



Input

第一行一个整数n,接下来n行每行五个整数,分别表示abcdk

 

Output

n行,每行一个整数表示满足要求的数对(x,y)的个数

 

Sample Input

2

2 5 1 5 1

1 5 1 5 2



Sample Output


14

3



HINT



100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000

Source

 

posted @ 2016-01-07 14:29  JSZX11556  阅读(203)  评论(0编辑  收藏  举报