Problem b

题目描述

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

输入

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

输出

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

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

来源

HAOI2011


solution

首先用类似前缀和的方法

题目求f(b,d)-f(a-1,d)-f(c-1,b)+f(a-1,c-1)

这样子就好求了不少

按照套路反演

那么我们算同一个询问的效率就是O(n)的了

这时发现询问数很多

注意到在一定范围内n/kd,m/kd是相同的,可以一起算

范围是多少呢

假设当前为i,nex=n/(n/i)

很好理解,我要找的是连续最长的一段,都等于n/i

n/(n/i)就是最大的那一个,也就是右端

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 50008
#define ll long long
using namespace std;
int T,n,a,b,c,d,k,mu[maxn];
int pri[maxn],flag[maxn],tot;
void init(){
n=50000;
mu[1]=1;
for(int i=2;i<=n;i++){
if(!flag[i])mu[i]=-1,pri[++tot]=i;
for(int j=1;j<=tot&&pri[j]<=n/i;j++){
flag[i*pri[j]]=1;mu[i*pri[j]]=-mu[i];
if(i%pri[j]==0){
mu[pri[j]*i]=0;break;
}
}
mu[i]+=mu[i-1];
}
}
ll C(int x,int y){
ll ans=0;
if(x<y)swap(x,y);
for(int i=1;i<=y;){
int l=i,r=min(x/(x/i),y/(y/i));
ans=ans+(1LL)*(mu[r]-mu[l-1])*(x/i)*(y/i);
i=r+1;
}
return ans;
}
void work(){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
ll tmp=C(b/k,d/k)-C(d/k,(a-1)/k)-C(b/k,(c-1)/k)+C((a-1)/k,(c-1)/k);
printf("%lld\n",tmp);
}
int main(){
init();
cin>>T;while(T--)work();
return 0;
}

Problem b的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

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

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. 46个Linux面试常见问题

    问题一: 绝对路径用什么符号表示?当前目录.上层目录用什么表示?主目录用什么表示? 切换目录用什么命令? 答案:绝对路径: 如/etc/init.d当前目录和上层目录: ./  ../主目录: ~/切 ...

  2. ethereum(以太坊)(八)--Address

    pragma solidity ^0.4.0; contract Test{ address _owner; uint160 _c; constructor() public{ _owner = 0x ...

  3. js常用的2中排序方法:冒泡排序和快速排序

    冒泡排序:例如9 4 5 6 8 3 2 7 10 1 首先:9和4比较  4放前   4 9 5 6 8 3 2 7 10 1 4和5比较   4不动   4 9 5 6 8 3 2 7 10 1 ...

  4. 微信小程序插件内页面跳转和参数传递

    在此以插件开发中文章列表跳传文章详情为例. 1.首先在插件中的文章列表页面wxml中绑定跳转事件. bindtap='url' data-id="{{item.article_id}}&qu ...

  5. python+scrapy环境搭建步骤描述

    Python3(3.5.4)搭建爬虫系统步骤描述: 1.下载python安装包,路径:https://www.python.org/downloads/windows/  选择3.5.4版本64位的安 ...

  6. C++ 基础 初始化列表

    当一个类组合了其他类,或者使用了 const 成员,就要用 初始化列表. Class A {...}; Class B {...}; Class C { private: A a; B b; int ...

  7. python os模块练习题

    # 1.获取某个文件所在目录的上一级目录. # 例如'D:\python\projects\test19.py'目录的结果 :D:\python\projects # 方法1 # path = os. ...

  8. Android stadio Switch repository Android stadio切换仓库

    Android stadio 有时候,有很多module. 这些module 都有自己的仓库.也就是不在一块.那么,Android stadio 默认管理的就是根git. 如图,画对号的就是默认的. ...

  9. 接口&抽象类&继承&父类和子类

    1.接口是什么? 接口可以理解为一个特殊的抽象类,是由全局常量和公共抽象方法组成的. 所以,如果,一个抽象类的方法都是抽象的,那么就可以通过接口的形式来表示. class 用于定义类 interfac ...

  10. Qsys配置生成nios系统模块

    1. 本次使用的是别人写好的例程,主要研究学习,使用quartus 11打开工程 2. bdf文件是块编辑器的,相当于原理图,以前只在用NIOS的时候会用到这种方式.接下来新建一个工程,添加原理图元件 ...