light oj 1370-Bi-shoe and Phi-shoe (欧拉函数模板题)

来源:互联网 发布:淘宝开店教程自学网 编辑:程序博客网 时间:2024/06/05 17:46

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 n which 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


唉这个题,真的无话可说,卡时间卡了20多发全是TLE,也是菜,想不出来时间少的算法,直接套板子了,不过有一点需要注意,就是检测欧拉函数的时候,从这个数-5000到这个数+5000进行遍历,某个数的欧拉值大于等于输入的值,再求和,区间太小答案不对,区间太大就超时!

附代码:

#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;const int maxn=1000100;int m[maxn],phi[maxn],p[maxn],pt;void Euler(){    memset(m,0,sizeof(m));    memset(phi,0,sizeof(phi));    memset(p,0,sizeof(p));    phi[1]=0;    int N=maxn;    int k;    for(int i=2;i<N;i++)    {        if(!m[i])            p[pt++]=m[i]=i,phi[i]=i-1;        for(int j=0;j<pt&&(k=p[j]*i)<N;j++)        {            m[k]=p[j];            if(m[i]==p[j])            {                phi[k]=phi[i]*p[j];                break;            }            else                phi[k]=phi[i]*(p[j]-1);        }    }}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    Euler();    int t,k,num;long long sum=0;    scanf("%d",&t);    for(int i=1;i<=t;i++)    {        scanf("%d",&k);        for(int j=1;j<=k;j++)        {            scanf("%d",&num);            for(int s=num-5000;s<=num+5000;s++)            {                if(s>0&&phi[s]>=num)                {                    sum+=s;                    break;                }            }        }        printf("Case %d: %lld Xukha\n",i,sum);        sum=0;    }    return 0;}


阅读全文
1 0