码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4135 容斥原理

时间:2014-11-05 23:18:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   os   for   sp   on   2014   log   

又搞了一道容斥原理。

题目:求【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],dis[maxn];
LL n,m;
LL f[maxn];
void getprim(){
    cn = 0;
    for(LL i=2;i<10000;i++){
        if(!dis[i]){
            numpri[cn++] = i;
            for(LL j=2*i;j<=10000;j+=i)
                dis[j] = 1;
        }
    }
}
LL getans(LL Max,LL d){
    vector <int > v;
    LL sum = 0;
        for(LL j=0;j<cn&&numpri[j] * numpri[j] <=d;j++){
            if(d % numpri[j] == 0){
                v.push_back(numpri[j]);
                while(d % numpri[j] == 0)d /= numpri[j];
            }
        }
        if(d!=1)v.push_back(d);
        int len = v.size();
        //cout << len <<endl;
        for(int j=0;j<(1<<len);j++){
            LL op =0,ans = 1;
            for(int k=0;k<len;k++){
                if((1<<k)&j){
                    op++;
                    ans*=v[k];
                }
            }
            if(op&1)sum-= Max/ans;
            else sum += Max/ans;
        }
    return sum;
}
int su = 0;
int main(){
    LL T;
    LL a,b,c;
    scanf("%I64d",&T);
    getprim();
    while(T--){
        cin >> a>>b>>c;
       // cout << getans(b,c)<<endl;
       printf("Case #%d: %I64d\n",++su,getans(b,c) - getans(a-1,c));

    }
}


 

hdu 4135 容斥原理

标签:style   blog   io   os   for   sp   on   2014   log   

原文地址:http://blog.csdn.net/wyl_zheyang/article/details/40833297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!