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. 基于LNMP环境的ssh2扩展

    openssl: 加密算法集合,C语言实现 libssh2:ssh2协议库库,C语言实现 PECL/ssh2: libssh2的php扩展,允许php程序调用libssh2中的函数 依赖关系:PECL ...

  2. dts--framework(一)

    dts 大体框架 framework 定义类 定义方法 tests framework调用所需要的函数 ./dpdk/usertools/cpu_layout.py /sys/devices/syst ...

  3. python爬虫:爬取猫眼TOP100榜的100部高分经典电影

    1.问题描述: 爬取猫眼TOP100榜的100部高分经典电影,并将数据存储到CSV文件中 2.思路分析: (1)目标网址:http://maoyan.com/board/4 (2)代码结构: (3) ...

  4. Requests库:python实现的简单易用的http库

    1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...

  5. [Bzoj4818]序列计数(矩阵乘法+DP)

    Description 题目链接 Solution 容斥原理,答案为忽略质数限制的方案数减去不含质数的方案数 然后矩阵乘法优化一下DP即可 Code #include <cstdio> # ...

  6. sparkStreaming统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数

    样例数据: __clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=89385239&iamount=1& ...

  7. MyEclipse - 问题集 - maven update project 后,项目jdk的版本变化

    解决方法: 进入maven安装根目录,conf/settings.xml <profiles> <profile> <id>jdk-1.7</id> & ...

  8. fix34

    public int[] fix34(int[] nums) { int i3=0; int i4=0; int temp=0; while( (i3<nums.length)&& ...

  9. 《Cracking the Coding Interview》——第7章:数学和概率论——题目1

    2014-03-20 01:57 题目:玩篮球投篮,有两种玩法:要么1投1中,要么3投两中.你单次投篮中的概率是p,那么对于不同的p,哪种玩法胜率更高? 解法:第一种总是胜率更高,可以列不等式算算,结 ...

  10. CodeIgniter学习笔记四:CI中的URL相关函数,路由,伪静态,去掉index.php

    一.URL相关函数 1.加载url模块 加载url有两种方式: a.自动加载:在 application/config/autoload.php 中开启 $autoload['helper'] = a ...