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. 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记

    高斯消元 其实开始只是想搞下线性基,,,后来发现线性基和高斯消元的关系挺密切就一块儿在这儿写了好了QwQ 先港高斯消元趴? 这个算法并不难理解啊?就会矩阵运算就过去了鸭,,, 算了都专门为此写个题解还 ...

  2. java获取系统当前服务器IP地址

    public String getServiceIp(){ InetAddress address; String myIp; try { address = InetAddress.getLocal ...

  3. 【Python】【亲测好用】安装第三方包报错:AttributeError:'module' object has no attribute 'main'

    安装/卸载第三包可能出现如下问题及相应解决办法: 在pycharm编辑中,使用anconda2更新.卸载第三方包时,出现如下错误: AttributeError:'module' object has ...

  4. python基础班-淘宝-目录.txt

    卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python基础班-淘宝-目录.txt│ ├─1-1 Linux基础│ ├─01-课程简介│ │ 01-课程 ...

  5. (4.15)存储DAS,NAS,SAN在数据库存储上的应用

    关键词:存储,硬盘接口类型,磁盘类型,网络类型,DAS,DNS,SAN 转自:http://blog.51cto.com/qianzhang/1254617 一. 硬盘接口类型 1. 并行接口还是串行 ...

  6. python 面向对象 issubclass

    判断是否 他的父类 class Foo(object): pass obj = Foo() class Boo(Foo): pass class Coo(Boo): pass obj = Boo() ...

  7. thinkpad X1 extreme 安装Ubuntu 18.04.2 LTS

    1.安装的时候需要禁用:nouveau.modeset=0 2.安装完成之后需要添加:acpi=off ,ro后面加上3,直接进入终端 3.启动之后:安装nvdia驱动 $ ubuntu-driver ...

  8. oracle怎么恢复被覆盖的存储过程

    在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...

  9. java数据结构经典问题

    A:栈抽象数据类型 1.栈的主要操作 void push(int data);将data数据插入栈中. int pop();删除并返回最后一个插入栈的元素. 2.栈的辅助操作 int top();返回 ...

  10. Jason使用

    Jason是一种数据传输时候的一种格式,类似XML. package liferay; import java.beans.IntrospectionException; import java.be ...