您的位置:首页 > 其它

False Ordering(统计因子个数、素因子分解)

2013-04-02 21:46 453 查看
False Ordering
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1109

Description

We define b is a Divisor of a number a if a is divisible by b. So, the divisors of 12 are 1, 2, 3, 4, 6, 12. So, 12 has 6 divisors.

Now you have to order all the integers from 1 to 1000. x will come before y if

1) number of divisors of x is less than number of divisors of y

2) number of divisors of x is equal to number of divisors of y and x > y.

Input

Input starts with an integer T (≤ 1005), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 1000).

Output

For each case, print the case number and the nth number after ordering.

Sample Input

5

1

2

3

4

1000

Sample Output

Case 1: 1

Case 2: 997

Case 3: 991

Case 4: 983

Case 5: 840

统计一个整数n的因子个数的方法:将n进行素因子分解,n=(q1^r1)*(q2^r2)*...*(qi^ri),则n的因子数为(r1+1)*(r2+1)*...*(ri+1).

因为对于每个q,其对应的指数为r,q可取0个,1个,...,r-1个。

AC Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int dr[1001], num[1001];  //dr[i]是i的因子个数,num[i] = i
int d[500], di = 0;
bool p[1001];

bool cmp(const int& a, const int& b)
{
if(dr[a] != dr[b]) return dr[a] < dr[b];
return  a > b;
}

void Init_Prime()
{
memset(p, 0, sizeof(p));
p[1] = 1;
for(int i = 2; i < 1001; i++)
{
if(!p[i])
{
d[di++] = i;
for(int j = 2; j * i < 1001; j++)
p[i*j] = 1;
}
}
//for(int i = 0; i < di; i++) cout << d[i] << ' ';
return ;
}

void Cal(int x)
{
int t = x;
int sum = 1, cnt;
for(int i = 0; d[i] <= t / 2 && i < di; i++)
{
cnt = 0;
while(x % d[i] == 0)
{
cnt++;
x /= d[i];
}
sum *= (cnt + 1);
}
if(!p[t]) sum *= 2;
dr[t] = sum;
return ;
}

int main()
{
int t, n, ca = 1;
scanf("%d", &t);
Init_Prime();
for(int i = 1; i < 1001; i++)
{
num[i] = i;
Cal(i);
}
//for(int i = 1; i < 30; i++) cout << i << ' ' << dr[i] << endl;
sort(num + 1, num + 1001, cmp);
while(t--)
{
scanf("%d", &n);
printf("Case %d: %d\n", ca++, num
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: