LightOJ - 1370 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 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 欧拉函数 题解的更多相关文章
- POJ 2407 Relatives 欧拉函数题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- poj3696 快速幂的优化+欧拉函数+gcd的优化+互质
这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...
- HDU 4483 Lattice triangle(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
- 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 ...
- 【欧拉函数】【HDU1286】 找新朋友
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- 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 ...
- 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) 值为 ...
随机推荐
- 阿里云服务器连接以及centos 搭建 web java环境(linux java部署 tomcat部署)
版权声明:本文为博主原创文章,未经博主允许不得转载. 最近弄了个试用阿里云服务器倒腾了半天终于部署好,分享一下. 1.登入阿里云打开你申请的是云服务器的实例: 点击重置密码---重置密码后重启服务器才 ...
- Linux vi/vim使用
vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令. 1.vi的基本概念 基本上vi ...
- (12)ASP.NET Core 中的配置二(Configuration)
1.内存配置 MemoryConfigurationProvider使用内存中集合作为配置键值对.若要激活内存中集合配置,请在ConfigurationBuilder的实例上调用AddInMemory ...
- 基于 Autojs 的 APP、小程序自动化测试 SDK - 2019年8月3日
原文:https://blog.csdn.net/laobingm/article/details/98317394 autojs sdk基于 Autojs 的 APP.小程序自动化测试 SDK,支持 ...
- 2019前端面试系列——JS高频手写代码题
实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...
- .net持续集成测试篇之Nunit文件断言、字符串断言及集合断言
使用前面讲过的方法基本上能够完成工作中的大部分任务了,然而有些功能实现起来还是比较麻烦的,比如说字符串相等性比较不区分大小写,字符串是否匹配某一正则规则,集合中的每一个(某一个)元素是否符合特定规则等 ...
- Java 设置PDF文档浏览偏好
在查看PDF文档时,可进行一些浏览偏好设置,例如是否全屏浏览.隐藏或显示菜单栏/工具栏.设置页面布局模式等,下面将通过Java编程的方式来演示如何设置. 使用工具: Free Spire.PDF fo ...
- 5.Go-封装、继承、接口、多态和断言
面向对象 Go语言开发者认为:面向对象就是特定类型(结构体)有着自己的方法,利用这个方法完成面向对象编程, 并没有提封装.继承.多态.所以Go语言进行面向对象编程时,重点在于灵活使用方法. Go语言有 ...
- DevOps实施历程-v1.0
有AF项目的成功案例(DevOps实施历程-半自动化),公司新项目全部依此为模板,实现了从代码到安装的自动化流水线,为此我输出了Jenkins自动化指南.AF项目指南等文档,方便大家查阅和参 ...
- 有趣的RPC理解
RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在,如TCP或UDP,为通 ...