HDU 4135 Co-prime (分解质因数 + 容斥)

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2314    Accepted Submission(s): 865

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

 

Source

The Third Lebanese Collegiate Programming Contest

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135

题目大意:求区间[A,B]内由多少数字与N互质

题目分析:先预处理出N的质因子,开始开的1e5,wa死,开到5e5就过了,算出[A,B]内有多少数字不与N互质,拿总数减即可,计算不互质的数的个数先把区间转化为[1,a-1]和[1,b]然后直接容斥搞

#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 5e5 + 5;
int fac[MAX];
int cnt;
ll a, b, n, num1, num2;

void get_factor(ll x)
{
    cnt = 0;
    for(int i = 2; i * i < MAX; i++)
    {
        if(x % i == 0)
        {
            fac[cnt ++] = i;
            while(x % i == 0)
                x /= i;
        }
    }
    if(x > 1)
        fac[cnt ++] = x;
}   

void DFS(int idx, ll cur, int sgin, ll s, bool f)
{
    for(int i = idx; i < cnt; i++)
    {
        ll tmp = (ll) cur * fac[i];
        if(f)
            num1 += (ll) sgin * (s / tmp);
        else
            num2 += (ll) sgin * (s / tmp);
        DFS(i + 1, tmp, -sgin, s, f);
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        scanf("%I64d %I64d %I64d", &a, &b, &n);
        get_factor(n);
        num1 = 0;
        DFS(0, 1, 1, b, 1);
        num2 = 0;
        DFS(0, 1, 1, a - 1, 0);
        ll ans = (ll)b - a + 1 - (num1 - num2);
        printf("Case #%d: %I64d\n", ca, ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 14:20:10

HDU 4135 Co-prime (分解质因数 + 容斥)的相关文章

hdu 6059 Kanade&#39;s trio(trie+容斥)

题目链接:hdu 6059 Kanade's trio 题意: 给你n个数,让你找有多少个(i,j,k),使得i<j<k满足a[i]^a[j]<a[j]^a[k]. 题解: 首先考虑a[i]和a[k],将他们都转换成二进制,对于a[i]和a[k],我们用Bi[p]表示二进制下的a[i]的第p位.考虑a[i]和a[k]二进制不同的最高位,这里假设为p,如果Bi[p]=0,Bk[p]=1,那么Bj[p]要为0,才能使得a[i]^a[j]<a[j]^a[k].(因为p前面的位相同,只有亦

hdu 4135 Co-prime(素数分解 容斥)

容斥做,欧拉好像比这个麻烦??? 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 6 using namespace std; 7 typedef long long ll; 8 9 ll solve(ll a,ll n){ 10 vector<ll> v; 11 for(ll i = 2 ; i*i <= n ; i ++){

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1469    Accepted Submission(s): 579 Problem Description There are n people standing in a line. Each of them has a unique id number. Now

hdu 1695 GCD 欧拉函数+容斥

题意:给定a,b,c,d,k x属于[1 , c],y属于[1 , d],求满足gcd(x,y)=k的对数.其中<x,y>和<y,x>算相同. 思路:不妨设c<d,x<=y.问题可以转化为x属于[1,c / k ],y属于[1,d/k ],x和y互质的对数. 那么假如y<=c/k,那么对数就是y从1到c/k欧拉函数的和.如果y>c/k,就只能从[ c/k+1 , d ]枚举,然后利用容斥.详见代码: /****************************

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

HDU 5428 [The Factor] 分解质因数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5428 题目大意:给你若干个整数,让你输出这些数乘积的一个最小因子,并且这个因子至少有3个因子. 关键思想:分解质因数,我们要找的其实就是两个最小质因数的乘积.我的算法关键就是维护最小的两个质因数. 代码如下: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> u

hdu 5471(状压DP or 容斥)

想了最复杂的思路,用了最纠结的方法,花了最长的时间,蒙了一种规律然后莫名其妙的过了. MD 我也太淼了. 后面想了下用状压好像还是挺好写的,而且复杂度也不高.推出的这个容斥的规律也没完全想透我就CAO. Count the Grid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 400    Accepted Submission(s)

HDU 5838 (状压DP+容斥)

Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的点.   n<=5 , m<=5. 解题分析 考虑从1~n*m,从小到大依次填数,则如果某个位置编号为X且该位置还未填数,那么其周围的点均不能填数. 令dp[i][j]表示填到第i个数,状态为j . 令X的个数为cnt,那么 j ∈[ 0 , 1<<cnt). 一种情况为第i个数填在

Lucky HDU - 5213 (莫队,容斥)

WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a stranger with numbers:.The stranger asks him questions.Each question is like this:Given two ranges and ,you can choose two numbers and to make .The you can

HDU 4790:Just Random(容斥)

Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3932 Accepted Submission(s): 1276 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with n