题意:

给出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. Java(六)——抽奖系统

     总体思路: 将编号加入ArrayList动态数组中,利用集合的静态方法Collections.shuffle() 乱序集合中的元素从而获得随机数,remove删除已抽编号  代码如下: import ...

  2. map 容器的使用

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map> ...

  3. PPPoE(以太网上的点对点协议)

    协议概述 PPPoE分为两个阶段,即Discovery(地址发现)阶段和PPP会话阶段.当某个主机希望发起一个PPPoE会话时,它必须首先执行Discovery来确定对方的以太网MAC地址并建立起一个 ...

  4. 课堂笔记-background属性

    一,background-position:(图片定位)   三种写法:   1):按%比,左上角最小(0%,0%),右下角最大(100%,%100):   2):(x,y)左上角最小(0,0),右下 ...

  5. 用php脚本给html中引用的js和css路径打上版本

    比如 <link rel="stylesheet" type="text/css" href="./css/globel.css"&g ...

  6. Hbase的flush机制

    Hbase Flush机制最小Flush单元为HRegion,尽量减少CF数量以减少HStrore数量从而减少MemStore的数量,最终减少每次Flush的开销.1.Region级别触发条件:   ...

  7. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  8. MongoDB学习笔记五:聚合

    『count』count是最简单的聚合工具,返回集合中的文档数量:> db.foo.count()0> db.foo.insert({"x" : 1})> db. ...

  9. grunt压缩js文件

    grunt是node中很好的管理项目的工具,利用它可以实现对整个项目的管理,避免很多重复性的工作如合并.压缩,检查语法等. 使用grunt首先要安装node环境,nodejs官网http://node ...

  10. Quant的笑话

    Q) Why was the FX quant so unlucky with the ladies?A) Because he always kept his dates short. Q) Why ...