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. jmeter简单压测设置

    参数化 随机参数 时间参数 顺序自增函数  文件读取  直接引用 响应断言 用来查看sessionid 关联 关联引用 jmeter操作数据库 安装连接程序包 ip 端口号 哪个数据库 可以执行多条s ...

  2. 32位Server2008添加IIS

  3. Android的事件分发机制

    public boolean dispatchTouchEvent(MotionEvent event) 通过方法名我们不难猜测,它就是事件分发的重要方法.那么很明显,如果一个MotionEvent传 ...

  4. webpack.base.conf.js

    var path = require('path')var utils = require('./utils')var config = require('../config')var vueLoad ...

  5. 类似No module named 'bs4'等错误的解决方法

    最近开始接触爬虫,写了如下源代码: from bs4 import BeautifulSoup import requests url='https://www.tripadvisor.cn/Attr ...

  6. WEB框架Django之ORM操作

    一 ORM的简介 MVC或者MVC框架中包括的一个重要部分就是ORM,它实现了数据模型与数据库的解耦. 即数据模型的设计不需要依赖于特定的数据库,通过简单的配置可以轻松更换数据库,这可以大大减少开发人 ...

  7. vue通过代理实现跨域

    http://www.cnblogs.com/wangyongcun/p/7665687.html

  8. Jmeter常用脚本开发之Java请求

    Java请求:就是用来对java项目的类进行压测 例子:将输入的两个参数通过IO存入文件 1.创建Java工程,工程目录如下 2.将Jmeter-lib下面的所有jar包导入java工程, 3.创建一 ...

  9. codeforces round#509

    博主水平不高, 只能打完$4$题, QAQ什么时候才能变强啊嘤嘤嘤 订正完6题了,  还想打今天下午的CF , 只能迟十分钟了, 掉分预定 A. Heist 输出 $max - min + n - 1 ...

  10. Lftp 简单使用步骤

    有一用户需要在Linux上面将数据备份至存储上面,但是不支持挂载. 只可以FTP. 找了下,Lftp 这款FTP Client 满足用户的需求. 相关资料参考:http://blog.chinauni ...