hdu4135 容斥定理

Co-prime

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

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

#include<stdio.h>
#include<string.h>
using namespace std;
long long a[1000],num;
void prime(long long n)
{
    long long i;
    num=0;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            a[num++]=i;
            while(n%i==0)
                n=n/i;
        }
    }
    if(n>1)
        a[num++]=n;
}
long long get(long long m)
{
    long long que[10000],k,t=0,sum=0;
    que[t++]=-1;
    for(int i=0;i<num;i++)
    {
        k=t;
        for(int j=0;j<k;j++)
           que[t++]=que[j]*a[i]*(-1);
    }
    for(int i=1;i<t;i++)
        sum=sum+m/que[i];
    return sum;
}

int main()
{
    long long T,x,y,n,cnt;
    while(scanf("%lld",&T)!=EOF)
    {
        for(int i=1;i<=T;i++)
        {
           scanf("%lld%lld%lld",&x,&y,&n);
           prime(n);
           cnt=y-get(y)-(x-1-get(x-1));
           printf("Case #%d: ",i);
           printf("%lld\n",cnt);
        }
    }
    return 0;
}
时间: 2024-10-11 17:34:11

hdu4135 容斥定理的相关文章

容斥定理 hdu2204 Eddy&#39;s爱好

传送门:点击打开链接 很明显会有大量重复的被计算,所以很容易就想到容斥定理. 我们设dp[i]表示能表示成M^i(i>1)且i是这个数字能表示出来的最大的情况时的总类数 比如,27拆成M^K时的K最大能表示成3,所以27这个数字分在dp[3]这一类 1我们暂时不考虑,不把它放在任何一类 因为K>1,所以K至少是2,最大是2^K=N的时候,所以K最大等于log2(N),所以K非常的小 首先,求出K最大大概的位置 然后开始求dp[i].求法如下: 首先,1~N中有哪些是能拆分成M^i的,利用pow

cf451E Devu and Flowers 卢卡斯定理+容斥定理

题目:http://codeforces.com/problemset/problem/451/E 题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数. 组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理. 卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p) 从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况.

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

HDU 1796 How many integers can you find (容斥定理 + 二进制)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 1567 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

GCD SUM 强大的数论,容斥定理

GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j)

HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7

容斥定理,皮克公式

(来源:哈工大算法培训) 容斥定理:在计算集合的并集的时候,我们经常需要减去重复的部分.但是怎么减比较麻烦.容斥定理就是解决这类问题. 内容: 比如: 皮克公式: 1.如何求多边形面积: 例: 思路:按顺序两点求叉积. S=abs(1/2*((x1*y2-x2*y1)+.......+(xk*yk+1-xk+1*yk)+.........+(xn*y1-x1*yn))). 由于计算的时候可能因为顺逆时针的不同而产生正负.所以加上一个abs. 2.求多边形边上的整数点. 3. 适用于格点图. 原文

题解报告:hdu 4135 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