zoukankan      html  css  js  c++  java
  • HDU 4135 Co-prime

    Co-prime

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 4135
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:容斥+数论
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 vector<LL>g;
     5 LL solve(LL x,LL y) {
     6     g.clear();
     7     for(LL i = 2; i*i <= y; ++i) {
     8         if(y%i == 0) {
     9             while(y%i == 0) y /= i;
    10             for(int j = g.size()-1; j >= 0; --j)
    11                 if(abs(i*g[j] <= x)) g.push_back(-i*g[j]);
    12             g.push_back(i);
    13         }
    14     }
    15     if(y > 1) {
    16         for(int i = g.size()-1; i >= 0; --i)
    17             if(abs(y*g[i]) <= x) g.push_back(-y*g[i]);
    18         g.push_back(y);
    19     }
    20     LL ret = 0;
    21     for(int i = g.size()-1; i >= 0; --i) ret += x/g[i];
    22     return x - ret;
    23 }
    24 int main() {
    25     int t,cs = 1;
    26     LL a,b,n;
    27     scanf("%d",&t);
    28     while(t--){
    29         scanf("%I64d%I64d%I64d",&a,&b,&n);
    30         printf("Case #%d: %I64d
    ",cs++,solve(b,n) - solve(a-1,n));
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    自己用的,存储代码
    ASCII编码表
    全球最热门编程语言20种
    C++中二维数组new小结
    字符,字节和编码
    让工资涨的快的小技巧
    Ip Messenger
    xajax中文手册
    BitmapFile Formats(BMP文件的格式)
    python中返回字符串中指定字符的索引
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4802017.html
Copyright © 2011-2022 走看看