hdu 4135 Co-prime (素数打表+容斥原理)
题目链接
题意:问从A到B中与N互素的个数。
题解:
利用容斥原理:先求出与n互为素数的个数。
#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 (素数打表+容斥原理)的更多相关文章
- hdu 4548 筛法求素数 打表
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- HDU 2136 素数打表+求质数因子
Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
- HDOJ(HDU) 2136 Largest prime factor(素数筛选)
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- POJ 1595 Prime Cuts (ZOJ 1312) 素数打表
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...
随机推荐
- Leetcode 445. 两数相加 II
1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...
- STM32之窗口看门狗
1.有个7位递减计数器(WWDG->CR),就这个计数器和窗口计数器(WWDG->CFR)决定什么时候喂狗.狗喂早了,复位——“早”体现在 计数器值(tr)>窗口值(wr),也就是计 ...
- Jmeter-6-创建数据库测试计划
1. 将mysql 的jdbc的jar包放到Jmeter lib的目录下. 2. 创建线程组. 3. 创建JDBC Connection Configuration, 提供详细的数据库配置信息. 4. ...
- 使用.net core abp framework
abp是一个有用的框架,包含许多功能,可以用来作为脚手架. 直接在官方网站上输入相应的工程名称,选择对应的版本就会下载对应的版本..net core 版本的可以使用后端框架部分来做api,包含了常用框 ...
- js_判断当前页面是否有网络和网络连接超时
2018-04-12 方法一:通过navigator.onLine属性判断,返回true为有联网状态,false为断网状态. //方法一 if(navigator.onLine) { console. ...
- jeecg3.7中弹出窗操作标签dgOpenOpt的用法
1.基本参数 参数名 描述 url 弹出页面地址 title ...
- 集合框架源码学习之HashMap(JDK1.8)
目录: 0-1. 简介 0-2. 内部结构分析 0-2-1. JDK18之前 0-2-2. JDK18之后 0-3. LinkedList源码分析 0-3-1. 构造方法 0-3-2. put方法 0 ...
- 另类dedecms后台拿shell
遇到一个被阉割的后台,发现直接传shell显然不行. 然后就有了下文 添加一个新广告. 插入一句话木马: --><?php $_GET[c]($_POST[x]);?><!-- ...
- 【Matlab】让Matlab程序发出声音
我有时候运行一段很长的代码,在等待的时候去做别的事,希望程序运行完可以有一个提示音. 这可以用matlab的一个函数sound实现,该函数的输入参量是音频数据向量.采样频率和转换位数. % 响一声 s ...
- 【设计模式】原型模式(Prototype)
摘要: 1.本文将详细介绍原型模式的原理和实际代码中特别是Android系统代码中的应用. 纲要: 1. 引入原型模式 2. 原型模式的概念及优缺点介绍 3. 原型模式对拷贝的使用 4. 原型模式在A ...