hdu4135---Co-prime(容斥原理)

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

Recommend

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

先考虑区间[1, x]里和n不互质的数个数

考虑n的每个素因子

可以被某个素因子整除的数个数为(int)x / pi

但是不能这么单纯算,某些数会被多次统计

用容斥来搞

这个数的素因子不多

所以可以状压来搞出所有组合然后 奇加偶减

/*************************************************************************
    > File Name: hdu4135.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月26日 星期二 20时37分52秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

vector <int> fac;

LL calc(LL m) {
    int n = fac.size();
    LL ans = 0;
    for (int i = 1; i < (1 << n); ++i) {
        int cnt = 0;
        int getf = 1;
        for (int j = 0; j < n; ++j) {
            if (i & (1 << j)) {
                getf *= fac[j];
                ++cnt;
            }
        }
        if (cnt % 2) {
            ans += (m / getf);
        }
        else {
            ans -= (m / getf);
        }
    }
    return m - ans;
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        LL l, r;
        int n;
        cin >> l >> r >> n;
        fac.clear();
        for (int i = 2; i * i <= n; ++i) {
            if (n % i == 0) {
                fac.push_back(i);
                while (n % i == 0) {
                    n /= i;
                }
            }
        }
        if (n > 1) {
            fac.push_back(n);
        }
        cout << "Case #" << icase++ << ": " << calc(r) - calc(l - 1) << endl;
    }
    return 0;
}
时间: 2024-08-16 07:28:09

hdu4135---Co-prime(容斥原理)的相关文章

HDU4135 (求a~b内与n互素的数的个数) 容斥原理

掌握了容斥原理后,便会发现,这是一道简单的容斥原理的题. 题目描述:给定A, B, N (1 <= A <= B <= 10^15,1<=N <= 10^9).求[A,B]区间内与N互素的数的个数 步骤: (1)将问题化为求1~B,1~A中与N互素的数的个数的差,当然考虑到A可能与N互素的情况,在实际操作时, 即求1~A时最好改成求1~A-1(包含A-1): (2)求N的质因子(可参考:http://blog.csdn.net/yzj577/article/details/3

Hdu4135容斥原理

求A到B之间有多少个数能与N互质. 学了下容斥原理的写法, 想将N分解质因数,然后容斥原理,N - 单个质因数倍数个数+2个质因数倍数的个数-3个质因数的个数...... #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<iostream> #include<string> #include<queue> #include<stack> #include<list>

HDU4135(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4090    Accepted Submission(s): 1619 Problem Description Given a number N, you are asked to count the number of integers between A and B

ACM 容斥原理

VJ 点击打开链接 参考 点击打开链接 非常好的译文:点击打开链接 容斥原理的想法就是求多个集合的并集.所以要先设计好集合. 组合数学问题中,正面解决会困难,常用方法是正难则反,使用容斥原理求反向在用全集减去.将对立面的限制条件分析清楚. eg 求区间互质的数的个数,则用除法等计算出一个数的倍数的方法再减去. UVa 11806 Cheerleaders 求k个石子放在n*m的矩阵里 并且第一行 最后一行 第一列 最后一列都要有石子 考虑反面 求出所有的 减去不满足的情况 容斥原理总共4个 集合

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

Co-prime(hdu4135)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3313    Accepted Submission(s): 1286 Problem Description Given a number N, you are asked to count the number of integers between A and B

[容斥原理] hdu 4135 Co-prime

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 427 Problem Description Given a number N, you are a

容斥原理 求M以内有多少个跟N是互质的

开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理! 本题是求[a,b]中与n互质的数的个数,可以转换成求[1,b]中与n互质的数个数减去[1,a-1]与n互质的数的个数. #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define LL long

poj 2773(容斥原理)

容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3341 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7,

HDU-4407-Sum(容斥原理)

Problem Description XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds. Operation 1: among the x-th number to the y-th number (inclusive), get the sum o