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 ...
随机推荐
- 【Mocha.js 101】同步、异步与 Promise
前情提要 在上一篇文章<[Mocha.js 101]Mocha 入门指南>中,我们提到了如何用 Mocha.js 进行前端自动化测试,并做了几个简单的例子来体验 Mocha.js 给我们带 ...
- 黑马程序员:Java编程_IO流
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设 ...
- oracle数据库卸载(需要完全卸载oracl才能重装)
1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. 2. 开始->程序->Oracle - OraHome81->Oracle I ...
- C++学习笔记32:泛型编程拓展1
标准模板库的内容 标准模板类:复数.序偶 迭代器 标准容器:向量,表,栈,队列,集合,映射等 标准算法:查找,排序等 标准模板库型式的使用方法 "<>":模板名称< ...
- js获取url的常用方法
//设置或获取对象指定的文件名或路径. console.log(window.location.pathname) //设置或获取整个 URL 为字符串. console.log(window.loc ...
- CSS counter-reset 属性
一.实例 对部分和子部分进行编号(比如 "Section 1"."1.1"."1.2")的方法: eg: css部分: ol { list- ...
- e
frame问题 以下面的frame为例: ? 1 <frame src=”xxx.html” id=”frameId” name=”frameName” /> (1)访问frame ...
- git 版本控制
用gitbash进入类似命令行的窗口 用命令 cd e:/learngit 进入该目录,然后在此目录下初始化$ git init, 于是该文件夹就成为了一个工作区,里面的.git文件就是版本库(rep ...
- code标签和pre标签
code标签: 1.code标签的定义: <code>标签, 用于表示计算机源代码或者其他机器可以阅读的文本内容.软件代码的编写者习惯了编写代码时的代码格式,那么这个<code> ...
- gruntjs
先输入命令: npm install -g grunt-clinpm install grunt --save-devgrunt –version 新建json文件:package.json { &q ...