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) 值为 ...
随机推荐
- 打包名命令:tar
将多个文件或目录包成一个大文件的命令功能,我们称它是一种"打包命令". tar的参数非常多,这里只列出几个常用的参数,更多的参数你可以自行man tar查询. [root@www ...
- TP框架基础(一)
[使用框架] 官网:thinkphp.cn. 目前建议使用thinkPHP3.2版本 一.结构目录>Thinkphp文件夹,是thinkPHP的核心文件,里面的内容是不允许我们修改的 > ...
- 段落超出div部分隐藏显示
overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
- web设计_1_思路总览
核心思想:结构和样式分离 HTML与CSS 只有充分将页面核心内容和外观设计相分离而获得的灵活性,才能顺利构建出能够满足每个web用户需要的最佳设计方案. 核心要求:灵活性 适应不同的浏览器,适应各种 ...
- 【iOS】The filename 未命名.ipa in the package contains an invalid character(s)
提交 APP 到苹果官网审核时遇到了这个问题,如图: 其实就是不支持中文,随便换个英文名就行了. 参考:http://blog.csdn.net/u011439689/article/details/ ...
- Android 开发环境之 VMware 虚拟机(android8.1)
VM版本14 在官网下载androidx86的VMDK文件 官方下载地址 (VMDK文件是VMware的专用文件,比iso镜像文件安装要简便许多,内部已经配置好了,只需要按照虚拟机安装普通流程即可) ...
- Nginx配置安装(Mac)
我用到的安装工具是:homebrew 真的很方便! 步骤1: 打开终端,输入 brew info nginx结果:我们可以看到,nginx在本地还未安装(Not installed),nginx的来源 ...
- 非UI线程更新界面
package com.caterSys.Thread; import java.text.SimpleDateFormat; import java.util.Date; import org.ec ...
- mac安装ElasticSearch+head+node+一个例子~
1.下载ElasticSearch 官网下载链接:https://www.elastic.co/cn/downloads/past-releases(进去的可能会比较慢,网络好的情况下会好一些) 我下 ...
- 精准测试与开源工具Jacoco的覆盖率能力大PK
导读:本文根据实际使用情况,简要分析了精准测试和类Jacoco等传统白盒工具在设计理念.功能和应用场景的异同点,并阐述了覆盖率技术如何在新型企业开发体系中,发挥应有的重要作用. 覆盖率技术可以说是测试 ...