BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
2301: [HAOI2011]Problem b
Time Limit: 50 Sec Memory Limit: 256 MB
Submit: 436 Solved: 187
[Submit][Status]
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 5 1 5 1
1 5 1 5 2
Sample Output
3
HINT
100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000
Source
HOME Back
思路可以参考下面的链接:
http://www.cnblogs.com/zhsl/p/3269288.html
http://wenku.baidu.com/view/fbe263d384254b35eefd34eb.html
分段优化。
/* ***********************************************
Author :kuangbin
Created Time :2013/8/21 20:19:04
File Name :F:\2013ACM练习\专题学习\数学\莫比乌斯反演\BZOJ2301.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
bool check[MAXN+];
int prime[MAXN+];
int mu[MAXN+];
void Moblus()
{
memset(check,false,sizeof(check));
mu[] = ;
int tot = ;
for(int i = ; i <= MAXN; i++)
{
if( !check[i] )
{
prime[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot; j ++)
{
if( i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == )
{
mu[i * prime[j]] = ;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
}
int sum[MAXN+];
//找[1,n],[1,m]内互质的数的对数
long long solve(int n,int m)
{
long long ans = ;
if(n > m)swap(n,m);
for(int i = , la = ; i <= n; i = la+)
{
la = min(n/(n/i),m/(m/i));
ans += (long long)(sum[la] - sum[i-])*(n/i)*(m/i);
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
Moblus();
sum[] = ;
for(int i = ;i <= MAXN;i++)
sum[i] = sum[i-] + mu[i];
int a,b,c,d,k;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
long long ans = solve(b/k,d/k) - solve((a-)/k,d/k) - solve(b/k,(c-)/k) + solve((a-)/k,(c-)/k);
printf("%lld\n",ans);
}
return ;
}
BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)的更多相关文章
- Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...
- BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1007 Solved: 415[Submit][ ...
- BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
[Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...
- BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演
分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...
- bzoj 2301: [HAOI2011]Problem b mobius反演 RE
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 设f(i)为在区间[1, n]和区间[1, m]中,gcd(x, y) = i的个数. 设F( ...
- BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 6519 Solved: 3026[Submit] ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- BZOJ 2301: [HAOI2011]Problem b( 数论 )
和POI某道题是一样的... http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...
随机推荐
- linux 定时执行任务 at atq atrm命令的使用
1.at命令在指定时刻执行指定的命令序列 at [-V] [-q 队列] [-f 文档名] [-mldbv] 时间 下面对命令中的参数进行说明.-V 将标准版本号打印到标准错误中.-q queue 使 ...
- 中文chrome font-size 10px,11px,12px,rem只为12px解决办法
问题来源: html { font-size: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .form-signin { max-wi ...
- 以太坊go-ethereum项目源码本地环境搭建
如果要深入了解go-ethereum项目的实现与机制,看源代码是必不可少的.今天这篇博客就简单介绍一下如何在本地搭建项目的开发环境. GO语言环境搭建 以win8为例,访问地址https://gola ...
- SQL Server 数据库优化剖析
一.SQL Profiler 事件类 Stored Procedures\RPC:Completed TSQL\SQL:BatchCompleted 事件关键字段 EventSequence.Even ...
- ssh连接远程主机免密登入
核心思想: 1.本地主机生成公钥私钥,私钥自己存着,公钥传到远程主机.ssh文件夹下authorized_keys文件(默认是这个,用追加的方式) 2.本地连接远程主机,公私钥对上就可以免密登入了. ...
- zookeeper的简单使用
前言 最近项目中要使用基于zookeeper的集中配置管理系统,而对于zookeeper仅在当初使用阿里开源分布式服务调用框架dubbo时简单的了解一下.本 文的主要目的,调用zkclient (ma ...
- js求连个数之间的数字
整理出自项目中一个需求,求两个数之间的数字. const week = function(arr,arr2){ let a=parseInt(arr); let b=parseInt(arr2); l ...
- 【LOJ】 #2520. 「FJOI2018」所罗门王的宝藏
题解 发现似乎相当于问一个2000个元的方程组有没有解-- 然而我懵逼啊-- 发现当成图论,两个点之间连一条边,开始BFS,每个点的值赋成边权减另一个点的点权 如果一个环不合法那么肯定无解 代码 #i ...
- spring_150905_sqlmapclient
添加ibatis相关的jar包! 实体类: package com.spring.model; public class DogPet { private int id; private String ...
- bean的singleton(没有看到生命周期范围??)
4.5.1 The singleton scope Only one shared instance of a singleton bean is managed, and all requests ...