Light OJ 1097 Lucky Number(线段树模拟)
来源:互联网 发布:欧树蜂蜜洁面凝胶 知乎 编辑:程序博客网 时间:2024/05/16 16:13
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<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1500000;int sum[maxn<<2];int n;void pushup(int rs){ sum[rs]=sum[rs<<1]+sum[rs<<1|1];}void build(int rs,int l,int r){ sum[rs]=0; if(l==r) { sum[rs]=(l&1); return ; } int mid=(l+r)>>1; build(rs<<1,l,mid); build(rs<<1|1,mid+1,r); pushup(rs);}void update(int x,int l,int r,int rs){ if(l==r) { sum[rs]=0; return ; } int mid=(l+r)>>1; if(x<=sum[rs<<1]) update(x,l,mid,rs<<1); else update(x-sum[rs<<1],mid+1,r,rs<<1|1); pushup(rs);}int query(int x,int l,int r,int rs){ if(l==r) return l; int mid=(l+r)>>1; if(x<=sum[rs<<1]) query(x,l,mid,rs<<1); else query(x-sum[rs<<1],mid+1,r,rs<<1|1);}void init(){ build(1,1,1429431); int pos=2; while(sum[1]>=pos) { int val=query(pos,1,1429431,1); int lit=sum[1]; int temp=val; while(temp<=lit) { update(temp,1,1429431,1); temp+=val-1; } pos++; }}int main(){ init(); int t,n,cas=1; scanf("%d",&t); while(t--) { scanf("%d",&n); printf("Case %d: %d\n",cas++,query(n,1,1429431,1)); }}
- Light OJ 1097 Lucky Number(线段树模拟)
- LightOJ 1097 - Lucky Number(线段树)
- 【HDU4937】Lucky Number(模拟)
- Light OJ 1084 Winter (线段树+DP)
- Light OJ 1360 Skyscraper (线段树+DP)
- LightOJ 1097 - Lucky Number [线段树二分前缀和构造数列]
- codefroces 110A Nearly Lucky Number(模拟水题)
- Light oj 1234 - Harmonic Number
- Light OJ 1234 Harmonic Number
- light oj 1234 Harmonic Number
- Light-oj 1078 (除法模拟)
- CF121E Lucky Array(线段树+暴力)
- Light OJ 1084 线段树+dp or(单调队列+dp) 水题
- Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)
- light oj 1080 线段树和树状数组
- Light-oj 1080 - Binary Simulation(线段树区间更新)
- 九度oj 1380 lucky number + 二进制
- Light oj 1245 Harmonic Number (II)(基础数论)
- 15/8/17 日学习记录1
- JS的常用正则表达式 验证密码
- HDOJ 1874 畅通工程续(最短路--dijkstra)
- 2015/8/17/安卓环境的搭建/adb is down问题
- hdu 1087 Super Jumping! Jumping! Jumping!
- Light OJ 1097 Lucky Number(线段树模拟)
- 20150807 表单美化
- hdu 5381 The sum of gcd 原来有个算法叫莫队 2015 Multi-University Training Contest 8
- 20150808 CSS3 3D特效
- AppDelegate.h文件
- 9
- 第4章 JSP技术概述
- HDOJ 1596 find the safest road(最短路--dijkstra变形)
- 20150809 CSS圆角进化论