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


题意:给你n个欧拉函数值,找出每一个欧拉函数值大于等于所给值的数,并且相加和最小

思路1:用筛法求1~N的欧拉函数,然后打表每个欧拉函数值的最优解,再取和最小

思路2:因为对于素数Φ(N)=N-1,所以给出p只要找出大于等于p+1的素数即可,用素筛

参考:很详细的欧拉函数解释

代码1:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<string>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=1000100;
const int MOD=1000;
using namespace std;
int euler[N];
int ans[N];
void init(){
memset(ans,-1,sizeof(ans));
for(int i=0;i<N;i++){
euler[i]=i;
}
for(int i=2;i<N;i++){
if(euler[i]==i){
for(int j=i;j<N;j+=i){
euler[j]=euler[j]/i*(i-1); //f(n)=n*(1-1/p1)(1-1/p2)....(1-1/pk)
}
}
}
int now=0;    
for(int i=2;i<N;i++){ //1不符合
if(euler[i]>now && ans[euler[i]]==-1){
ans[euler[i]]=i;
now=euler[i];
}
}
}
int main(){
int T,t,n;
init();
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d",&n);
long long sum=0;
while(n--){
int p;
scanf("%d",&p);
for(int i=p;;i++){
if(ans[i]!=-1){
sum+=ans[i];
break;
}
}
}
printf("Case %d: %lld Xukha\n",t,sum);
}
return 0;
}

代码2:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<string>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=1000100;
const int MOD=1000;
using namespace std;
int prime[N];
void init(){
memset(prime,0,sizeof(prime));
prime[0]=prime[1]=1;
for(int i=2;i<N;i++){
if(!prime[i]){
for(int j=i*2;j<N;j+=i){
prime[j]=1;
}
}
}
}
int main(){
int T,t,n;
init();
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d",&n);
long long sum=0;
while(n--){
int p;
scanf("%d",&p);
p++;
while(prime[p]!=0){
p++;
}
sum+=p;
}
printf("Case %d: %lld Xukha\n",t,sum);
}
return 0;
}

Bi-shoe and Phi-shoe(欧拉函数/素筛)题解的更多相关文章

  1. 【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛

    题目描述 求 输入 第一行包含一个正整数T,表示有T组测试数据.接下来T<=10^5行,每行给出一个正整数N,N<=10^6. 输出 包含T行,依次给出对应的答案. 样例输入 7 1 10 ...

  2. Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3241  Solved: 1437[Submit][Status][Discuss ...

  3. 【bzoj2190】【仪仗队】欧拉函数+线性筛(浅尝ACM-J)

    向大(hei)佬(e)势力学(di)习(tou) Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪 ...

  4. Farey Sequence (素筛欧拉函数/水)题解

    The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...

  5. lightoj1370欧拉函数/素数筛

    这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...

  6. BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)

    一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...

  7. 【BZOJ2401】陶陶的难题I 欧拉函数+线性筛

    [BZOJ2401]陶陶的难题I 题意:求,n<=1000000,T<=100000 题解:直接做是n*sqrt(n)的,显然会TLE,不过这题a和b都是循环到n,那么就可以进行如下的神奇 ...

  8. HDU6434 Count【欧拉函数 线性筛】

    HDU6434 I. Count T次询问,每次询问\(\sum_{i=1}^{n}\sum_{j=1}^{n-1}[gcd(i-j,i+j)=1]\) \(T\le 1e5, n \le 2e7\) ...

  9. Lightoj1007【欧拉函数-素数表】

    基础题. PS:注意unsigned long long; 以及%llu #include<bits/stdc++.h> using namespace std; typedef unsi ...

随机推荐

  1. phpsocketclient以及server样例

    一个菜鸟朋友,突然问了我这个问题...如今稍稍有点时间,就写了一个简单的样例给他,顺便贴上来 server端: <? php /** * @author 邹颢 zouhao619@gmail.c ...

  2. Java-idea-设置类头注释和方法注释

    一.文件级别的注释         主要是通过File-->Setting-->Editor→File and Code Template中来设置 可以再右侧include中设置File ...

  3. MVC html.beginform & ajax.beginform

    1.指定表单提交方式和路径等 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { nam ...

  4. slideUp() 函数

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. [LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst

    Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...

  6. 机器学习理论基础学习13--- 隐马尔科夫模型 (HMM)

    隐含马尔可夫模型并不是俄罗斯数学家马尔可夫发明的,而是美国数学家鲍姆提出的,隐含马尔可夫模型的训练方法(鲍姆-韦尔奇算法)也是以他名字命名的.隐含马尔可夫模型一直被认为是解决大多数自然语言处理问题最为 ...

  7. c++四种强制类型转化

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast C++ 类型转换(C风格的强制转换): (1)将浮点型数据赋值给整型变 ...

  8. js数组之可变函数

    在js的数组中有两个方法为数组添加元素:1.push();2.unshift(),push函数是将元素添加到数组的末尾,现在不用说大家估计也能猜出来,unshift这个函数就是把元素添加到数组的开头的 ...

  9. 026-chmod命令

    语法# chmod [ 选项参数 ]  选择修改权限的对象   权限的改变   目标文件 语义:对哪些目标文件的哪些权限进行修改. (1)# chmod -R ugo +r /home/apple.将 ...

  10. 利用keyframes实现幻灯效果

    源码如下: <style> @keyframes looppic{ from{ background:url(images/1.jpg); /*图片的地址*/ } 25%{ /*可以依据不 ...