HDU - 4135 Co-prime(容斥原理)

Question

参考

题意
找出[a,b]中与n互质的数的个数
分析
通常我们求1~n中与n互质的数的个数都是用欧拉函数.但如果n比较大或者是求1~m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理。先对n分解质因数,分别记录每个质因数, 那么所求区间内与某个质因数不互质的个数就是 m/r(i),假设r(i)是r的某个质因子 假设只有三个质因子, 总的不互质的个数应该为p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3. pi代表m/r(i),即与某个质因子不互质的数的个数 ,当有更多个质因子的时候,可以用状态压缩解决,二进制位上是1表示这个质因子被取进去了。 如果有奇数个1,就相加,反之则相减

容斥原理
求事件A、B、C的交集时,A U B U C=A+B+C-AB-AC-BC+ABC

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define ull unsigned long long
#define LOCAL

using namespace std;
const int maxn=5000+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;

vector<ll> v;
ll a,b,n;

void getPrime(ll n){//素数分解,要求的n较少时
    v.clear();
    for(ll i = 2;i*i<=n;i++){
        if(n%i==0){
            v.push_back(i);
            while(n%i==0) n/=i;
        }
    }
    if(n>1) v.push_back(n);
    return ;
}

ll solve(ll x,ll n){
    getPrime(n);
    ll sum=0,val,cnt;
    for(ll i=1;i<(1<<v.size());i++){
        val=1;
        cnt=0;
        for(ll j=0;j<v.size();j++){
            if((1<<j)&i){
                val*=v[j];
                cnt++;
            }
        }
        if(cnt&1) sum+=x/val;
        else sum-=x/val;
    }
    return x-sum;
}

int main(){
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){

        cin>>a>>b>>n;
        printf("Case #%d: %I64d\n",i,solve(b,n)-solve(a-1,n));
    }
    return 0;
}
时间: 2024-10-27 06:16:19

HDU - 4135 Co-prime(容斥原理)的相关文章

HDU 4135 Co-prime(容斥原理 + 基础数论)

传送门 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3695    Accepted Submission(s): 1461 Problem Description Given a number N, you are asked to count the number of integers between A an

hdu 4135 Co-prime【容斥原理】

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

[容斥原理] hdu 4135 Co-prime

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 427 Problem Description Given a number N, you are a

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

HDU 1016:Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25156    Accepted Submission(s): 11234 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

容斥原理学习(Hdu 4135,Hdu 1796)

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

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

hdu 4135 容斥原理

又搞了一道容斥原理. 题目:求[1,n]区间对m互质的数有多少个? #include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL __int64 const int maxn = 1e5+8; LL a[maxn],cn,numpri[maxn],vis[maxn]

HDU 4135 Co-prime (容斥原理+质因数分解)

这题只要知道质因数的性质就很容易做了.任意一个正整数(除了1)都可以分解成有限个质数因子的乘积. 那么假如两个数互质,那么这两个数肯定至少各有一个对方没有的质因子.所以若一个数跟n不互质,那么这个的数的质因子肯定也都属于n的质因子,那么就用容斥原理求出所有跟n不互质的所有数的个数.然后再用总的减去即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue&g