LightOJ 1097 - Lucky Number [线段树二分前缀和构造数列]
来源:互联网 发布:java md5工具类运用 编辑:程序博客网 时间:2024/06/05 16:39
Lucky numbers are defined by a variation of the well-known sieve of Eratosthenes. Beginning with the natural numbers strike out all even ones, leaving the odd numbers 1, 3, 5, 7, 9, 11, 13, ... The second number is 3, next strike out every third number, leaving 1, 3, 7, 9, 13, ... The third number is 7, next strike out every seventh number and continue this process infinite number of times. The numbers surviving are called lucky numbers. The first few lucky numbers are:
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, ...
In this problem your task is to find the nth lucky number where n is given in input.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105).
Output
For each case, print the case number and the nth lucky number.
Sample Input
Output for Sample Input
2
2
100000
Case 1: 3
Case 2: 1429431
#include <bits/stdc++.h>using namespace std;const int MAX_N = 1429431+100;int sum[MAX_N<<2],T;void build(int idx,int l,int r){ if( l==r ){ if(l&1) sum[idx] = 1; else sum[idx] = 0; return; } int m = (l+r)>>1; build(idx<<1,l,m); build(idx<<1|1,m+1,r); sum[idx] = sum[idx<<1]+sum[idx<<1|1];}int query(int x,int idx,int l,int r){ if( l==r ) return l; int m = (l+r)>>1; if(sum[idx<<1]>=x) return query(x,idx<<1,l,m); else return query(x-sum[idx<<1],idx<<1|1,m+1,r);}int update(int pos,int x,int idx,int l,int r){ if( l==r ) { sum[idx] = x; return l; } int m = (l+r)>>1; int res = 0; if( sum[idx<<1]>=pos ) res = update(pos,x,idx<<1,l,m); else res = update(pos-sum[idx<<1],x,idx<<1|1,m+1,r); sum[idx] = sum[idx<<1] + sum[idx<<1|1]; return res;}int main(){ build(1,1,MAX_N); int now = 2; while( sum[1]>=now ){ int t = query(now,1,1,MAX_N); int nowt = t; bool flag = false; while(sum[1]>=nowt){ flag = true; int res = update(nowt,0,1,1,MAX_N); nowt+=t-1; } if(!flag) break; now++; } //printf("%d\n",sum[1]); scanf("%d",&T); for(int t=1;t<=T;t++){ int num; scanf("%d",&num); int ans = query(num,1,1,MAX_N); printf("Case %d: %d\n",t,ans); } return 0;}
- LightOJ 1097 - Lucky Number [线段树二分前缀和构造数列]
- LightOJ 1097 - Lucky Number(线段树)
- LightOJ 1087 - Diablo[线段树二分前缀和]
- Light OJ 1097 Lucky Number(线段树模拟)
- LightOJ-1097-线段树
- POJ2104 K-th Number 【线段树+二分】
- POJ2352_Stars_线段树求前缀和
- HDU6103Kirinriki(前缀和+二分)
- nyoj+二分前缀和
- lightoj1097 lucky number 报数类问题 线段树的单点更新
- lucky number
- Lucky Number
- Lucky Number
- lucky number
- Lucky Number
- Lucky Number
- LightOJ-2991-线段树
- LightOJ-1087-线段树
- Mac OS使用技巧之八:Dock栏使用技巧
- PushbackInputStream理解
- hdu1495——非常可乐
- HDU 1251-统计难题(map)
- 用什么软件做音乐
- LightOJ 1097 - Lucky Number [线段树二分前缀和构造数列]
- C#使用图片制作不规则窗体
- HTTP状态码详解
- 剑指Offer:面试题31 连续子数组的最大和
- <图形图像,动画,多媒体> 读书笔记 --- Quartz 2D 图形的基本变换
- 单片机加密初级
- viterbi算法
- PL/SQL如何查看过程的声明和定义
- jsp学习之路之Myeclipse部署tomcat服务器并实现Hello World一个小网页