(kuangbin带你飞--数论基础)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

中文概要:
一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6。给你T组数据,每组n个学生,每个学生都有一个幸运数字,求出要求买n个竹子每个竹子的score都要大于或等于该学生的幸运数字,每个竹竿长度就是花费,求最小花费。

下面附上欧拉函数及欧拉定理的一些东西:

欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 。

通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。
对于质数p,φ(p) = p - 1。注意φ(1)=1.
欧拉定理:对于互质的正整数a和n,有aφ(n) ≡ 1 mod n。
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。
                                 若n是质数p的k次幂,φ(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。

特殊性质:当n为奇数时,φ(2n)=φ(n)
欧拉函数还有这样的性质:
设a为N的质因数,若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N / a) * a;若(N % a == 0 && (N / a) % a != 0) 则有:E(N) = E(N / a) * (a - 1)。


代码实现欧拉函数

#include<iostream>     //欧拉之实现
using namespace std;
int ef(int n)
{
	int cnt=n;
	int i;
	for(i=2;i<=n;i++)
		if(n%i==0)
		{
			cnt - =cnt/i;      //   m-m/p
			while(n%i==0)
				n/=i;
		}
		return cnt;
}
int main()
{
	int n;int m;
	int count;
	while(scanf("%d",&m)!=EOF)
	{
		while(m--){
			scanf("%d",&n);
		count=ef(n);
		printf("%d\n",count);}
	}
	return 0;
}

其实这个问题求长度为p的竹竿它的score其实就是欧拉函数值φ(p),一个素数p的φ(p)=p-1

只需要从x+1(x是幸运数字)开始找第一个出现的素数,那就是最小花费

#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e7+5;
bool prime[N];
void is_prime(){//求素数 
    for(int i=2;i<N;i++){
        prime[i]=true;
     }
    for(int i=2;i*i<N;i++){
        if(prime[i]){
            for(int j=i*i;j<=N;j+=i){
                prime[j]=false;        
             }
         }            
     }
 }
int main(){
    is_prime();
    int t,n;
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>n;
        ll sum=0;
        for(int j=1;j<=n;j++){
            int x;
            cin>>x;
            for(int k=x+1;;k++){
                if(prime[k]){
                    sum+=k;
                   // printf("%d\n",k);
                    break;
                 }
             }
         }
         cout<<"Case "<<i<<": "<<sum<<" Xukha"<<endl;
     }
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值