2354: Number Pyramid 线性素数筛选变形

本文介绍了一种基于整数性质构建特殊金字塔结构的方法,并提供了一个算法实现案例,该算法能够根据输入的数值快速定位其在金字塔中的位置。

2354: Number Pyramid


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
5s131072K25059Standard

Given a positive integer n, a number pyramid is build as follow:

  • 1 is top
  • if (x>y, x mod y==0), x is somewhere under y.
  • each number is on its most possible high level.
  • if x and y are same level, small is on left.

For example, when n is 6, the pyramid looks like here:

1
2 3 5
4 6

For a value x, you should given the position in the pyramid. The position is counted from top to bottom, form left to right, the top position is 1.

Input

For each test case, one positive integer n (1 <= n <= 1000000) is given at the first line. The next line are a series of integer x ( 1 <= x <= n) which you should give the position, -1 means the end of this list.

Output

Output the position of each question at one line. Print a blank line after each case.

Sample Input

6
1 3 4 -1

Sample Output

1
3
5

 

Problem Source: 5th JLU Programming Contest - Team, skywind

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
int isprim[1000001],prim[1000001],pet[1000001],fpet[1000001],pr[1000001];
void getprime()
{
    int i,j;
    memset(isprim,0,sizeof(isprim));
    int pnum=0;
    for(i=2;i<1000000;i++)
    {
        if(isprim[i]==0) prim[pnum++]=i;
        for(j=0;j<pnum&&prim[j]*i<=1000000;j++)
        {
            isprim[prim[j]*i]=isprim[i]+1;
        }
    }
    return ;
}
bool cmp(int a,int b)
{
    if(isprim[a]==isprim[b]) return a<b;
    return isprim[a]<isprim[b];
    //return (isprim[a]<isprim[b]||((isprim[a]==isprim[b])&&(a<b)));
}
int main()
{
    int i;
    getprime();
    while(scanf("%d",&n)!=EOF)
    {
       // memcpy(pr,isprim,sizeof(isprim));
        for(i=1;i<=n;i++) pet[i]=i;
        sort(pet+1,pet+n+1,cmp);
        //for(i=2;i<=20;i++) printf("%d..",isprim[i]);printf("/n");
        int a;
        for(i=1;i<=n;i++) fpet[pet[i]]=i;
        while(scanf("%d",&a),a!=-1) printf("%d/n",fpet[a]);
        printf("/n");
       // memcpy(isprim,pr,sizeof(pr));
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值