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 ...
随机推荐
- sql生成一个日期表
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...
- HDU 2154 跳舞毯 | DP | 递推 | 规律
Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...
- JavaScript之typedof,instanceof,Array.isArray()
typedof value检测对象类型. value instanceof Array检测a对象是否由b对象类型 Array.isArray(value)在两个框架中判断是否为数组类型.
- \(\rm LightOJ 1371 - Energetic Pandas 简单计数+组合\)
http://www.lightoj.com/volume_showproblem.php?problem=1371 题意:给你n根竹子,和n只熊猫(XD),每个熊猫只能选择重量不大于它的竹子,问有几 ...
- [洛谷P2365] 任务安排
洛谷题目链接:任务安排 题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时 ...
- C11简洁之道:循环的改善
1. for循环的新用法 在C++98/03中,通过for循环对一个容器进行遍历,一般有两种方法,常规的for循环,或者使用<algorithm>中的for_each方法. for循环遍 ...
- Linux目录结构nginx
alias 别名( 永久 ) 1.vim /root/.bashrc 2. alias vimens33='vim /etc/sysconfig/network-scripts/ifcfg-ens33 ...
- 解决win10 CPU占用高的问题
[PConline 技巧]很多笔记本用户在升级到Win10后,都遇到了这样一个问题,那就是Win10的CPU占用明显高于Win7.这个问题对于台式机可能还算不了什么,顶多就是偶尔卡一下罢了.可由于笔记 ...
- Tomcat 7下如何利用 catalina.properties 部署公用类
Tomcat 有很多配置文件,其中一个是 catalina.properties ,本文介绍catalina.properties 中的设置项. 一.组成 catalina.properties ...
- 【BZOJ4236】JOIOJI [DP]
JOIOJI Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description JOIOJI桑是JOI君的叔叔 ...