A - Bi-shoe and Phi-shoe LightOJ - 1370 【 素数打表】

Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of Bamboos of all possible integer lengths (yes!) are available in the market. According to Xzhila tradition,

Score of a bamboo = Φ (bamboo's length)

(Xzhilans are really fond of number theory). For your information, Φ (n) = numbers less than nwhich are relatively prime (having no common divisor other than 1) to n. So, score of a bamboo of length 9 is 6 as 1, 2, 4, 5, 7, 8 are relatively prime to 9.

The assistant Bi-shoe has to buy one bamboo for each student. As a twist, each pole-vault student of Phi-shoe has a lucky number. Bi-shoe wants to buy bamboos such that each of them gets a bamboo with a score greater than or equal to his/her lucky number. Bi-shoe wants to minimize the total amount of money spent for buying the bamboos. One unit of bamboo costs 1 Xukha. Help him.

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 10000) denoting the number of students of Phi-shoe. The next line contains n space separated integers denoting the lucky numbers for the students. Each lucky number will lie in the range [1, 106].

Output

For each case, print the case number and the minimum possible money spent for buying the bamboos. See the samples for details.

Sample Input

3

5

1 2 3 4 5

6

10 11 12 13 14 15

2

1 1

Sample Output

Case 1: 22 Xukha

Case 2: 88 Xukha

Case 3: 4 Xukha



n个人要爬竹竿,教练要去买竹子。每个人都有一个幸运数字,他们竹子的长度大于他们幸运数字并且和幸运数字互质。问竹子总长度的最小值是多少???


首先当然要素数打表。然后把这些幸运数字从小到大排序。然后再去一一匹配、这样可以大大减少运行时间。直接一个个匹配的话,会超时。还有,最后输出的总和要用long long 否则也会错。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
int prime[1000005];
bool check[1000005];
int num[10005];
int cas=1;
void getprime()///素数打表部分
{
    int flag=0;
    for(int i=2; i<=1000005; i++)
    {
        if(!check[i])
        {
            prime[flag++]=i;
        }
        for(int j=i+i; j<=1000005; j+=i)
        {
            check[j]=1;
        }
    }

}
int main()
{
    M(prime,0);
    M(check,0);

    getprime();
    int n;
    int t;
    cin>>t;
    while(t--)
    {
        M(num,0);
        scanf("%d",&n);
        int flag=0;
        LL sum=0;

        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
        }
        sort(num,num+n);
        int i=0;
        while(i<1000005)///排序后匹配
        {
            if(flag==n)
            {
                break;
            }
            if(prime[i]>num[flag])
            {
                sum+=prime[i];
//                printf("prime==%d\n",prime[i]);
                flag++;
            }
            else
            {
                i++;
            }
        }
        printf("Case %d: %lld Xukha\n",cas++,sum);

    }
    return 0;
}





猜你喜欢

转载自blog.csdn.net/qq_37405320/article/details/77908988
今日推荐