hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d)。
知识点:
线性筛求莫比乌斯反演函数:
void Init()
{
memset(vis,0,sizeof(vis));
mu[1] = 1;
cnt = 0;
for(int i=2; i<N; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
mu[i] = -1;
}
for(int j=0; j<cnt&&i*prime[j]<N; j++)
{
vis[i*prime[j]] = 1;
if(i%prime[j]) mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = 0;
break;
}
}
}
}
题解:
转化题意就是[1,n/k],[1,m/k]之间互质的数的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000+10;
int u[N],prime[N];
bool vis[N]; void init()
{
memset(vis,0,sizeof(vis));
u[1] = 1;
int cnt = 0;
for(int i=2; i<N; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -1;
}
for(int j=0; j<cnt&&i*prime[j]<N; j++)
{
vis[i*prime[j]] = 1;
if(i%prime[j]) u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = 0;
break;
}
}
}
}
int main()
{
init();
int t;
cin>>t;
int a,b,c,d,k;
for(int kase=1;kase<=t;kase++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==0)
{
printf("Case %d: 0\n",kase);
continue;
}
long long ans=0;
int ma=max(b,d),mi=min(b,d);
for(int i=k;i<=mi;i+=k)
{
ans+=(long long)u[i/k]*((ma/i)*2-(mi/i)+1)*(mi/i)/2;
}
printf("Case %d: %I64d\n",kase,ans);
}
return 0;
}
GCD
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Output
For each test case, print the number of choices. Use the format in the example.
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
Sample Output
Case 1: 9
Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
hdu1695 GCD(莫比乌斯反演)的更多相关文章
- HDU1695 GCD(莫比乌斯反演)
传送门 看了1个多小时,终于懂了一点了 题目大意:给n,m,k.求gcd(x,y) = k(1<=x<=n, 1<=y<=m)的个数 思路:令F(i)表示i|gcd(x,y)的 ...
- hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对
/** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...
- [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)
[BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...
- hdu 1695 GCD 莫比乌斯反演入门
GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...
- hdu1695(莫比乌斯反演)
传送门:GCD 题意:求[1,n],[1,m]gcd为k的对数. 分析:莫比乌斯入反演门题,gcd(x,y)==k等价于gcd(x/k,y/k)==1,求出[1,n][1,m]互质的对数,在减去[1, ...
- 洛谷P2257 YY的GCD 莫比乌斯反演
原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...
- HYSBZ - 2818 Gcd (莫比乌斯反演)
莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数. 易知\[F(p) = \lfloor \frac{n}{p} \rf ...
- 【BZOJ2818】Gcd [莫比乌斯反演]
Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...
- Luogu P2257 YY的GCD 莫比乌斯反演
第一道莫比乌斯反演...$qwq$ 设$f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$ $F(n)=\sum_{n|d}f(d)=\lfloor \frac{N ...
- BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 2534 Solved: 1129 [Submit][Status][Discu ...
随机推荐
- CSS实现图片缩放特效
今天是感恩节,祝大家感恩节快乐哦!最近天冷了,大家注意保暖哟.下面一起看看小颖写的demo吧. html代码: <!DOCTYPE html> <html> <head& ...
- C# XML转JSON,不引用第三方JSON.NET类库
应用场景:需要调用第三方接口(返回XML)数据,然后供自己多个系统使用(涉及跨域,使用JSONP) 代理:调用接口(把XML转换为JSONP解决跨域问题) B/S应用系统:调用代理返回的数据进行UI显 ...
- 安卓v7支持包下的ListView替代品————RecyclerView
RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 项目已经同步至github:https://github.com/ ...
- 你真的会玩SQL吗?让人晕头转向的三值逻辑
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- c#编程基础之函数可变参数
可变参数:int sum (params int[] values)int sum (string name,params int[] values) 注意:params参数必须是形参表中的最后一个参 ...
- IE兼容性问题汇总【持续更新中】
问题:IE8/9不支持Array.indexOf 解决方案 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt ...
- 简简单单学会C#位运算
一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...
- 福利到!Rafy(原OEA)领域实体框架 2.22.2067 发布!
距离“上次框架完整发布”已经过去了一年半了,应群中的朋友要求,决定在国庆放假之际,把最新的框架发布出来,并把帮助文档整理出来,这样可以方便大家快速上手. 发布内容 注意,本次发布,只包含 Rafy ...
- Xcode7.1环境下上架iOS App到AppStore 流程③(Part 三)
前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息.使用Application Loader上传.ipa文件到AppStore 一.Xcode配置发布证书信息 1)给 ...
- c#中get set 的使用
在早期学习c#的过程中,经常遇到这样的语句: public string StudentName { get{return stuName;} set{stuName=value;} } 当时也不是很 ...