Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDU 1695 GCD (欧拉函数,容斥原理)

HDU 1695 GCD (欧拉函数,容斥原理)

作者头像
ShenduCC
发布于 2018-04-26 09:04:03
发布于 2018-04-26 09:04:03
51100
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9046    Accepted Submission(s): 3351

Problem 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2
1 3 1 5 1
1 11014 1 14409 9

Sample Output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Case 1: 9
Case 2: 736427
对于求x在1~n之间,y在1~m之间的gcd(x,y)=k;就相当于求x在1~n/k之间,y在1~m/k之间的gcd(x,y)=1;即x,y互质的对数对于欧拉函数,可以求比n小的和n互质的个数。而容斥原理可以求1~指定范围,和n互质的个数。所以我们枚举一个区间的数,然后求这个数在另一个区间的互质的个数。容斥原理可以解决,但是为了学习熟悉欧拉函数,所以可以分成两段,一段用欧拉函数,另一段用容斥原理。
求解欧拉函数,可以用线性素数晒求解,这样同时打了一个素数表,为容斥原理服务
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <bitset>

using namespace std;
typedef long long int LL;
#define MAX 1000000
bool check[MAX+5];
LL fai[MAX+5];
LL prime[MAX+5];
LL sprime[MAX+5];
LL q[MAX+5];
int cnt;
void eular()//线性筛求解欧拉函数
{
	memset(check,false,sizeof(check));
	fai[1]=1;
	int tot=0;
	for(int i=2;i<=MAX+5;i++)
	{
		if(!check[i])
		{
            prime[tot++]=i;
			fai[i]=i-1;
		}
		for(int j=0;j<tot;j++)
		{
			if(i*prime[j]>MAX+5) break;
			check[i*prime[j]]=true;
			if(i%prime[j]==0)
			{
				fai[i*prime[j]]=fai[i]*prime[j];
				break;
			}
			else
			{
				fai[i*prime[j]]=fai[i]*(prime[j]-1);
			}
		}
	}
}
void Divide(LL n)//分解质因子
{
    cnt=0;
    LL t=(LL)sqrt(1.0*n);
    for(LL i=0; prime[i]<=t; i++) {
        if(n%prime[i]==0) {
            sprime[cnt++]=prime[i];
            while(n%prime[i]==0)
                n/=prime[i];
        }
    }
    if(n>1)
        sprime[cnt++]=n;
}
LL Ex(LL n)//容斥原理之队列实现
{
    
    LL sum=0;
    LL t=1;
    q[0]=-1;
    for(LL i=0; i<cnt; i++) {
        LL x=t;
        for(LL j=0; j<x; j++){
            q[t]=q[j]*sprime[i]*(-1);
            t++;
        }
    }
    for(LL i=1; i<t; i++)
        sum+=n/q[i];
    return sum;
}
int main()
{
	int t;
	scanf("%d",&t);
	eular();
	int cas=0;
	int a,b,c,d,k;
	while(t--)
	{
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
		if(k==0||k>b||k>d)
		{
			printf("Case %d: 0\n",++cas);
			continue;
		}
		if(b>d) swap(b,d);
		b/=k;d/=k;
		LL ans=0;
		for(int i=1;i<=b;i++)
             ans+=fai[i];
		for(int i=b+1;i<=d;i++)
		{ Divide(i);ans+=(b-Ex(b));}
		printf("Case %d: %lld\n",++cas,ans);
	}
	return 0;

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
GCD Determinant 解题报告
http://www.cn210.com/onlinejudge/problemshow.php?pro_id=98 我们的OJ Description We say that a set 
owent
2018/08/01
4870
HDU-5514-Frogs
该文是有关ACM模板题目的解题报告,题目为HDU-5514-Frogs,作者通过编写代码,分两步解决了该问题。第一步通过容斥原理求解了该问题,第二步通过欧拉函数求解了该问题。
f_zyj
2018/01/09
5820
HDU-5514-Frogs
HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 849    Accepted Submission(s): 232 Problem Description Please write a program to calculate the k-th positive integer that is coprime with m
ShenduCC
2018/04/26
6210
HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2494    Accepted Submission(s): 775 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and
ShenduCC
2018/04/26
6130
POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9..
ShenduCC
2018/04/26
6720
HDU-4407-Sum(容斥原理)「建议收藏」
XXX is puzzled with the question below:
全栈程序员站长
2022/07/08
2020
HDU 1796 How many integers can you find(容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6434    Accepted Submission(s): 1849 Problem Description   Now you get a number N, and a M-integers set, you should
ShenduCC
2018/04/26
6950
【HDU2865】构造矩阵+Burnside定理+欧拉函数类似poj2888[通俗易懂]
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 466 Accepted Submission(s): 238
全栈程序员站长
2022/09/15
2440
【HDU2865】构造矩阵+Burnside定理+欧拉函数类似poj2888[通俗易懂]
专题练习---(数论)莫比乌斯反演
            GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7026    Accepted Submission(s): 2584 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d th
Gxjun
2018/03/26
8750
专题练习---(数论)莫比乌斯反演
How many integers can you find(容斥原理) - HDU 1796
看这题之前先复习一下容斥原理,不然肯定看不懂,呃,如果第一次接触容斥原理的题,可能弄懂了容斥原理你还是看不懂代码,是的,等会你就知道了。
ACM算法日常
2018/08/07
7710
数学--数论--HDU 4675 GCD of Sequence
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She says each integers 1 ≤ a i ≤ M. And now Alice wants to ask for each d = 1 to M, how many different sequences b
风骨散人Chiam
2020/11/06
3230
召唤师·卡尔(Polya定理)- HDU 3923
Polya定理是为了解决环状组合计数的问题,比如,对于一个有5颗珠子的环形手链,给你2种颜色对珠子上色,能够得到多少种不同的手链呢?
ACM算法日常
2018/08/07
1.1K0
召唤师·卡尔(Polya定理)- HDU 3923
uva 11426 欧拉函数的运用
对于互质的两个数 a,b (a<b<=n), gcd(a,b) = 1 是显然的。
用户2965768
2019/08/01
3460
hdu 1695 GCD(莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6081    Accepted Submission(s): 2223 Problem 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)
Gxjun
2018/03/26
8510
莫比乌斯函数入门
要解决这道题目,它要求莫比乌斯(Mobius)函数作为知识背景. 所以我们先来学习一下mobius函数.
ACM算法日常
2020/06/19
1.6K0
莫比乌斯函数入门
【HDU4947】GCD Array (莫比乌斯反演+树状数组)
BUPT2017 wintertraining(15) #5H HDU- 4947 题意 题解 官方题解: 代码 #include<cstdio> #include<cstring> #include
饶文津
2020/06/02
3180
【HDU4947】GCD Array (莫比乌斯反演+树状数组)
D. Same GCDs
time limit per test:2 seconds memory limit per test:256 megabytes inputstandard input outputstandard output
某些人
2020/04/09
2510
数学--数论--广义欧拉降幂(模板)
未使用欧拉筛: 适用于较少次数计算的欧拉降幂。 #include <bits/stdc++.h> #define ll long long using namespace std; ll a,m,b; inline ll read(ll m){ register ll x=0,f=0;char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){ x=x*10+ch-'0';
风骨散人Chiam
2020/10/28
3150
数学--数论--HDU6919 Senior PanⅡ【2017多校第九场】
给出一个区间[L,R][L,R],问该区间中所有以KK作为最小因子(大于11的)的数字之和
风骨散人Chiam
2020/10/28
3050
hdu 3908 Triple(组合计数、容斥原理)
Triple Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1365    Accepted Submission(s): 549 Problem Description Given many different integers, find out the number of triples (a, b, c) which satisfy
Gxjun
2018/03/26
8480
相关推荐
GCD Determinant 解题报告
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验