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

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;
}


全部评论

相关推荐

霁华Tel:秋招结束了,好累。我自编了一篇对话,语言别人看不懂,我觉得有某种力量在控制我的身体,我明明觉得有些东西就在眼前,但身边的人却说啥也没有,有神秘人通过电视,手机等在暗暗的给我发信号,我有时候会突然觉得身体的某一部分不属于我了。面对不同的人或场合,我表现出不一样的自己,以至于都不知道自己到底是什么样子的人。我觉得我已经做的很好,不需要其他人的建议和批评,我有些时候难以控制的兴奋,但是呼吸都让人开心。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务