题意:给定区间和n,求区间中与n互素的数的个数, 。


思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于。详细分析见求指定区间内与n互素的数的个数 容斥原理

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;

LL solve(LL r, int n) {
    vector<int>p;
    p.clear();
    for(int i = 2; i*i <= n; ++i) {
        if(n % i == 0) {
            p.push_back(i);
            while(n % i == 0) n /= i;
        }
    }
    if(n > 1) p.push_back(n);
    LL sum = 0;
    for(int msk = 1; msk < (1<<(p.size())); ++msk) {
        LL mult = 1, bits = 0;
        for(int i = 0; i < p.size(); ++i) {
            if(msk & (1 << i)) {
                bits++;
                mult *= p[i];
            }
        }
        LL cur = r / mult;
        if(bits & 1) sum += cur;
        else sum -= cur;
    }
    return r - sum;
}

int main() {
    LL a, b;
    int n;
    int T;
    scanf("%d", &T);
    int kase = 1;
    while(T--) {
        scanf("%lld%lld%d", &a, &b, &n);
        LL x = solve(b, n), y = solve(a-1, n);
        //printf("%lld %lld\n", x, y);
        printf("Case #%d: %lld\n", kase++, x-y);
    }
    return 0;
} 

如有不当之处欢迎指出!

HDU - 4135 Co-prime 容斥定理的更多相关文章

  1. 题解报告:hdu 4135 Co-prime(容斥定理入门)

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

  2. HDU 1695 GCD(容斥定理)

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

  3. HDU 4135 Co-prime(容斥+数论)

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

  4. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

  5. HDU 4135 Co-prime 欧拉+容斥定理

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

  6. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

  7. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

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

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

  9. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

随机推荐

  1. linux_常用命令_2

    rev 反向读取, reverse echo 123456 | rev # 结果为 654321 rev Name.txt # 行号没变,每一行的数据翻转过来 less 具有more命令所有功能,更加 ...

  2. Android之MaterialDesign应用技术2-仿支付宝上滑搜索框缓慢消失

    PS:在这之前也就是上一篇介绍了MaterialDesign一些滑动删除.标题栏的悬浮效果等,如果没看过第一篇的小火鸡可以看一下,因为这篇是接着上一篇写的,有一些东西上一篇写过了这里就不在重复了(Ma ...

  3. javaweb后台转码

    为什么需要转码? 客户端向服务器发送请求的四种情况:1.URL方式直接访问;2.页面链接(属于get请求);3.表单get提交;4.表单post提交 1.url(url和页面链接):各大浏览器.各个操 ...

  4. linux 中 ping的回传值

      今天在学习鸟哥私房菜的过程中,不明白ping的回传值是怎么设置的,后来网上找的结果了,特此记录一下 1 题目大意是指,ping一个网段的机器,如果可以通,就显示UP,如果不通就显示Down,其中一 ...

  5. eclipse:Workspace in use or cannot be created

    打开eclipse出现:Workspace in use or cannot be created, choose a different one 原因:出现这种情况一般是workspace的配置文件 ...

  6. Ubuntu 安装 Nginx 实现反向代理

    安装Nginx依赖库(ubuntu平台) 最近域名通过了备案, 想着应用总不能带着端口号访问吧, 于是在网上踩了很多坑, 终于找到了一步直达的方法,起码这一次很顺利的实现了 安装gcc g++的依赖库 ...

  7. Nginx+Keepalived负载均衡高可用

    Nginx+Keepalived负载均衡高可用方案: Nginx 使用平台:unix.linux.windows. 功能: A.www web服务  http 80 b.负载均衡(方向代理proxy) ...

  8. Cookie简述

    1. Cookie是什么? Cookie(Cookies,浏览器缓存), 是指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). 2. Cookie的作用: ...

  9. java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.classes.views.index_jsp 问题解决方法

    本人使用的是taglib作为模板页,然后碰到的这个问题,如果有类似的可以参考. <%@tag description="Overall Page template" page ...

  10. 分析AJAX抓取今日头条的街拍美图并把信息存入mongodb中

    今天学习分析ajax 请求,现把学得记录, 把我们在今日头条搜索街拍美图的时候,今日头条会发起ajax请求去请求图片,所以我们在网页源码中不能找到图片的url,但是今日头条网页中有一个json 文件, ...