Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6425    Accepted Submission(s): 2569

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
 
Recommend
lcy

AC代码:

 1 #include<iostream>
2 #include<cstdio>
3 #include<vector>
4 typedef long long ll;
5
6 using namespace std;
7
8 long long solve(long long n, long long r)
9 {
10 vector<int> p;
11 p.clear();
12 for(ll i = 2; i * i <= n; i++)
13 {
14 if(n % i == 0)
15 {
16 p.push_back(i);
17 while(n % i == 0)
18 {
19 n /= i;
20 }
21 }
22 }
23 if(n > 1)
24 p.push_back(n);
25 ll sum = 0;
26 for(ll msk = 1; msk < ((ll)1 << p.size()); msk++)
27 {
28 ll mult = 1, bits = 0;
29 for(ll i = 0; i < p.size(); i++)
30 {
31 if(msk & ((ll)1 << i))
32 {
33 bits++;
34 mult *= p[i];
35 }
36 }
37 ll cur = r / mult;
38 if(bits % 2 == 1)
39 sum += cur;
40 else
41 sum -= cur;
42 }
43 return r-sum;
44 }
45
46 int main()
47 {
48 int t;
49 long long a, b, n;
50 scanf("%d", &t);
51 int cas = 1;
52 while(t--)
53 {
54 scanf("%lld %lld %lld", &a, &b, &n);
55 long long f1 = solve(n, b);
56 long long f2 = solve(n, a-1);
57 printf("Case #%d: %lld\n", cas++, f1 - f2);
58 }
59 return 0;
60 }

Co-prime(容斥原理)的更多相关文章

  1. Codeforces1036F Relatively Prime Powers 【容斥原理】

    题目分析: 这种题目标题写莫比乌斯反演会不会显得太恐怖了,那就容斥算了. gcd不为1的肯定可以开根.所以把根式结果算出来就行了. 辣鸡题目卡我精度. 代码: #include<bits/std ...

  2. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  3. HDU 2204Eddy's爱好(容斥原理)

    Eddy's爱好 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Sta ...

  5. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 2014 Super Training #3 H Tmutarakan Exams --容斥原理

    原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...

  7. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. POJ 3904 Sky Code (容斥原理)

    B - Sky Code Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  10. HDU4135 Co-prime(容斥原理)

    题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...

随机推荐

  1. Linux下搭建ZooKeeper集群

    摘要 Zookeeper是针对大型分布式系统的高可靠的协调系统.它基于对Paxos算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得ZooKeeper解决很多分布式问题. ...

  2. spring-Cloud初步依赖

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  3. PAT-1140(Look-and-say Sequence)字符串处理

    Look-and-say Sequence PAT-1140 #include<iostream> #include<cstring> #include<string&g ...

  4. 主机回来以及,简单的环境配置(RTX3070+CUDA11.1+CUDNN+TensorRT)

    紧接着前几天的事: 特殊的日子,想起了当年的双(1080TI)显卡装机实录 和 炼丹炉买不起了:聊一聊这段日子的显卡行情 之后,决定买一台整机玩玩. 而现在,主机终于回!来!了!主机回来干什么,当然是 ...

  5. MyBatis(一):JDBC使用存在的问题

    JDBC使用步骤: a:加载 JDBC 驱动程序 b:创建数据库的连接对象Connection c:根据链接获取Statement d:拼接SQL语句及设置参数 e:执行SQL并获取结果集 f:关闭使 ...

  6. LZZY高级语言程序设计之169页**5.17

    import java.util.Scanner;public class MQ3 { public static void main(String[] args) { Scanner sc = ne ...

  7. 【H264】视频编码发展简史

    一.常见视频编码格式 编码格式有很多,如下图: 目前比较常用的编码有: H26x系列:由ITU(国际电传视讯联盟)主导,侧重网络传输 MPEG系列:由ISO(国际标准组织机构)下属的MPEG(运动图象 ...

  8. MySQL入门(5)——运算符

    MySQL入门(5)--运算符 算术运算符 MySQL支持的算数运算符包括加.减.乘.除.求余. 符号 作用 + 加法运算 - 减法运算 * 乘法运算 / 除法运算 % 求余运算 DIV 除法运算,返 ...

  9. 数据搬运组件:基于Sqoop管理数据导入和导出

    本文源码:GitHub || GitEE 一.Sqoop概述 Sqoop是一款开源的大数据组件,主要用来在Hadoop(Hive.HBase等)与传统的数据库(mysql.postgresql.ora ...

  10. 文件锁fcntl

    一.python中的文件锁 我们在写python应用的时候,当涉及到多个进程向同一个文件write(或者read)的情况,如果几个进程同时都对这个文件进行写操作,那么文件的内容就会变得非常混乱,这个时 ...