LightOJ 1370 Bi-shoe and Phi-shoe

1370 - Bi-shoe and Phi-shoe
Time Limit: 2 second(s) Memory Limit: 32 MB

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

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

(Xzhilans are really fond of number theory). For yourinformation, Φ (n) = numbers less thann which arerelatively prime (having no common divisor other than 1) ton. So, scoreof 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 eachstudent. 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 scoregreater than or equal to his/her lucky number. Bi-shoe wants to minimize thetotal amount of money spent for buying the bamboos. One unit of bamboo costs 1Xukha. 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 linecontainsn space separated integers denoting the lucky numbers for thestudents. Each lucky number will lie in the range[1, 106].

Output

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

Sample Input

Output for Sample Input

3

5

1 2 3 4 5

6

10 11 12 13 14 15

2

1 1

Case 1: 22 Xukha

Case 2: 88 Xukha

Case 3: 4 Xukha

題意是知道一串數字。然後規定一個規則,score 爲 length以內與length互質的數的個數,每個數字依次分析,對於每個數字,求不小於這個數的score對應的length,然後加起來。

這個題可以轉化一下題意,我們知道對於一個length爲質數的數,他的score一定等於length - 1,然後對於任意一個合數,他的score一定小於它前面的那個質數的score,所以題意就轉化爲求解比當前數字大的最小的質數的大小,將所有的累計,得到最後的答案。

歐拉篩法解決。

代碼如下:

/*************************************************************************
	> File Name: Bi-shoe_and_Phi-shoe.cpp
	> Author: Zhanghaoran
	> Mail: [email protected]
	> Created Time: Tue 08 Dec 2015 12:38:03 PM CST
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;
const int Max = 1000100;
int Prim[Max];
int Isprim[Max];

void euler(){
    int ans = 0;
    Isprim[0] = Isprim[1] = 1;
    for(int i = 2; i <= Max - 1; i ++){
        if(!Isprim[i])
            Prim[ans ++] = i;
        for(int j = 0; j < ans &&  Prim[j] * i <= Max - 1; j ++){
            Isprim[Prim[j] * i] = 1;
            if(i % Prim[j] == 0)
                break;
        }
    }
}

int T;
int N;
int main(void){
    cin >> T;
    long sum = 0;
    euler();
    int cas = 0;
    while(T --){
        cin >> N;
        cas ++;
        int temp;
        for(int i = 0; i < N; i ++){
            cin >> temp;
            for(int j = temp + 1; j <= Max - 1; j ++){
                if(!Isprim[j]){
                    sum += j;
                    break;
                }
            }
        }
        printf("Case %d: %ld Xukha\n", cas, sum);
        sum = 0;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章