• Co-prime 杭电4135


    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 than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.

    InputThe first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10 15) and (1 <=N <= 10 9).OutputFor each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.Sample Input

    2
    1 10 2
    3 15 5

    Sample Output

    Case #1: 5
    Case #2: 10
    
    
            
     

    Hint

    In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}. 
     题目大意:给你3个数A,B,C,让你求出从A到B的所有数字中与C互质的个数,
    题解:直接求互质不好求,我们就求与C不互质的个数,然后最后在减去就可以了
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int N=1E5+7;
    ll arr[N];
    vector<ll >ve;
    int main(){
        int t,k=0;
        scanf("%d",&t);
        while(t--){
            k++;
            ll a,b,c,pos=0;
            scanf("%lld%lld%lld",&a,&b,&c);
            for(int i=2;i*i<=c;i++){
                if(c%i==0){
                    pos++;
                    ve.push_back(i);
                    while(c%i==0) c/=i;
                }
            }
            if(c>1) 
            {
                pos++;
                ve.push_back(c);
            }
            ll sa=0,sb=0;
            for(ll i=1;i<(1<<pos);i++){
                ll sum=1,cnt=0;
                for(ll j=0;j<pos;j++){
                    if(1&(i>>j)){
                        sum*=ve[j];
                        cnt++;
                    }
                }
                if(cnt&1){//容斥里的奇减偶加 
                    sa+=(a-1)/sum;//a-1前有多少个数字是sum的倍数, 
                    sb+=(b)/sum;
                }
                else {
                    sa-=(a-1)/sum;
                    sb-=(b)/sum;
                }
            }
            sb=sb-sa;//应题目要求  从A到B 
            printf("Case #%d: %lld
    ",k,b-a+1-sb);
            ve.clear();
        }
        return 0;
    } 
  • 相关阅读:
    前端安全
    关于HTTPS的概念性了解
    数组去重
    防抖与节流
    对meta标签的再次认识
    关于路由, 我好奇的那些点
    关于构造函数,实例,原型对象一纯手工的理解
    数据库查找操作-java
    python之图像加载和简单处理
    python之excel表格操作
  • 原文地址:https://www.cnblogs.com/Accepting/p/11360461.html
Copyright © 2020-2023  润新知