UVA11876 N + NOD (N)【欧拉筛法+前缀和】
Consider an integer sequence N where,
N0 = 1
Ni = Ni−1 + NOD(Ni−1) for i > 0
Here, NOD(x) = number of divisors of x.
So the first few terms of this sequence are 1 2 4 7 9 12 18
…
Given two integers A and B, find out the number of integers in the above sequence that lies within the range [A, B].
Input
The first line of input is an integer T (T < 100000), that indicates the number of test cases. Each case contains two integers, A followed by B (1 ≤ A ≤ B ≤ 1000000).
Output
For each case, output the case number first followed by the required result.
Sample Input
3
1 18
1 100
3000 4000
Sample Output
Case 1: 7
Case 2: 20
Case 3: 87
问题链接:UVA11876 N + NOD (N)
问题简述:给定公式Ni = Ni-1 + NOD(Ni-1),NOD为因子个数,求一个区间[A,B]上有多少个N。
问题分析:用欧拉筛法得到素数备用,打表计算函数的前缀和,然后查表计算。
程序说明:数组sum[]是前缀和。
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA11876 N + NOD (N) */#include <bits/stdc++.h>using namespace std;const int N = 1000000;
bool isprime[N + 1];
int prime[N / 3], pcnt = 0;
// 欧拉筛法
void eulersieve(void)
{memset(isprime, true, sizeof(isprime));isprime[0] = isprime[1] = false;for(int i = 2; i <= N; i++) {if(isprime[i])prime[pcnt++] = i;for(int j = 0; j < pcnt && i * prime[j] <= N; j++) { //筛选isprime[i * prime[j]] = false;if(i % prime[j] == 0) break;}}}int ni[N + 1], sum[N + 1];int nod(int n)
{map<int, int> m;for(int i = 0; i < pcnt; i++) {if(n < prime[i]) break;else if(n % prime[i] == 0) {int cnt = 0;while(n % prime[i] == 0)cnt++, n /= prime[i];m[prime[i]] = cnt;}}if(n != 1) m[n] = 1;int ret = 1;for(map<int, int>::iterator iter = m.begin(); iter != m.end(); iter++)ret *= iter->second + 1;return ret;
}void maketab()
{int ncnt = 0;ni[ncnt++] = 1;sum[0] = 0;sum[1] = ni[1];for(;;) {int n = ni[ncnt - 1];int ni2 = n + nod(n);if(ni2 > N) {fill(&sum[n], &sum[N + 1], ncnt);break;} else {fill(&sum[n], &sum[ni2], ncnt);ni[ncnt++] = ni2;}}
}int main()
{eulersieve();maketab();int t, a, b;scanf("%d", &t);for(int k = 1; k <= t; k++) {scanf("%d%d", &a, &b);printf("Case %d: %d\n", k, sum[b] - sum[a - 1]);}return 0;
}
UVA11876 N + NOD (N)【欧拉筛法+前缀和】相关推荐
- UVA12043 Divisors【欧拉筛法】
Let us define the functions d(n) and σ(n) as d(n)=numberofdivisorsofnd(n) = number of divisors of nd ...
- 质数c语言欧拉筛选,Python|欧拉筛法求质数
欢迎点击「算法与编程之美」↑关注我们! 本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章. 问题描述 我们知道第一个质数是 2.第二个质数是 3.第三个质 ...
- 素数计算之埃氏筛法、欧拉筛法
埃氏筛法 int main() {const int maxNumber=200;int isPrime[maxNumber];int i;int x;for (i=0;i<maxNumber; ...
- UVA516 POJ1365 LA5533 ZOJ1261 Prime Land【欧拉筛法】
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- UVA10738 Riemann vs Mertens【欧拉筛法】
One of the biggest, most mathematicians would call it THE biggest, unsolved problems in mathematics ...
- UVA12039 Goldbach‘s Cardinality【欧拉筛法】
Goldbach's cardinality (GC) of an even number means the number of ways the even number can be expres ...
- 欧拉筛法(线性筛)的学习理解
前言 在刚接触编程语言时,对于寻找素数,第一时间想到的便是二重循环暴力查找,其复杂度O(n^2),通过循环中只判断到根号n可以优化一些,不过复杂度也达不到预期.在数论的学习中,我学到了埃氏筛法,O(n ...
- 筛质数—(朴素筛法、埃氏筛法、欧拉筛法(线性筛法))
筛质数时首先要了解质数的定理:1~n中有 个质数 下面再来看具体算法: 1.朴素筛法: 直接把2~n-1中质数和合数的倍数都筛一遍,其代码如下所示: int primes[N],cnt=0; boo ...
- 素数筛法(传统普通、朴素筛法、埃式筛法、欧拉筛法(线性筛))
素数筛法(普通.朴素筛法.埃式筛法.欧拉筛法) 1.题目 2.分析 3.代码 传统普通 朴素筛法 朴素筛法(6.14) 埃式筛法 埃式筛法(6.14) 欧拉筛法(线性筛) 欧拉筛法(线性筛 6.14) ...
最新文章
- 企业网络推广期间面对网站外链优化企业网络推广专员有话说
- 关于一个tomcat里面放2个项目启动报listener错误
- VL09 不能取消来自分散系统的货物移动
- 洛谷 P1656 炸铁路
- 看美文,记单词(6)
- 【Android笔记】Unable to execute dex: Multiple dex files define 解决方法
- Java native方法String转char*以及String[]转char**
- CC(Context Capture)软件安装及空三过程中的十大常见报错与解决方法
- 图书馆借阅系统java参考文献_基于java的图书馆管理系统
- Android中复杂日历控件,CalenderView-一个优雅、高度自定义、性能高效的Android日历控件...
- 树莓派 linux安装中文语言包6,Linux下给树莓派安装及配置系统-Go语言中文社区
- Coggle打卡——Linux使用基础
- 图表嵌入到数据表格下方_在excel图表下方添加数据表 excel图表添加数据表
- 计算机网络传输层课件,计算机网络技术,传输层协议课件
- 【sj借鉴】NP-hard
- LINUX杂谈与系统编程
- 2019牛客多校训练营第一场 E题 ABBA 题解
- 牛客网 NC204859 组队 滑动窗口
- 安mysql一直转圈_在访问数据库时没报错一直转圈,mysql版本不同的连接方式
- (转)Moodle平台简介
热门文章
- 使用GDAL将下载的Google卫星图像转为带坐标的tif
- ssm注解配置连接mysql_基于注解和配置类的SSM(Spring+SpringMVC+Mybatis)项目详细配置...
- java 监听端口_java游戏服务器检查报告(经验分享)
- 怎么删除flash弹出的广告_电脑桌面老是弹出广告怎么办?一个无需下载软件即可屏蔽的方法...
- java创建数据库mysql数据库_用Java创建MySQL数据库
- 认识HTML与CSS
- Spark利用(idea+maven+scala)创建wordcount打包jar并在spark on yarn上运行——Spark的开发
- 计算机tlv简介_TLV编码格式详解
- Linux下mysql主从复制配置(CentOS7)
- 文本编辑器查看 cprintf颜色_做生信,你需要一款好用的文本编辑器