lightoj1370 - Bi-shoe and Phi-shoe(欧拉函数)

来源:互联网 发布:呜呼与嗟乎的意思 编辑:程序博客网 时间:2024/05/16 19:40

http://lightoj.com/volume_showproblem.php?problem=1370

1370 - Bi-shoe and Phi-shoe
PDF (English) Statistics Forum
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 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
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

首先分析下题意,这题就是给你一个数t,让你求使得phi(n)>=t的最小的n。
这题有很多种做法,我简洁的说明两种
1.可以打出1~1e6+7的欧拉数的表,有递推的打法,详情见
http://blog.csdn.net/acdreamers/article/details/8007991
这里有关于欧拉函数和欧拉定理的介绍和一些应用,不妨看一下
接下来就是如何快速的找到第一个值(据说有人直接扫就水过去了,我不了解。。。。)
那么只需要使欧拉函数值的数列变成单调的就可以。
如何实现?因为设n2>n1且phi(n2)<=phi(n1),那么n2肯定不选,所以说我们只需要保存前i项中欧拉函数值得最大值就可以了
phi[i]=max(phi[i],phi[i-1]);
接下来二分查找。我就不多说了
2.
有一个有趣的性质就是
给你一个数t,让你求使得phi(n)>=t的最小的n。
这个n就是大于t的最小的素数。
扔草稿箱了好久,发现证不出来,日了狗了
那么直接打个素数表就好了~~

//这个并没有打欧拉函数的表#include <stdio.h>#include<iostream>#include<cstring>#include<ctime>#include<algorithm>#define Max 2000000using namespace std;typedef long long LL;int p[Max];bool vis[Max];int cnt=0;void isprime(){//这里窝打一个素数表    memset(vis,0,sizeof(vis));    for(int i=2;i<Max;i++){        if(!vis[i])p[++cnt]=i;        for(int j=1;j<=cnt&&p[j]*i<Max;j++){            vis[p[j]*i]=1;            if(i%p[j]==0)break;        }    }}int main(void){    isprime();    int t;    int cas=1;    scanf("%d",&t);    while(t--){        int n;        scanf("%d",&n);        LL sum=0;        int nu;        for(int i=1;i<=n;i++){            scanf("%d",&nu);            sum+=(LL)p[lower_bound(p+1,p+1+cnt,nu+1)-p];            /*每次找到第一个比nu大的素数*/        }        printf("Case %d: %lld Xukha\n",cas++,sum);    }    return 0;}
0 0
原创粉丝点击