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

HDU 4135

时间:2014-11-13 07:00:58      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

http://acm.hdu.edu.cn/showproblem.php?pid=4135

求[A,B]内与N互素的数字个数

首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这个质因数),这样可以得到m个集合(m是N分解出的质因数的个数),对这m个集合用容斥原理解出来它们的并集,再用总数去减

这里学习了用位运算求解容斥原理,非常简单,和状压dp中的位运算差不多感觉,注意容斥中奇加偶减

bubuko.com,布布扣
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

typedef __int64 ll;

ll A,B,N;

vector <ll> v;

ll gao(ll n){
    int m=v.size();
    ll res=0;
    for(int i=1;i<(1<<m);i++){
        int cnt=0;
        ll temp=1;
        for(int j=0;j<m;j++){
            if(i&(1<<j)){
                cnt++;
                temp*=v[j];
            }
        }
        if(cnt&1)res+=n/temp;
        else res-=n/temp;
    }
    return n-res;
}

int main(){
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%I64d%I64d%I64d",&A,&B,&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);
        printf("Case #%d: %I64d\n",cas,gao(B)-gao(A-1));
    }
    return 0;
}
View Code

 

HDU 4135

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/xiaohongmao/p/4093974.html

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