Co-prime

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

The Third Lebanese Collegiate Programming Contest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<cstring>
#include<cstdio>
  
using namespace std;
typedef long long LL;
const int N = 1e5+5;
  
LL f[N],prime[N],vis[N],cnt,k;
void prime_factor(){
    memset(vis,0,sizeof(vis));
    vis[0]=vis[1] = 1,cnt = 0;
    for(LL i=2;i*i<N;i++)
    if(!vis[i]) for(LL j=i*i;j<N;j+=i) vis[j] = 1;
    for(LL i=0;i<N;i++) if(!vis[i]) prime[cnt++] = i;
}
LL poie(LL x){
    LL ret = 0,sum,tmp;
    for(LL i=1;i<(1LL<<k);i++){
        tmp = 1,sum=0;
        for(LL j=0;j<k;j++) if(i&(1LL<<j)){sum++,tmp*=f[j];}
        if(sum&1) ret += x/tmp;
        else ret -= x/tmp;
    }
    return ret;
}
  
void solve_question(LL A,LL B,LL n){
    LL tmp = n;
    k = 0 ;
    for(LL i=0;prime[i]*prime[i]<= tmp;i++){
        if(tmp%prime[i]==0)
            f[k++] = prime[i];
        while(tmp%prime[i]==0)
            tmp/=prime[i];
    }
    if(tmp > 1) f[k++] = tmp;
    LL ans =B-poie(B)-A+1+poie(A-1);
    printf("%I64d\n",ans);
}
int main(){
    int T,Case=0;
    LL A,B,n;
    scanf("%d",&T);
    prime_factor();
    while(T--){
        scanf("%I64d %I64d %I64d",&A,&B,&n);
        printf("Case #%d: ",++Case);
        solve_question(A,B,n);
    }
}
 

数学: HDU Co-prime的更多相关文章

  1. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  4. HDU 1016 Prime Ring Problem

    在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...

  5. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  6. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  7. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  8. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  9. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. forEach、map、filter、reduce的区别

    1.相同点: 都会循环遍历数组中的每一项: map().forEach()和filter()方法里每次执行匿名函数都支持3个参数,参数分别是:当前元素.当前元素的索引.当前元素所属的数组: 匿名函数中 ...

  2. JSP如何实现文件断点上传和断点下载?

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  3. Spoj4060 game with probability Problem

    题目链接:Click here Solution: 刚开始还以为博弈论加概率,然而并不是... 设两个状态:\(f(i)\)表示当前剩下\(i\)个石头时,先手的获胜概率,\(g(i)\)为后手的获胜 ...

  4. python双划线类型

    通过__开始的属性感觉分为两种类型: 利用dir调用的时候: < build in x object at 0x01F116B0>:这种貌似和运算符重载有关系,用户如果需要的话可以重写该函 ...

  5. the path component: '/var' is world-writable

    java.io.IOException: the path component: '/var' is world-writable.  Its permissions are 0666.  Pleas ...

  6. Centos7源码安装jdk

    1. 下载jdk  : jdk-8u211-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...

  7. 为什么电脑连接不上FTP

    我们对服务器的FTP状况有实时监控,一般问题都不在服务器端. 而且我们客服一般会第一时间测试下您空间FTP是否真的不能连接 99%绝大部分FTP连接不上的问题,都是客户那边的软件端或网络问题. 问题分 ...

  8. xsens melodic ros driver

    sudo apt-get update sudo apt-get install ros-melodic-xsens-driver 设置数据输出: // 输出四元数,加速度.角速度.地磁 python ...

  9. Mybaits 运行原理流程时序图

    1 .初始化sqlsessionFactory 2openSession 3.getMapper返回接口的代理对象 包含了SqlSession对象 4.查询流程

  10. 二、Jmter查看结果数只能显示有限的数据,查看全部数据

    1.打开jmeter安装目录,找到bin目录下jmeter.properties文件 2.搜索:view.results.tree.max_size=10485760 3.将#号去掉,重启jmeter