题目:

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个人去买竹子,现在我们定义一根竹子的分数为Φ(L),其中L是竹子的长度,Φ(L)是L的欧拉函数值。这n个人分别有各自的幸运数字,某人要买的竹子必须满足一个条件,即:所买竹子的分数要大于等于相应那个人的幸运数字,现在竹子的价格为1长度1Xukha,问:某人为n个人买到满足条件的竹子的最小开销是多少?输出该最小开销。

分析:

本题从题意上来看是欧拉函数的裸题,首先肯定是预处理求出[1,1000000]的欧拉函数值,但是注意到n可以取到10000且幸运数字可以取到1000000,本题的一个难点在于求出欧拉函数值后如何在限定时间内找到满足每一个人条件的最短竹子。如果我们每次都从0到n直接暴力求解每一个人的竹子价格再相加就会面临TLE问题。考虑到这个情况后我使用了二分来枚举符合当前那个人条件的最短竹子,但是还是TLE了,后来我又尝试预处理每一个n的最短竹子后再直接输出答案,很不巧的是还是TLE了(尽管现在还是不明白为什么二分超时了)。

后来参考了一篇题解的思路。【参考链接】https://www.cnblogs.com/sky-stars/p/11221735.html

显然,对任意L>1都有L>Φ(L),我们不妨先对要处理的n个数从小到大排序,在查找这n个数对应的符合条件的Φ(n)时,如果找到了这个数的满足条件的长度j就结束,但是结束的同时j不清零,记下这个j,下一次遍历直接从这个继续遍历,这样一来就可以大幅缩短查找j的时间。

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> book;
set<int>::iterator it;
map<int,int> L;
bool vis[];
int a[];
int tot=;
int pri[], phi[];
void Get_phi(int N)
{
phi[] = ;
for(int i=; i<=N; ++i)
{
if(!vis[i])
{
pri[++tot] = i;
phi[i] = i-;
}
for(int j=,x; j<=tot&&(x=i*pri[j])<=N; ++j)
{
vis[x] = true;
if(i%pri[j] == )
{
phi[x] = phi[i]*pri[j];
break;
}
else phi[x] = phi[i]*phi[pri[j]];
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
Get_phi();
for(int i=; i<=; i++)
{
if(book.find(phi[i])==book.end())
{
book.insert(phi[i]);
L[phi[i]]=i;
}
}
ll t;
int k=;
scanf("%lld",&t);
while(t--)
{
ll n;
scanf("%lld",&n);
ll ans=;
for(int i=; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int pos=;
for(int i=;i<n;i++)
{
for(int j=pos;;j++)
{
if(phi[j]>=a[i])
{
pos=j;
ans+=j;
break;
}
}
}
printf("Case %d: %lld Xukha\n",k++,ans);
}
return ;
}

LightOJ - 1370 Bi-shoe and Phi-shoe 欧拉函数 题解的更多相关文章

  1. POJ 2407 Relatives 欧拉函数题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  2. FZU 1759 欧拉函数 降幂公式

    Description   Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...

  3. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

  4. HDU 4483 Lattice triangle(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...

  5. UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)

    题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...

  6. 【欧拉函数】【HDU1286】 找新朋友

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  8. SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...

  9. uva 11426 GCD - Extreme (II) (欧拉函数打表)

    题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...

随机推荐

  1. os模块习题

    os 1.使用python代码统计一个文件夹中所有文件的总大小 import os def func(path): size_sum = 0#文件总大小为0 name_lst = os.listdir ...

  2. Python基础之变量,常量,注释,数据类型

    由于上学期学了C语言,对于这一块的内容肯定算熟悉,只是注释的方法有些不同,但得还是一步一步的来!没有基础的同学看了这篇随笔也会大有助益的! 什么是变量?所谓变量就是将一些运算的中间结果暂存到内存中,以 ...

  3. Flutter学习笔记(12)--列表组件

    如需转载,请注明出处:Flutter学习笔记(12)--列表组件 在日常的产品项目需求中,经常会有列表展示类的需求,在Android中常用的做法是收集数据源,然后创建列表适配器Adapter,将数据源 ...

  4. 【Android】drawable VS mipmap

    Android Studio 创建工程后默认的资源文件夹如下图所示: 一直有些疑惑的是 mipmap 和 drawable 文件夹有什么区别,以及是否还需要创建 drawable-xhdpi, dra ...

  5. Core CLR Host 源码简单分析

    在定制 CLR Host的时候,可以通过调用如下代码,来获取当前需要被宿主的程序调用入口: hr = Host->CreateDelegate( domainId, L"Main,Ve ...

  6. windows下hexo+github搭建个人博客

    网上利用hexo搭建博客的教程非常多,大部分内容都大同小异,选择一篇合适的参考,跟着一步一步来即可. 但是,很多博客由于发布时间较为久远等问题,其中某些操作在现在已不再适用,从而导致类似于我这样的小白 ...

  7. 8天入门docker系列 —— 第八天 让程序跑在swarm集群上

    真正的落地部署都是希望程序跑在集群下,而不是单机版下测测玩玩,所以这篇就来聊一下怎么使用docker swarm进行部署,因为是swarm是docker自带的, 所以部署起来还是非常简单的. 一:前置 ...

  8. linux文本编辑vim命令

    1.Vim Vim  是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...

  9. 最全数据分析资料汇总(含python、爬虫、数据库、大数据、tableau、统计学等)

    一.Python基础 Python简明教程(Python3) Python3.7.4官方中文文档 Python标准库中文版 廖雪峰 Python 3 中文教程 Python 3.3 官方教程中文版 P ...

  10. iOS的录屏功能

    iOS的录屏功能其实没什么好说的,因为网上的教程很多,但是网上的Demo无一例外几乎都有一个bug,那就是iPad上会出现闪退,这也体现了国内的教程文档的一个特点,就是抄袭,教程几乎千篇一律,bug也 ...