lightoj 1097

1097 - Lucky Number

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<iostream>
#include<cstdio>
using namespace std;
const int MAXN=1500000;
struct node
{
    int l,r,sum;
}tree[MAXN*3];
void pushup(int o)
{
    tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum;
}
void build(int l,int r,int o)
{
    tree[o].l=l,tree[o].r=r;
    if(l==r)
    {
        tree[o].sum=l&1;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,o<<1);
    build(mid+1,r,o<<1|1);
    pushup(o);
}
int query(int pos,int o)
{
    if(tree[o].l==tree[o].r)return tree[o].l;
    if(pos<=tree[o<<1].sum)return query(pos,o<<1);
    else return query(pos-tree[o<<1].sum,o<<1|1);
}
void update(int pos,int o)
{
    if(tree[o].l==tree[o].r){tree[o].sum=0;return;}
    if(pos<=tree[o<<1].sum)update(pos,o<<1);
    else update(pos-tree[o<<1].sum,o<<1|1);
    pushup(o);
}
void table()
{
    int cnt=0;
    build(1,1429431,1);
    for(int i=2;i<=100000;i++)
    {
        int v=query(i,1);
        for(int j=v;v+j<=1429431;j+=v-1)
            update(j,1);
    }
}
int main()
{
    table();
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++)
    {
        int n;
        scanf("%d",&n);
        printf("Case %d: %d\n",kase,query(n,1));
    }
    return 0;
}
时间: 2024-08-03 04:08:36

lightoj 1097的相关文章

LightOJ 1097 - Lucky Number 线段树

http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3的倍数的元素,再是7……重复操作,最后问第n项的值 思路:使用线段树构造序列,对一个数进行标记是否已被删去,和为元素个数.由于样例给出了大小,所以很容易控制空间. /** @Date : 2016-12-05-19.34 * @Author : Lweleth ([email protected])

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

1097:零起点学算法04——再模仿一个算术题

1097: 零起点学算法04--再模仿一个算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 2627  Accepted: 2202[Submit][Status][Web Board] Description 上题会模仿了吧.再来模仿一个. 现在要求你模仿一个乘法的算术题 Input 没有输入 Output 输出9乘以10的值 Sample Output 90 Source 零起点学算法

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include

Lightoj 1090 - Trailing Zeroes (II)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0? 解题思路: 是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕? 因为你是Acmer,这都不应该是问题.因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数.然后算出中分别含有2,5的个数,

暑期训练狂刷系列——Lightoj 1084 - Winter bfs

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k,问最终能不能把所有点都聚集在大于等于三个点的集合里面,如果能最少需要几个这样的集合? 解题思路: 刚开始看到题目感觉好简单,就开始了sort然后贪心之旅,这就是错误的开始.最后发现这样并不行,然后再Alex的提醒下想用单调队列优化,在我愚昧的理解下竟然写成了bfs,提交竟然ac了,surprise~

[swustoj 1097] 2014

2014(1097) 问题描述 今年是2014年,所以小明喜欢2014的每一位数字(即:2,0,1,4),小明想知道在区间[l,r](包括l和r)中有多少个数中含有这4个数字(数字无前缀零). 输入 多组数据. 每组数据输入2个数l,r(0<l<r<=10^9) 输出 输出占一行,即区间[l,r](包括l和r)中包含的满足条件的数的个数 样例输入 1 10100 1024 样例输出 0 1 简单数位DP #include <iostream> #include <cst