LightOJ - 1370 -Bi-shoe and Phi-shoe (素数与欧拉的关系)

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=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

扫描二维码关注公众号,回复: 2649443 查看本文章

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

题目大意:一个人教练要为他的学生们买竹竿,竹竿的价格=主干的长度,主干的价值=竹竿长度的欧拉值(质因子的个数),每个学生都有一个幸运数字,教练想要每个同学的竹竿的价值都大于等于他们的幸运数字,教练又想少花钱,输出满足条件的最少花费。

方法一:对于每个幸运数字,最近的就是下一个素数  ( 对于素数x: Φ(x)=x-1 )

因此,打一个素数表,遍历求和即可。

方法二:直接打一个欧拉表,查找即可。

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

ll oula[1000100],arr[1000100];

/*ll euler(ll n)
{
	ll res=n;
	for(ll i=2;i*i<=n;++i)
	{
		if(n%i==0)
		{
			res=res/i*(i-1);
			while(n%i==0)
				n=n/i;
		}
	}
	if(n>1)
		res=res/n*(n-1);
	return res;
}*/

void intt()
{
	clean(oula,0);
	clean(arr,0);
	oula[1]=1;
	for(int i=2;i<1000100;++i)
	{
		if(oula[i]==0)
		{
			for(int j=i;j<1000100;j=j+i)
			{
				if(oula[j]==0)
					oula[j]=j;
				oula[j]=oula[j]-oula[j]/i;
			}
		}
	}
}

int main()
{
	intt();
	int t,num=1;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		ll ans=0;
		for(int i=0;i<n;++i)
		{
			int x;
			cin>>x;
			for(int j=x+1;j<1000100;++j)
			{
				if(oula[j]>=x)//符合要求的欧拉值
				{
					ans=ans+j;
					break;
				}
			}
		}
		cout<<"Case "<<num++<<": "<<ans<<" Xukha"<<endl;
	}
}

 

 

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81505160