Time Limit: 2 second(s) | Memory Limit: 64 MB |
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;
}