hdu4135 Co-prime(容斥)

题目链接:点这里!!!!

题意:

给你一个区间[A,B](1<=A<=B<=1E15),一个x(x<=1e9)。问你[A,B]区间内有多少个数与x互质。

题解:

1、我们先把x的所有质因子找出来,注意x的最多存在1个质因子大于(sqrt(x)),所以我们可以先预处理出[1,sqrt(1e9)]所有的质数。

2、然后我们把x质因子分解并往下除,最后剩下的要么是1,要么就是大于sqrt(x)的质因子。

3、我们得到的质因子最多15个左右,我们直接利用容斥搞就可以了。我们得出的是与x不互质数的个数,利用总数减去就可。

代码;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const LL  MOD = 1E9+7;
const LL N = 3e4+5000;
const int maxn = 5e5+15;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
LL a,b;
int x;
int vis[N],cnt=0;
int prime[10000],ps[32];
void init(){
    vis[1]=1;
    for(int i=2;i<N;i++){
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<N;j++){
            vis[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int main(){
    init();
    int T,cas=0;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d%d",&a,&b,&x);
        int num=0;
        for(int i=1;i<=cnt;i++){
            if(x%prime[i]==0){
                while(x%prime[i]==0){
                    x/=prime[i];
                }
                ps[num++]=prime[i];
            }
        }
        if(x!=1) ps[num++]=x;
        LL sum=b-a+1ll;
        LL tt=0;
        for(int i=1;i<(1<<num);i++){
            int cc=0;
            LL ans=1;
            for(int j=0;j<num;j++){
                if(i&(1<<j)){
                    cc++;
                    ans*=1ll*ps[j];
                }
            }
            if(cc&1) tt=tt+(b/ans-(a-1)/ans);
            else tt=tt-(b/ans-(a-1)/ans);
        }
        printf("Case #%d: %I64d\n",++cas,sum-tt);
    }
    return 0;
}
/*
100
3 15 12

*/

给你一个区间[A,B](1<=A<=B<=1E15),一个x(x<=1e9)。问你[A,B]区间内有多少个数与x互质。

题解:

1、我们先把x的所有质因子找出来,注意x的最多存在1个质因子大于(sqrt(x)),所以我们可以先预处理出[1,sqrt(1e9)]所有的质数。

2、然后我们把x质因子分解并往下除,最后剩下的要么是1,要么就是大于sqrt(x)的质因子。

3、我们得到的质因子最多15个左右,我们直接利用容斥搞就可以了。我们得出的是与x不互质数的个数,利用总数减去就可。

代码;

时间: 2024-08-30 05:51:36

hdu4135 Co-prime(容斥)的相关文章

POJ 2773 Happy 2006 二分+容斥(入门

题目链接:点击打开链接 题意: 输入n ,k 求与n互质的第k个数(这个数可能>n) 思路: solve(mid)表示[1,mid]中有多少个和n互质,然后二分一下最小的mid 使得互质个数==k solve(x) 实现: 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

数论 + 容斥 - HDU 4059 The Boss on Mars

The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若自己手动推公式的话,还是需要一定的数学基础. 总的思路:先求出sum1=(1^4)+(2^4)+...(n^4),再求出sum2=(1~n中与n不互质的数的四次方的和),answer=sum1-sum2. 如何求sum1呢? 有两种方法: 1.数列差分.由于A={Sn}={a1^4+a2^4+...an^4}

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 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...are a

HDU 3970 Harmonious Set 容斥欧拉函数

链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中任意选一些数使得选出的数和为n的倍数的方法数 ...并不会如何递推.. 思路: 然后这是公式:点击打开链接 a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d). d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi. #pragma comment(linker, "/STA

hdu 1695 GCD 欧拉函数+容斥

题意:给定a,b,c,d,k x属于[1 , c],y属于[1 , d],求满足gcd(x,y)=k的对数.其中<x,y>和<y,x>算相同. 思路:不妨设c<d,x<=y.问题可以转化为x属于[1,c / k ],y属于[1,d/k ],x和y互质的对数. 那么假如y<=c/k,那么对数就是y从1到c/k欧拉函数的和.如果y>c/k,就只能从[ c/k+1 , d ]枚举,然后利用容斥.详见代码: /****************************

HDU 4135 Co-prime(组合+容斥)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other tha

hdu1695:数论+容斥

题目大意: 求x属于[1,b]和 y属于[1,d]的 gcd(x,y)=k 的方案数 题解: 观察发现 gcd()=k 不好处理,想到将x=x/k,y=y/k 后 gcd(x,y)=1.. 即问题转化为求区间 [1,b/k]和 [1,d/k]的互质数对个数 由于题目规定 (x,y)和(y,x)是同一种,所以我们可以规定 x<y,,然后只需对每一个y求出比他小的即可 公共部分可以通过欧拉函数快速求出.. 非公共部分就不行了.. 所以就分解质因数,用容斥的方法求了 #include <iostre

51 nod 1439 互质对(Moblus容斥)

1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开始集合为空.然后有一种操作每次向集合中加入一个数字或者删除一个数字.每次操作给出一个下标x(1 ≤ x ≤ n),如果a[x]已经在集合中,那么就删除a[x],否则就加入a[x]. 问每次操作之后集合中互质的数字有多少对. 注意,集合中可以有重复的数字,两个数字不同当且仅当他们的下标不同. 比如a[