2354: Number Pyramid
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 5s | 131072K | 250 | 59 | Standard |
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;
}
本文介绍了一种基于整数性质构建特殊金字塔结构的方法,并提供了一个算法实现案例,该算法能够根据输入的数值快速定位其在金字塔中的位置。
8533

被折叠的 条评论
为什么被折叠?



