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. Codeforces 962D Merge Equals ( 模拟 )

    题意 : 给出一个序列,然后每次将重复出现的元素进行求和合并(若有多个,则优先取最小的进行合并),若某重复元素有很多,那么取最左边的那两个进行合并且合并后元素位于原来右边元素的位置,例如 3 2 6 ...

  2. #419 Div2 Problem C Karen and Game (贪心 && 暴力)

    题目链接:http://codeforces.com/contest/816/problem/C 题意 :给出一个 n*m 的变化后的矩阵,变化前矩阵的元素全是0,变化的规则是选择其中的一行或者一列将 ...

  3. BeanPostProcessor和BeanFactoryPostProcessor的区别

    官方文档: 在Spring核心的1.8章节 使用BeanPostProcessor自定义Bean BeanPostProcessor 接口定义了您可以实现的回调方法,以提供您自己的(或覆盖容器的默认) ...

  4. NOIP2009靶形数独(暴搜)

    题目传送门 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z博士请教,Z博士拿出了他最近发明 ...

  5. ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari'

    配置Ambari远程maridb 报错: ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari' ...

  6. db2缓冲池调优

    缓存池: 冲池是内存中的一块区域,db2会将用到数据放到缓冲池中提高性能.缓冲池太小,每次查询仍然要到磁盘中操作,达不到缓冲的效果.缓冲池太大,超出操作系统管理的限制,会导致数据库无法连接的错误. 缓 ...

  7. MySQL的explain分析sql语句

    explain分析查询 使用 EXPLAIN 关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的.这可以帮你分析你的查询语句或是表结构的性能瓶颈.通过explain命 ...

  8. ASP.NET对路径"C:/......."的访问被拒绝 解决方法小结 [转载]

    问题: 异常详细信息: System.UnauthorizedAccessException: 对路径“C:/Supermarket/output.pdf”的访问被拒绝. 解决方法: 一.在IIS中的 ...

  9. SAP屏幕事件的控制

    1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT  Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...

  10. cannot assign to struct field xxx in map

    golang 中对 map 类型中的 struct 赋值报错 type s struct{ name string age int}func main(){ a := map[string]s{ &q ...