UVA10909 Lucky Number题解
原文链接:http://www.algorithmist.com/index.php/User:Sweepline/UVa_10909.cpp
AC的C++语言程序:
/** Solution for problem 10909 'Lucky Number'.** The code uses a (simple) binary search tree to build the list of* lucky numbers.*/
#include <cstdio>
#include <cstring>#define MAXN 670000 /* max. number of nodes in the tree *//* Tree's housekeeping...*/
int left[MAXN], right[MAXN], parent[MAXN], key[MAXN], count[MAXN], N, root;char lucky[2010000];/* Returns the index of the k-th smallest element in the tree. */
int find(int k)
{for (int x = root;;)if (k < count[left[x]])x = left[x];else if (k == count[left[x]])return x;elsek -= count[left[x]] + 1, x = right[x];
}/** Removes the element with index 'x' from the tree.* Implemented algorithm ensures that the height of the tree does not increase.*/
void rm(int x)
{int y;if (left[x] != 0 && right[x] != 0) {if (count[right[x]] >= count[left[x]])for (y = right[x]; left[y] != 0; y = left[y]);elsefor (y = left[x]; right[y] != 0; y = right[y]);key[x] = key[y];x = y;}if (left[x] == 0 && right[x] == 0) {if (left[parent[x]] == x)left[parent[x]] = 0;elseright[parent[x]] = 0;} else {y = (left[x] == 0) ? right[x] : left[x];if (parent[x] == 0) {parent[root = y] = 0;return;}if (left[parent[x]] == x)left[parent[x]] = y;elseright[parent[x]] = y;parent[y] = parent[x];}for (x = parent[x]; x != 0; x = parent[x])count[x]--;
}/* Constructs a balanced tree with b-a+1 elements; returns index of its root.The tree's nodes will get consecutive indices in their order in the tree. */
int build(int a, int b)
{if (a > b) return 0;if (a == b) { N++; left[N] = right[N] = 0; count[N] = 1; return N; }int c=(a+b)/2, t = build(a, c-1);left[++N] = t; t = N; right[t] = build(c+1, b);count[t] = count[left[t]] + count[right[t]] + 1;parent[left[t]] = parent[right[t]] = t;return t;
}void mark(int x)
{for (; x; x = right[x])lucky[key[x]] = 1, mark(left[x]);
}/* Constructs the list of lucky numbers */
void make()
{int i, j, k;/* First off, initialize the tree... */N = count[0] = 0;parent[root = build(0, 666667)] = 0;/** As an optimization, we start with the tree, containing all numbers* of form 6k+1 and 6k+3 in the range of interest.* These are the numbers, which remain after the first two elimination* rounds.*/for (i = 1, j = 1; i <= 666700; j += 6)key[i++] = j, key[i++] = j+2;/* Now just simulation... */for (k = 2; k < count[root]; k++) {j = key[find(k)]-1;if (j >= count[root]) break;for (i = j; i < count[root]; i += j)rm(find(i));}/* Finally, mark the remaining numbers in the boolean array lucky[] */memset(lucky, 0, sizeof(lucky));mark(root);
}int main()
{int a, n;for (make(); scanf("%d", &n) == 1;) {a = 0;if (n >= 1 && (n & 1) == 0) {for (a = n/2; a > 0 && !lucky[a]; a--);for (; a > 0; a -= 2)if (lucky[a] && lucky[n-a]) break;}if (a <= 0)printf("%d is not the sum of two luckies!\n", n);elseprintf("%d is the sum of %d and %d.\n", n, a, n-a);}return 0;
}
UVA10909 Lucky Number题解相关推荐
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) T ...
- hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场
Lucky Number Time Limit: 20 ...
- lucky number
4,7是lucky number ,当一个数出现1个以上非lucky number就是我们所不需要的,问a,b间有多少个,由于数据范围只有1-1000000 int findLuckyNumber(i ...
- 九度oj 题目1380:lucky number
题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ...
- Nearly Lucky Number
文章目录 一.Nearly Lucky Number 总结 一.Nearly Lucky Number 本题链接:Nearly Lucky Number 题目: A. Nearly Lucky Num ...
- B. Nezzar and Lucky Number
Codeforces Round #698 (Div. 2) B B. Nezzar and Lucky Number Nezzar's favorite digit among 1,-,9 is d ...
- C#,幸运数字(Lucky Number)的算法与源代码
Lucky Number不是蔡依林的歌曲名字哦. 给你的NV朋友选一个幸运数字吧. 一.幸运数字是怎么产生的? 幸运数是整数的子集.与其进行大量理论研究,不如让我们来看看得出幸运数字的过程, 以整数集 ...
- 110A.Nearly Lucky Number
110A.Nearly Lucky Number 题目 翻译 题目 输入 输出 分析 代码 题目 翻译 题目 Petya喜欢幸运数.众所周知只包含幸运数字4和7的十进制正数称为幸运数.比如说,47,7 ...
- Lucky Number(HDU-4937)
Problem Description "Ladies and Gentlemen, It's show time! " "A thief is a creative a ...
最新文章
- vim下自动排版命令
- java audiorecord_Android 录音实现(AudioRecord)
- 为了更好——关于博客搬迁的说明
- 熊猫分发_熊猫新手:第二部分
- 引擎设计跟踪(九.6) 地形最近更新
- mysql 机器复制_MySQL复制在同一台机器上
- 华为背锅?微博大V质疑华为P30 Pro拍月亮造假 公司称误导观众已开除
- python 一些练习 (初学)
- Java Json字符串或Json对象属性查找工具类
- 图书管理系统/库存管理系统等计算机毕业论文设计
- java身份证号码验证
- 饮料自动售货机C++
- Ajax配合jQuery和数据库
- 记录WIN10选择文件右键后资源管理器无响应的解决方法
- Private,Public,Protected
- 利用正则表达式判断邮箱
- 【vconsole】vconsole网页调试
- Windows系统下批量重命名文件(超详细操作讲解)
- return的作用,返回函数值和结束程序执行
- 调音台使用基础-增益结构与推子位置
热门文章
- 容器大小_C++复习篇(7)序列式容器vector
- native8081端口 react_教你轻松修改React Native的端口(如何同时运行多个React Native、8081端口占用问题)...
- Arcgis Javascript那些事儿(五)--arcgis server发布自定义比例尺地图
- 巅峰对决 Spring Boot VS .NET 6
- 毁掉孩子自信的10个杀手
- ASP.NET的内置对象介绍
- java中的jni_JAVA中JNI的简单使用
- Hadoop之mapReduce有几种排序及排序发生的阶段
- linux 信号集 同步,linux信号集
- Spark Shuffle详解剖析