zoukankan      html  css  js  c++  java
  • hdu4135 Co-prime【容斥原理】

    Co-prime

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

    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
     
    Recommend
    lcy
     
    就是求区间与2互质的数的个数,就是简单容斥吧,用dfs做会比较好。
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #define ll long long
     8 using namespace std;
     9 
    10 ll ans,l,r,k;
    11 int num,cnt;
    12 ll a[107],prime[100007];
    13 bool boo[100007];
    14 int Case;
    15 
    16 void init_prime()
    17 {
    18     for (int i=2;i<=100000;i++)
    19     {
    20         if (!boo[i]) prime[++cnt]=i;
    21         for (int j=1;j<=cnt&&prime[j]*i<=100000;j++)
    22         {
    23             boo[prime[j]*i]=1;
    24             if (i%prime[j]==0) break;
    25         }
    26     }
    27 }
    28 void devide_prime()
    29 {
    30     num=0;
    31     for (int i=1;i<=cnt;i++)
    32     {
    33         if (k%prime[i]==0)
    34         {
    35             a[++num]=prime[i];
    36             while(k%prime[i]==0) k/=prime[i];
    37         }
    38     }
    39     if (k>1) a[++num]=k;
    40 }
    41 void query(int deep,ll shu,ll qz,ll zhi)
    42 {
    43     if (deep>num)
    44     {
    45         ans+=qz*zhi/shu;
    46         return;
    47     }
    48     query(deep+1,shu*a[deep],-qz,zhi);
    49     query(deep+1,shu,qz,zhi);
    50 }
    51 int main()
    52 {
    53     init_prime();
    54     int cas;
    55     scanf("%d",&cas);
    56     while(cas--)
    57     {
    58         scanf("%lld%lld%lld",&l,&r,&k);
    59         devide_prime();ans=0;
    60         query(1,1,1,r);
    61         query(1,1,-1,l-1);
    62         printf("Case #%d: %lld
    ",++Case,ans);
    63     }
    64 }
  • 相关阅读:
    WQS二分
    题解 洛谷 P4696 【[CEOI2011]Matching】
    原根
    单位根反演
    题解 洛谷 P4218 【[CTSC2010]珠宝商】
    题解 洛谷 P5434 【有标号荒漠计数】
    题解 洛谷 P5406 【[THUPC2019]找树】
    题解 洛谷 P3563 【[POI2013]POL-Polarization】
    题解 洛谷 P6078 【[CEOI2004]糖果】
    拉格朗日插值法
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7700773.html
Copyright © 2011-2022 走看看