hdu 4135 A-B中,与N不互质的数

传送门

Co-prime

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


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 <= 10 15) and (1 <=N <= 10 9).
 

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

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1796  1434  3460  1502  4136 
 

求A-B中,与N不互质的数。也就是说求1-B中与N互质的数。再求1-(A-1)中与N互质的数,用B-(A-1)-互质的数=不互质的数。但有一个容斥在里面,因为N可以分解,

p1^a1*p2^a2*p3^a3````。奇加偶减。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e4+10;
const int maxx=1e6+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline LL Scan()
{
	LL Res=0,ch,Flag=0;
	if((ch=getchar())=='-')Flag=1;
	else if(ch>='0' && ch<='9')Res=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
	return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int tot=0,cnt=0,vis[maxx];
LL pr[maxx],a[maxx/100];
LL A,B;
int N;
void init()
{
    tot=0;
    int len = sqrt(maxx) + 1;
    for (int i = 2; i <= len; ++i)if (!vis[i])
    {
        pr[tot++]=i;
        for (int j = i*i; j <= maxx; j += i)vis[j] = 1;
    }
}
void pri(LL x)
{
    cnt=0;
    for(int i=0;i<tot&&pr[i]*pr[i]<=x;i++)
    {
        if(x%pr[i]==0)
        {
            a[cnt++]=pr[i];
            while(x%pr[i]==0)
                x/=pr[i];
        }
    }
    if(x>1)
        a[cnt++]=x;
}
LL solve(LL x)
{
    LL st=(1<<cnt);
    LL ret=0;
    for(LL i=1;i<st;i++)
    {
        LL t=i,k=0,tmp=1;
        for(LL j=0;j<cnt;j++)
        {
            //cout<<t<<endl;
            if(t&1)
            {
                k++;
                tmp*=a[j];
            }
            t>>=1;
        }
        if(k&1)
            ret+=x/tmp;
        else ret-=x/tmp;
    }
    return ret;
}
int main()
{
    init();
    int t;
    t=Scan();
    for(int cas=1;cas<=t;cas++)
    {
        A=Scan();B=Scan();N=Scan();
        pri(N);
        LL ans=B-solve(B)-A+1+solve(A-1);
        printf("Case #%d: %lld\n",cas,ans);
    }
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值