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 ...
随机推荐
- 29 个你必须知道的 Linux 命令
虽然Linux发行版支持各种各样的饿GUI(graphical user interfaces),但在某些情况下,Linux的命令行接口(bash)仍然是简单快速的.Bash和 Linux Shell ...
- REDIS fdatasync技术问题和BIO技术的引入
http://oldblog.antirez.com/post/fsync-different-thread-useless.html 这是原文作者的博客 把他翻译下 带上自己的一些理解 看看作者引入 ...
- 2016年5月面试题(Unity&iOS)
NGUI的DrawCall drawcall定义:对底层图形程序(比如:OpenGL ES)接口的调用,以在屏幕上画出东西. List在内存中连续的空间保存 List是线性直接存储类型 mipMap是 ...
- 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...
- js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版【转】
之前在修改IE6下的一个显示bug时,查到过这些,贴这备忘,后面给出了详细说明,以前的版本,没仔细的说明,特希望大家也收藏下. 网页可见区域宽: document.body.clientWidth ...
- 个人制作-css+html旋转立方体的制作
源代码: <!DOCTYPE html><html><head> <title></title> <meta charse ...
- Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 较老版本的AFNetworking使用心得
较老版本的 AFNetworking 下载链接 ( http://pan.baidu.com/s/14Cxga ) 将压缩包中的文件夹拖入xcode工程项目中并引入如下的框架 简单的 JOSN 解析例 ...
- 获取ORACLE数据库的构建信息
首先连接到数据库,获取ORACLE数据库的基本信息: C:\USERS\ADMINISTRATOR>SQLPLUS/NOLOG SQL*PLUS: RELEASE 10.2.0.3.0 - PR ...
- 模块:js实现一个倒计时功能
1.给显示内容加样式 <style> #p1{font-size: large; color: red;} </style> 2.客户端页面 <div id=" ...