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

HDU 4135 Co-prime

时间:2015-08-18 11:43:02      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2371    Accepted Submission(s): 887


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

 

Input
The 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 <= 1015) and (1 <=N <= 109).
 

 

Output
For 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}.
 

 容斥原理...

/* ***********************************************
Author        :PK28
Created Time  :2015/8/18 9:29:57
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
ll a,b,n;
ll solve(){
    ll sum=0;
    vector<ll>v;
    for(int 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);
    for(ll st=1;st<(1<<(v.size()));++st){//0 1
        ll bits=0,mult=1;
        for(int i=0;i<(int)v.size();++i){
            if(st&(1<<i)){
                ++bits;
                mult*=v[i];
            }    
        }
        ll cur=b/mult-a/mult;
        if(a%mult==0)cur++;
        if(bits&1)sum+=cur;
        else sum-=cur;
    }
    return b-a-sum+1;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin); 
    #endif
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        scanf("%I64d %I64d %I64d",&a,&b,&n);
        printf("Case #%d: %I64d\n",i,solve());
    }
    return 0;
}

 

HDU 4135 Co-prime

标签:

原文地址:http://www.cnblogs.com/pk28/p/4738724.html

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