• 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
  • 相关阅读:
    博客园第一篇随笔css3动画(奔跑的小杨)
    Python输出菱形
    Android开发经验总结
    Android中Activity共享变量的另一方法:Application context
    system()与execv()函数使用详解
    Sublime Text2 编译和运行C/C++程序(windows)
    Android View.post(Runnable )
    Android图像处理之Bitmap类
    android中dip、dp、px、sp和屏幕密度
    System.setProperty and System.getProperty
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4802017.html
Copyright © 2020-2023  润新知