题意:

给出l,r,k,(1 ≤ l ≤ r ≤ 2·109, 2 ≤ k ≤ 2·109)

求在区间[l,r]内有多少个数i满足 k | i,且[2,k-1]的所有数都不可以被i整除

首先,如果k不是素数的话,答案肯定是0

考虑k是素数:

fir[i]保存i的第一个素因子,fir[]可以在线性筛的时候得到

我们先把N以内的数线性筛出来

所以其实就是求:

[l,r]中满足fir[i] = k 的i的个数

[l,r] = [1,r] - [1,l-1]

所以现在我们要求的就是:

[1,r]中满足fir[i] = k 的i的个数,也就是

[1,r/k]中满足fir[i] >= k 或 i = 1的i的个数

n = r / k

如果n <N,直接遍历fir,统计fir[i] >= k || i == 1 的i的个数

如果n >= N,就相当于要把[1,n]中2的倍数,3的倍数,等小于k的素数的倍数筛去

dfs搜,容斥,考虑到2 * 3 * ... * 23 * 29 > 2 * 10^9,所以复杂度不会很大

这个套路也很喜闻乐见

代码:

  //File Name: cf83D.cpp
//Created Time: 2017年01月04日 星期三 22时48分56秒 #include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = + ;
bool check[MAXN];
int prime[],fir[MAXN],tot;
LL ans,n;
int ma;
void init(){
tot = ;
memset(check,false,sizeof(check));
for(int i=;i<MAXN;++i){
if(!check[i]){
prime[tot++] = i;
fir[i] = i;
}
for(int j=;j<tot;++j){
if((LL)i * prime[j] >= MAXN) break;
check[i * prime[j]] = true;
fir[i * prime[j]] = prime[j];
if(i % prime[j] == ) break;
}
}
// printf("tot = %d\n",tot);
}
bool is_prime(LL k){
if(k < MAXN)
return fir[k] == k;
for(int i=;i<tot;++i){
if(1LL * prime[i] * prime[i] > k) break;
if(k % prime[i] == ) return false;
}
return true;
}
void dfs(int p,LL now,LL f){
if(now > n) return ;
if(p > ma){
ans += f * (n / now);
return ;
}
dfs(p+,now,f);
dfs(p+,now * prime[p],-f);
}
LL cal(LL r,LL k){
ans = ;
n = r / k;
if(n < MAXN){
for(int i=;i<=n;++i){
if(i == || fir[i] >= k)
++ans;
}
return ans;
}
else{
ma = ;
for(;ma<tot;++ma){
if(prime[ma] == k)
break;
}
--ma;
dfs(,,);
return ans;
}
}
LL solve(LL l,LL r,LL k){
if(!is_prime(k)) return ;
return cal(r,k) - cal(l - ,k);
}
int main(){
init();
// while(cin >> n){
// cout << fir[n] << endl;
// }
LL l,r,k;
cin >> l >> r >> k;
cout << solve(l,r,k) << endl;
return ;
}

codeforces 83 D. Numbers的更多相关文章

  1. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  2. Codeforces 878 E. Numbers on the blackboard

    Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...

  3. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  4. CodeForces 151B Phone Numbers

     Phone Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  5. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 165E Compatible Numbers(二进制+逆序枚举)

    E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  7. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  8. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  9. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. CSS各种定位详解

    1.定位的专业解释 (1)语法 position:static|absolute|fixed|relative (2)说明 从上面语法可以看出,定位的方法有很多种,它们分别是静态(static),绝对 ...

  2. Javascript 事件对象(六)事件默认行为

    事件默认行为: 阻止默认事件普通写法:return false;屏蔽右键菜单 :  oncontextmenu <!DOCTYPE HTML> <html> <head& ...

  3. Objective-C汇总

    Objective  C(20世纪80年代初) 一.OC语言概述 .1985年,Steve  Jobs成立了NeXT公司 .1996年,12月20日,苹果公司宣布收购了NeXT  softwar ...

  4. nginx 基本操作

    nginx 是什么 nginx 是轻量.高性能的网页服务器,相较 Apache 占有内存小. 下载 https://nginx.org/en/download.html 默认根目录 安装目录下的 ht ...

  5. mysqldump-info

    其实很多东西都能在info里面找到非常详细的说明,只是,我们太忙了,只想要一个答案,而无心去看而已,所以呢,就把用得到的都看一下来记录吧. 命令模式:mysqldump [options] [db_n ...

  6. thinkphp3.2.3在框架截取文字

    Common/Common/function.php加入以下代码 /** * * 字符截取 * @param string $string * @param int $start * @param i ...

  7. 计算机网络自学之路-----IP协议(3)

    前面一期说到了IP层的IP协议跟ARP协议,因为IPV4协议自身有些不足的地方,为了弥补这些不足,又引入了一些别的协议.觉得这种弥补方式治标不治本~~ 1)ICMP网络控制报文协议 2)CIDR无类域 ...

  8. install vim

    常用命令: [0]安装vim: oee@copener:~$ sudo apt-get install vim vim-scripts vim-doc 刚安装完$HOME目录下只有两个文件:.vim/ ...

  9. HTTP Code

    所有 HTTP 状态代码及其定义. 代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理,但处理尚未完成.  20 ...

  10. MIT JOS学习笔记03:kernel 02(2016.11.08)

    未经许可谢绝以任何形式对本文内容进行转载! 本篇接着上一篇对kernel的分析. (5)pte_t * pgdir_walk(pde_t *pgdir, const void *va, int cre ...