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\n", ans);
}
return 0;
}

  

BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)的更多相关文章

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

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

  2. 2301: [HAOI2011]Problem b ( 分块+莫比乌斯反演+容斥)

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

  3. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)

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

  4. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演)

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

  5. Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...

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

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

  7. bzoj 2301: [HAOI2011]Problem b

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

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

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  9. 【BZOJ】2301: [HAOI2011]Problem b(莫比乌斯+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 和这题不是差不多的嘛--[BZOJ]1101: [POI2007]Zap(莫比乌斯+分块) 唯 ...

随机推荐

  1. http406错误

    The resource identified by this request is only capable of generating responses with characteristics ...

  2. c#、.net、asp.net、asp 、ado.net、.net framework的区别

    c#:一种编程语言 .net:一种运行环境 asp.net:基于.netFramework框架下的一种开发技术(相对与asp而言,引入了服务器控件,前后台可分,编译型的编程框架) asp:也是.net ...

  3. 33-Java中的String,StringBuilder,StringBuffer三者的区别

    转载自:https://www.cnblogs.com/su-feng/p/6659064.html StringBuilder 详解 (String系列之2) Java中的String,String ...

  4. FTPserver

    客户端代码: import os import hashlib BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__) ...

  5. Win10以管理员身份启动cmd.exe

    PowerShell -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k, pushd' -Ve ...

  6. 严重性代码说明项目文件行错误C4996'strcpy' 和Unicode 字符集选择问题

    严重性代码说明项目文件 行错误 C4996 ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s ins ...

  7. Luogu 2059 [JLOI2013]卡牌游戏 - 概率DP

    Solution 设状态 $F[i][j] $为 还剩余 $i$ 个人时, 第 $j$ 个人 的胜率. 边界: $F[1][1] = 1$(只剩下一个人了). 这样设置状态就能使 $i-1$ 个人的答 ...

  8. Luogu 2575 高手过招-SG函数

    Solution SG函数跑一遍就过了ouo Code #include<cstring> #include<cstdio> #include<algorithm> ...

  9. HTML 学习杂记

    代码范例 <?php function testFunc1 () { echo 'testFunc1'; } $b = ; ?> <!DOCTYPE html PUBLIC &quo ...

  10. PHP代码不应有的坏习惯

    >>使用echo取代print >>使用str_replace取代preg_replace, 除非你绝对需要 >>不要使用 short tag >>简单 ...