zoukankan      html  css  js  c++  java
  • Co-prime(容斥原理)

    Co-prime

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


    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

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<vector>
     4 typedef long long ll;
     5 
     6 using namespace std;
     7 
     8 long long solve(long long n, long long r)
     9 {
    10     vector<int> p;
    11     p.clear();
    12     for(ll i = 2; i * i <= n; i++)
    13     {
    14         if(n % i == 0)
    15         {
    16             p.push_back(i);
    17             while(n % i == 0)
    18             {
    19                 n /= i;
    20             }
    21         }
    22     }
    23     if(n > 1)
    24         p.push_back(n);
    25     ll sum = 0;
    26     for(ll msk = 1; msk < ((ll)1 << p.size()); msk++)
    27     {
    28         ll mult = 1, bits = 0;
    29         for(ll i = 0; i < p.size(); i++)
    30         {
    31             if(msk & ((ll)1 << i))
    32             {
    33                 bits++;
    34                 mult *= p[i];
    35             }
    36         }
    37         ll cur = r / mult;
    38         if(bits % 2 == 1)
    39             sum += cur;
    40         else
    41             sum -= cur;
    42     }
    43     return r-sum;
    44 }
    45 
    46 int main()
    47 {
    48     int t;
    49     long long a, b, n;
    50     scanf("%d", &t);
    51     int cas = 1;
    52     while(t--)
    53     {
    54         scanf("%lld %lld %lld", &a, &b, &n);
    55         long long f1 = solve(n, b);
    56         long long f2 = solve(n, a-1);
    57         printf("Case #%d: %lld
    ", cas++, f1 - f2);
    58     }
    59     return 0;
    60 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    C# .NET 在一个线程中访问另一个线程的控件 方法汇总 (转载)
    SQL Server 2008数据库复制实现数据库同步备份(2) (转载)
    对《30个提高Web程序执行效率的好经验》的理解
    Web开发常用速查手册大全(100+)(转载)
    web.config 文件详解(转载)
    常用Web Service 收藏
    Microsoft training Kits
    SSH+JQuery整合
    DWR框架在SHH中的配置说明
    卡尔曼滤波器 Kalman Filter
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8724578.html
Copyright © 2011-2022 走看看