http://acm.hdu.edu.cn/showproblem.php?pid=4135

给定一个数n,求某个区间[a,b]内有多少数与这个数互质。

对于一个给定的区间,我们如果能够求出这个区间内所有与其不互质的数的个数的话,那么互质数的个数也就求出来。

任何与N不互质的数一定是其某一个质因子的倍数,所以我们通过某一个质因子能够确定一个范围内的有多少个数该质因子的倍数的数。但是这并不是所有不互质数的充分条件,对于另外的质因子同样满足能够确定一系列的数,我们要做的就是容斥定理求他们的并集。num除以奇数个数相乘的时候是加,num除以偶数个数相乘的时候是减。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 1e5+5;
LL n,a,b;
vector<LL> q;
LL ans(LL x)
{
LL res = 0;
for(LL i = 1;i < (1LL<<q.size());++i){
LL u = 1,cnt = 0;
for(int j = 0;j < q.size();++j)
if(i & (1LL<<j))
cnt++,u *= q[j];
if(cnt & 1)
res += x / u;
else
res -= x / u;
}
return res;
}
int main() {
int _;RD(_);for(int cas = 1;cas <= _;++cas){
printf("Case #%d: ",cas);
RD3(a,b,n);
q.clear();
for(LL j = 2;j * j <= n;++j)if(n%j == 0){
q.push_back(j);
while(n%j == 0)
n/=j;
}
if(n != 1)
q.push_back(n);
printf("%I64d\n",b - ans(b) - (a - 1 - ans(a-1))); }
return 0;
}

hdu 4135 [a,b]中n互质数个数+容斥的更多相关文章

  1. hdu 1796 How many integers can you find 容斥第一题

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  4. HDU 1796 How many integers can you find (容斥)

    题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...

  5. HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)

    pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol:  因为N比较小,我们可以求出素因子,然后容斥.  主要问题就是求1到P的 ...

  6. HDU 1796 How many integers can you find 容斥入门

    How many integers can you find Problem Description   Now you get a number N, and a M-integers set, y ...

  7. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  8. hdu 1796 How many integers can you find 容斥定理

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. hdu 4135 a到b的范围中多少数与n互质(容斥)

    Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...

随机推荐

  1. redis 哨兵(sentinel)

    redis哨兵 哨兵自动故障转移 自动通知应用最新master信息 无需担心,master挂了,程序不需要修改IP啥的,由哨兵自动完成 修改sentinel.conf protected-mode n ...

  2. iOS常用代码总结

    1.读取图片NSString *path = [[NSBundle mainBundle] pathForResource"icon" ofType"png"] ...

  3. VS2010下MFC的串口编程

    串口通信简介 一般来说,计算机都有一个或多个串行端口,这些串口提供了外部设备与PC进行数据传输和通信的通道,在CPU和外设之间充当解释器的角色.当字符数据从CPU发送给外设时,这些字符数据将被转换成串 ...

  4. 201621123008 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...

  5. Eclipse生成部署描述符(web.xml)

    右键点击你的web项目名--->Java EE Tools-->Generate Deployment  Descriptor Stub 要想自动生成,只需在创建web项目时,把最后一页的 ...

  6. Nodejs学习笔记:基础

    本文章主要记录Nodejs基础知识点 安装 首先从Node.js官网下载安装包,并添加到环境变量.然后打开命令行,输入 node --version ,可查看版本信息 npm是Node.js的包管理工 ...

  7. windows 8 update to windows 8.1

    可以参考以下几个链接: http://blogs.windows.com/windows/b/appbuilder/archive/2013/07/24/windows-8-to-windows-8- ...

  8. java:static详解

    1.static修饰的变量习惯称为静态变量,static修饰的方法称为静态方法,static修饰的代码块叫做静态代码块. 1)static变量 static变量也称作静态变量,静态变量和非静态变量的区 ...

  9. 695. Max Area of Island

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  10. 2018.11.09 bzoj2165: 大楼(倍增+floyd)

    传送门 先倍增出iii使得2i2^i2i时间时刚好有每个点能够到mmm层及以上. 然后就可以用floyd+floyd+floyd+倍增求出刚好不超过mmm层的时间,最后再补一层就行了. 代码: #pr ...