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. Pandas初体验

    目录 Pandas 一.简介 1.安装 2.引用方法 二.series 1.创建方法 2.缺失数据处理 2.1 什么是缺失值 2.2 NaN特性 2.3 填充NaN 2.4 删除NaN 2.5 其他方 ...

  2. POJ-1182(经典带权并查集)

    食物链 POJ-1182 一个很好的分析博客:https://blog.csdn.net/niushuai666/article/details/6981689 三种关系:两者同类,吃父节点,被父节点 ...

  3. Python3.x 基础练习题100例(81-90)

    练习81: 题目: 809??=800??+9?? 其中??代表的两位数, 809??为四位数,8??的结果为两位数,9??的结果为3位数.求??代表的两位数,及809*??后的结果. 程序: a = ...

  4. AOP(面向切面编程)大概了解一下

    前言 上一篇在聊MemoryCache的时候,用到了Autofac提供的拦截器进行面向切面编程,很明显能体会到其优势,既然涉及到了,那就趁热打铁,一起来探探面向切面编程. 正文 1. 概述 在软件业, ...

  5. apicloud打包的ios证书的获取方法

    apicloud云编译的时候,需要测试证书或者正式证书进行编译. 那么这个证书是怎么来的呢?通过什么渠道可以获取呢? 这里我介绍下使用香蕉云编这个在线工具来生成: 1.登录香蕉云编,生成证书的csr文 ...

  6. Apache配置 2.用户认证

    1.用户认证用来对某些目录中的网页进行访问控制,当用户访问这些页面的时候需要输入用户名和密码进行认证. 2. 配置: # vim /usr/local/apache2.4/conf/extra/htt ...

  7. NIO三大组件之Selector选择器

    什么是选择器 选择器的作用是完成IO的多路复用.一个通道代表一条连接通路,通过选择器可以同时监控多个通道的IO(输入输出)状况.选择器和通道的关系,是监控和被监控的关系. 使用 重要的成员 Selec ...

  8. javascript 之对象-13

    对象 无序属性的集合,属性可以包含基本值.对象或者函数,简单理解为对象是若干属性的集合:我们常说的面向对象(oop)编程其实是指的一种编码的思想,简单理解为用对象来封装数据,利用封装.继承.多态对代码 ...

  9. Codeforces Round #558 B2. Cat Party (Hard Edition)

    题面: 传送门 题目描述: 题意:确定最大的x,使去除掉前x天的其中一天后,所有不同数字的数量相等.   题目分析: 可能是我太久没打cf了,水题都做不出来. 这道题的关键在于:要记录相同数量,的不同 ...

  10. HDFS设置配额的命令

    1 文件个数限额 #查看配额信息 hdfs dfs -count -q -h /user/root/dir1 #设置N个限额数量,只能存放N-1个文件 hdfs dfsadmin -setQuota ...