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. Centos和Redhat的区别与联系

    CentOS的简介 CentOS是Community ENTerprise Operating System的简称,我们有很多人叫它社区企业操作系统,不管你怎么叫它,它都是Linux操作系统的一个发行 ...

  2. linux操作Mysql数据库基本命令

    1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...

  3. 26- java的String 转json

    https://www.cnblogs.com/nihaorz/p/5885307.html Gjson 下载:https://download.csdn.net/download/qq_394515 ...

  4. solrj 测试连接 6.6.5solr集群

    我开始环境是 linux上是6.6.5  pom也是6.6.5 按照学习视频的demo,他用的是4点几的solr,我换成了6点几的,没有CloudSolrServer  只有CloudSolrClie ...

  5. mysql8 公用表表达式CTE的使用

    公用表表达式CTE就是命名的临时结果集,作用范围是当前语句. 说白点你可以理解成一个可以复用的子查询,当然跟子查询还是有点区别的,CTE可以引用其他CTE,但子查询不能引用其他子查询. 一.cte的语 ...

  6. linux minitools+minicom 安装及使用

    1,通过SSH将minitools的安装包传到ubuntu 文件下, 2,   解压minitools.tgz  (具体方法见上一篇) 3,命令安装minicom  :   apt-get insta ...

  7. MVC是架构模式,而不是设计模式

    最早学编程的时候看过一些书,印象深刻的一本书<设计模式解析>,那本书给我后来的工作提供了很大的帮助. 他叫我站在问题模型的立场上指定解决方法,也教会了我软件设计中每个问题都可以细化到到不可 ...

  8. IDEA配置maven,jdk,编码

    1.电脑已解压maven,修改好settings.xml文件,本地仓库,下载远程镜像 2.

  9. Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍

    Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍 1. PyCharm的基本使⽤用 在PyCharm下为你的Python项⽬目配置Python解释器器 1. Project:当前项⽬目名> ...

  10. 2.git使用之git fetch和git push的区别

    . git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin master git log -p master..origin/master git ...