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. Redis实战(十四)Redis实现Session共享

    序言 登录的处理流程: 1.登录页面提交用户名密码. 2.登录成功后生成token.Token相当于原来的jsessionid,字符串,可以使用uuid. 3.把用户信息保存到redis.Key就是t ...

  2. BZOJ 4773: 负环 倍增Floyd

    现在看来这道题就非常好理解了. 可以将问题转化为求两点间经过 $k$ 个点的路径最小值,然后枚举剩余的那一个点即可. #include <cstdio> #include <cstr ...

  3. event.stopPropagation()和event.preventDefault(),return false的区别

    我写公司的官网遇到一个问题,轮播图的上一层有一块内容,用鼠标拖动那块内容的时候下一层的轮播图也会跟着拖动,而上面的那层的内容是不会动的,我想这就是冒泡事件在作祟了吧 跟冒泡事件相关的,我想到三个: 1 ...

  4. Python3学习笔记(八):集合

    集合(set)是一种可变的无序的不重复的数据类型 要创建集合,需要将所有项(元素)放在花括号({})内,以逗号(,)分隔. >>> s = {'p','y','t','h','o', ...

  5. B. Uniqueness

    B. Uniqueness 给定一个序列,要求删除一段连续子段,满足删掉子段后每个元素唯一 求最小子段长度 枚举起点,二分子段长度 记得先sort 再unique #include<bits/s ...

  6. CSS 阴影应用

    1.背景阴影的使用 box-shadow:1px 1px 1px 1px #ccccccc inset; 说明:第一个参数为水平阴影的大小.第二个为垂直阴影的大小(值可以为负数).第三个参数是阴影模糊 ...

  7. sqli-labs(19)

    百度了一下 基于错误的referer头的注入 0X01爱之初体验 猜测是基于referer头的注入 我们在referer后面加入单引号测试一下 真的报错了诶 那我们猜测一下 他应该是把 referer ...

  8. EasyUI combobox下拉框添加水平滚动条和垂直滚动条

    在EasyUI中combobox组件设置滚动条: 1.垂直滚动条:设置panelHeight属性,默认200,组件的数据过多滚动条自动出现,设置auto,则不出现滚动条. 2.水平滚动条:水平滚动条在 ...

  9. 前台ajax传数组,后台java接收

    后端 //添加 @RequestMapping(value = "checkChoise") @ResponseBody ResultJson checkChoise(@Reque ...

  10. python数据类型之可hash,不可hash

    可变类型的数据不可哈希,如list,字典:同值不同址,不同值同址   列表,字典可变, 数值.字母.字符串.数字.元组不可变:同值同址,不同值不同址 怎么判断可变不可变 ?   总结:改个值 看id是 ...