#include <cstdio>
#include <string.h>
#include <cmath> using namespace std; #define MAXSIZE 40000
#define LL long long
int prim[MAXSIZE];
LL A,B,n,odd,even;
int k;//k记录素数的个数 void findPrim(LL n)
{
k=;
for(int i=; i*i<=n; i++) //对n进行素因子分解
{//筛法求素数
if(n%i==)
{
prim[k++]=i;
while(n%i==)
n/=i;
}
}
if(n>)
prim[k++]=n;
} void getCount(int N)
{
long long mul=;
int count=;
int m=;
while(N){
if(N&) {
count++;
mul*=prim[m];
}
m++;
N>>=;
}
long long t=B/mul-(A-)/mul;
if(count%==) even+=t;
else odd+=t;
} int main()
{
int T,log;
scanf("%d",&T);
for(int x=;x<=T;x++)
{
odd=,even=,log=;
scanf("%I64d%I64d%I64d",&A,&B,&n);
findPrim(n); /*for(int i=0;i<k;i++) printf("%d ",prim[i]);
printf("\n");*/ while(log<(<<k)){
getCount(log);
log++;
}
printf("Case #%d: ",x);
printf("%I64d\n",B-A+-odd+even);
}
return ;
}
 

HDU4135容斥原理的更多相关文章

  1. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  2. HDU4135(容斥原理)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

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

  4. hdu4135 Co-prime 容斥原理

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  5. 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D

    题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...

  6. hdu4135 Co-prime【容斥原理】

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  8. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

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

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

随机推荐

  1. Backbone学习记录(2)

    创建一个集合 1)new Backbone.Collection()方式 var user=new Backbone.Model({'name':'susan'}); var list=new Bac ...

  2. 17972 Golden gun的巧克力

    17972 Golden gun的巧克力 时间限制:1000MS  内存限制:65535K提交次数:93 通过次数:13 收入:124 题型: 编程题   语言: G++;GCC;JAVA Descr ...

  3. DNS练习之正向解析

    配置正向解析: 主机名:sishen63 主机IP:192.168.1.63 操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago) 安 ...

  4. 动手实现 React-redux(四):mapDispatchToProps

    在重构 ThemeSwitch 的时候我们发现,ThemeSwitch 除了需要 store 里面的数据以外,还需要 store 来 dispatch: ... // dispatch action ...

  5. java环境变量配置加maven配置

    1.安装JDK开发环境 下载网站:http://www.oracle.com/ 确定之后,单击“下一步”. 2.配置环境变量: 单击“计算机-属性-高级系统设置”,单击“环境变量”.在“系统变量”栏下 ...

  6. Java GUI 布局管理器

    容器可设置布局管理器,管理容器中组件的布局: container.setLayout(new XxxLayout()); Java有6种布局管理器,AWT提供了5种: FlowLayout Borde ...

  7. spoj GCJ1C09C Bribe the Prisoners

    题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to  ...

  8. Angular和SAP C4C的事件处理队列

    Angular 我们在Angular框架的代码里能看到一个名为processQueue的函数: 这个函数是通过$scope.$apply启动的: 核心代码位于一个for循环里,循环体是一个存储异步处理 ...

  9. JS concat() 方法

    [数组元素的合并] 一. concat() 方法 concat() 方法用于连接两个或多个数组. 返回一个新的数组.该数组是通过把所有 arrayX 参数添加到 arrayObject 中生成的.如果 ...

  10. this.$refs.tabs.activeKey ref就是vue里面的id

    this.$refs.tabs.activeKey ref就是vue里面的id