codeforces 83 D. Numbers
题意:
给出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的更多相关文章
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 878 E. Numbers on the blackboard
Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- CodeForces 151B Phone Numbers
Phone Numbers Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 165E Compatible Numbers(二进制+逆序枚举)
E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- <转>人生与最速曲线
看完你好象发现了什么o(∩_∩)o 来源网络 来自为知笔记(Wiz)
- 利用 iframe解决ajax的跨域问题
问题 1. form提交或a标签跳转方式提交不会引发跨域问题. 2. ajax出于安全问题就有了跨域问题,因为一次请求中既访问了外部域最后返回了自己的域. 3. 用iframe其实就是想仿照ajax的 ...
- VLOOKUP
vlookup(查找目标,查找范围, 返回值的列数,精确或模糊查找) 1. 查找目标:查找的内容或者单元格引用. 2. 查找范围: 选定一个查找区域. 注意一: 查找目标一定要在该区域的第一列. 注意 ...
- Ubuntu vi 常用命令集合
:w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件,并退出viq: 不保存文件,退出vi:q! ...
- ORACLE执行详解
本文源自TTT BLOG,原文地址:http://blog.chinaunix.net/u3/107265/showart_2192657.html 简介: 本文全面详细介绍oracle执行计 ...
- Java笔记7-多态父类静态
多态的应用-面向父类编程 1.对象的编译时类型写成父类 2.方法的返回类型写成父类 3.方法的参数类型写成父类 编译时类型:对象的声明时类型,在于编译期间 运行时类型:new运算符后面的类型 编译时类 ...
- 深入理解kmp中的next数组
next数组 1. 如果对于值k,已有p0 p1, ..., pk-1 = pj-k pj-k+1, ..., pj-1,相当于next[j] = k. 此意味着什么呢?究其本质,next[j] = ...
- null和undefined区别(转)
目前,null和undefined基本是同义的,只有一些细微的差别. null表示"没有对象",即该处不应该有值.典型用法是: (1) 作为函数的参数,表示该函数的参数不是对象. ...
- CSS样式--实际开发总结
1. div 嵌套,子div中内容超出范围可以设置: display:inline-block; overflow:auto 即可让子div中出现滚轴 2. 让div中内容垂直方向居中 设置: ...
- Codeforces Round #170 (Div. 2)
A. Circle Line 考虑环上的最短距离. B. New Problem \(n\) 个串建后缀自动机. 找的时候bfs一下即可. C. Learning Languages 并查集维护可以沟 ...