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 ...
随机推荐
- 制定clone的用户名
git clone http://username:password@127.0.0.1/res/res.git指定用户名clone,有时需要切换clone 的用户名,不切换,会默认config us ...
- Linux下iptables安全配置
Linux下配置IPTables,只开放特定端口,禁用其他网络. *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] ...
- java RSA加密解密实现(含分段加密)
该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1.jar 下载地址:http://download.csdn.net/detail/centralperk/50255 ...
- 在windows下如何使用密钥对远程登录服务器?
在企业的生产中相信各位朋友都会使用远程登录服务器,这样即高效也非常方便,(服务器在西藏,没有远程技术,公司在北京,你只能到西藏与机器相伴,在这里我使用xshell软件),我们使用ssh 服务登录服务器 ...
- PHP扩展--Yaf框架安装
安装/配置 编译安装 wge thttp://pecl.php.net/get/yaf-2.3.5.tgz tar -zxvfyaf-2.3.5.tgz cd yaf-2.3.5/ cd extens ...
- 【NOIP】提高组2016 蚯蚓
[题目链接]Universal Online Judge [题解]本题最大的特点在于从大到小切以及切分规则一致,都是切成px和x-px. 由这两个特点很容易得到结论,后切的蚯蚓得到的px一定比先切的蚯 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- UIScrollView---iOS-Apple苹果官方文档翻译
本系列所有文章,链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版) //转载请注明出处--本文永久链接:http://www ...
- MSSQL DBOtherSQL
--------------------------查询表中的数据------------------------------ --1.请查询出MyStudent表中的所有数据 --下面的语句表示查询 ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...