题目链接

题意:问从A到B中与N互素的个数。

题解:

利用容斥原理:先求出与n互为素数的个数。

可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数,然后每次这样求一遍,但是发现有重 复的:举个例子 [1,10] 区间中与 6 互素的个数,应该是 10−(10/2+10/3)+(10/6)
然后利用二进制枚举子集个数,奇数加偶数减。
具体看代码:
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO                       \
    ios::sync_with_stdio(false); \
    cin.tie();                  \
    cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const ll INF = 0x3f3f3f3f;
;
 + ;
+;
];
+];
int prime[maxn],n;
;
void getprime()
 {
     memset(vis, false, sizeof(vis));
     ; i <= maxn; ++i)
     {
         if ( !vis[i] )  prime[++num] = i;
         ; j <= num && i * prime[j] <= n;  j++)
         {
             vis[ i  *  prime[j] ]  =  true;
             ) break;
         }
     }
 }
 ;
 void f(ll x) {
     cnt = ;
     ; prime[i]*prime[i] <=x && i <=num; i++) {
         ){
             fac[++cnt] = prime[i];
             ) x /= prime[i];
         }
     }
     ) fac[++cnt] = x;
 }
 ll solve(ll x) { //利用二进制处理子集,奇数加偶数减。
     ll ans = ;
     ; i < ((ll) << cnt); i++) {
         ,sum = ;
         ; j < cnt;j++){
             <<j))sum++, tmp *= fac[j+];
         }
         ) ans += x/tmp;
         else ans -= x/tmp;
     }
     return ans;
 }
 int main() {
     int T;
     ll ans = ,A,B,N;
     scanf("%d",&T);
     getprime();
     ; i <= T; i++){
         scanf("%lld%lld%lld",&A,&B,&N);
         f(N);
         ans = B - solve(B) - ((A - ) - solve(A-));
         printf("Case #%d: %lld\n",i,ans);
     }
     ;
 }

hdu 4135 Co-prime (素数打表+容斥原理)的更多相关文章

  1. hdu 4548 筛法求素数 打表

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...

  2. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  3. HDU 2136 素数打表+求质数因子

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. hdu 5104 素数打表水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...

  5. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  6. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  7. hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  9. POJ 1595 Prime Cuts (ZOJ 1312) 素数打表

    ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...

随机推荐

  1. java集合之深入分析ArrayList

    ArrayList特点: ArrayList方法实现: 扩容方法的实现: 源码: private void ensureCapacityInternal(int minCapacity) { //如果 ...

  2. nodejs+react构建仿知乎的小Demo

    一.命令行进入指定项目文件夹 二.相关命令安装环境和项目工具 npm init npm install react -- save npm install -g gulp npm install -- ...

  3. php下载大文件

    <?php $file = @ fopen($file_dir . $file_name,"r"); $filesize=filesize($file_dir.$file_n ...

  4. [Luogu 3973] TJOI2015 线性代数

    [Luogu 3973] TJOI2015 线性代数 这竟然是一道最小割模型. 据说是最大权闭合子图. 先把矩阵式子推出来. 然后,套路建模就好. #include <algorithm> ...

  5. 省队集训 Day4 a

    [题目大意] 求有多少区间只包含1个出现次数为1的数. $1\leq n \leq 5*10^5, 0 \leq a_i \leq 10^9$ [题解] 考虑枚举右端点,设这个数上一次出现位置为pre ...

  6. POJ3495 Bitwise XOR of Arithmetic Progression

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 772   Accepted: 175 Description Write ...

  7. 【LA】5135 Mining Your Own Business

    [算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...

  8. 基于 Docker 的 Zabbix 微服务系统

    zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...

  9. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  10. Python switch-case语句的实现 -- 字典模拟实现

    static void print_asru_status(int status, char *label) { char *msg = NULL; switch (status) { : msg = ...