BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)

2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 6519  Solved: 3026
[Submit][Status][Discuss]

Description

对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。



Input

第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

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

析:首先很容易看出来是莫比乌斯反演,但是直接用会TLE,因为有数据数组,总时间复杂度就是O(n^2),肯定会超时,所以要进行优化,因为在求答案的时候,对于每个G函数都要一个个来求,而G函数就是(m/i)*(n/i),m,n表示现最大的两个边界,在一段值内,它们的值是相等的,所以可以先求莫比乌斯函数的前缀和,进行优化,时间复杂度就大降低了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int maxm = 3e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

bool vis[maxn];
int prime[maxn];
int mu[maxn];

void Moblus(){
  mu[1] = 1;
  int tot = 0;
  for(int i = 2; i < maxn; ++i){
    if(!vis[i])  prime[tot++] = i, mu[i] = -1;
    for(int j = 0; j < tot; ++j){
      if(i * prime[j] >= maxn)  break;
      vis[i*prime[j]] = 1;
      if(i % prime[j] == 0){
        mu[i*prime[j]] = 0;
        break;
      }
      else  mu[i*prime[j]] = -mu[i];
    }
  }
}

int sum[maxn];
LL solve(int n, int m){
  if(n > m)  swap(n, m);
  LL ans = 0;
  for(int i = 1, det = 1; i <= n; i = det+1){
    det = min(n/(n/i), m/(m/i));
    ans += (LL)(sum[det] - sum[i-1]) * (n/i) * (m/i);
  }
  return ans;
}

int main(){
  Moblus();
  for(int i = 1; i < maxn; ++i)  sum[i] = sum[i-1] + mu[i];
  int T;  cin >> T;
  while(T--){
    int a, b, c, d, k;
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
    LL ans = solve(b/k, d/k) - solve((a-1)/k, d/k) - solve((c-1)/k, b/k) + solve((a-1)/k, (c-1)/k);
    printf("%lld
", ans);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8335026.html