Bi-shoe and Phi-shoe(欧拉函数/素筛)题解
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(欧拉函数/素筛)题解的更多相关文章
- 【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛
题目描述 求 输入 第一行包含一个正整数T,表示有T组测试数据.接下来T<=10^5行,每行给出一个正整数N,N<=10^6. 输出 包含T行,依次给出对应的答案. 样例输入 7 1 10 ...
- Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3241 Solved: 1437[Submit][Status][Discuss ...
- 【bzoj2190】【仪仗队】欧拉函数+线性筛(浅尝ACM-J)
向大(hei)佬(e)势力学(di)习(tou) Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪 ...
- Farey Sequence (素筛欧拉函数/水)题解
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...
- lightoj1370欧拉函数/素数筛
这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...
- BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)
一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...
- 【BZOJ2401】陶陶的难题I 欧拉函数+线性筛
[BZOJ2401]陶陶的难题I 题意:求,n<=1000000,T<=100000 题解:直接做是n*sqrt(n)的,显然会TLE,不过这题a和b都是循环到n,那么就可以进行如下的神奇 ...
- 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\) ...
- Lightoj1007【欧拉函数-素数表】
基础题. PS:注意unsigned long long; 以及%llu #include<bits/stdc++.h> using namespace std; typedef unsi ...
随机推荐
- 2015 湘潭大学程序设计比赛(Internet)H题-括号匹配
括号匹配 Accepted : 30 Submit : 234 Time Limit : 10000 MS Memory Limit : 65536 KB 题目描述 有一串括号(只包含&quo ...
- 高并发秒杀系统方案(JSR303参数校验)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- OC convertRect
举个例子: redView = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; redView.backgroundColor ...
- 从浏览器输入参数,到后台处理的vertx程序
vertx由于性能较高,逐渐变得流行.下面将一个vertx的入门案例. 添加依赖 <!-- vertx --> <dependency> <groupId>io.v ...
- PHP操作Redis常用技巧
这篇文章主要介绍了PHP操作Redis常用技巧,结合实例形式总结分析了php针对redis的连接.认证.string.hash等操作技巧与注意事项,需要的朋友可以参考下 本文实例讲述了PHP操作Red ...
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- C# 序列化(Serialize)与反序列化(Deserialize)
序列化是将对象的状态信息转换为可保持或传输的格式的过程(一堆字符),比如转化为二进制.xml.json等的过程. 反序列化就是将在序列化过程中所生成的二进制串.xml.json等转换成数据结构或者对象 ...
- win10 java环境变量的正确配置
变量名:[JAVA_HOME]变量值:[D:\Program Files\Java\jdk1.8.0_92][jdk安装路径]变量名:[Path]变量值:[;%JAVA_HOME%\bin;%JAVA ...
- 批量生成反色图片,用PHOTOSHOP批处理功能。
http://zhidao.baidu.com/link?url=Iz46PDPnEITummTEwo2GtUrK6AeAjlidJ7HtCPJ6NYZJbbllRwNg2iBAcNwF2TYjccP ...