题意:求出在一个区间[A,B]内与N互质的个数 。

思路:

先求出n的质因子,然后求出与N的质因子不互质的个数然后总个数减去就是。用位运算二进制表示那个因子用到过,实现容斥原理。在1到n之间是c倍数的个数为n/c;

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std; int t;
LL a,b,n;
LL f[];
int cnt; void inti()
{
memset(f,,sizeof(f));
cnt=;
for(int i=; i*i<=n; i++)
{
if(n%i==)
{
f[cnt++]=i;
while(n%i==)
{
n=n/i;
}
}
}
if(n>)
{
f[cnt++]=n;
}
} LL Get_num(LL m)
{
LL ans=;
for(int i=; i<(<<cnt); i++)
{
int xx=;
LL x=;
for(int j=; j<cnt; j++)
{
if(i&(<<j))
{
xx++;
x*=f[j];
}
}
if(xx%!=)
{
ans+=m/x;
}
else
{
ans-=m/x;
}
}
return m-ans;
} int main()
{
scanf("%d",&t);
for(int cas=; cas<=t; cas++)
{
scanf("%I64d%I64d%I64d",&a,&b,&n);
inti();
printf("Case #%d: %I64d\n",cas,Get_num(b)-Get_num(a-));
}
return ;
}

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. js复选框全选

    <input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'&g ...

  2. bzoj3669: [Noi2014]魔法森林 lct

    记得去年模拟赛的时候好像YY出二分答案枚举a,b的暴力,过了55欸 然后看正解,为了将两维变成一维,将a排序,模拟Kruskal的加边过程,同时维护1到n的最大值,加入一条边e(u,v,a,b)时有以 ...

  3. JS常用扩展

    // 清除两边的空格 String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ''); }; // 合并多 ...

  4. [PWA] 7. First Cache when installed

    If you want your application works offline or lie-wifi. You need to use cache. API: Create Caches: c ...

  5. Go语言学习笔记(一) : 搭建Windows下的Go开发环境

    最近突然对Go语言产生了兴趣,主要是因为在使用python的时候遇到了一些不爽的问题,然后发现了Go.Go是Google出的一个动态语言,语法和C++接近,性能也非常的好,而且还支持编译成exe发布, ...

  6. MyBatis报错

    1.错误描写叙述 2014-11-2 15:03:11 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engin ...

  7. android中的Handler和Runnable

    最近在做一个项目,在网络请求时考虑用Handler进行处理,然后就研究了一下Handler和Runnable 首先在看一下java中的Runnable The Runnable interface s ...

  8. 构建服务端的AMD/CMD模块加载器

    本文原文地址:http://trock.lofter.com/post/117023_1208040 . 引言:  在前端开发领域,相信大家对AMD/CMD规范一定不会陌生,尤其对requireJS. ...

  9. Day2 - Python基础2 列表、字典、集合

    Python之路,Day2 - Python基础2   本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一, ...

  10. NPOI从数据库中导出数据到Excel

    首先要添加NPOI.dll程序集 https://yunpan.cn/cMeSTELJSXmJJ  访问密码 8d83 把里面的程序集都添加到引用里 下面的代码是从数据库导出到Excel { //pa ...