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 ...
随机推荐
- idea 创建的maven+spring+mybatis项目整合 报错无法创建bean
报错如下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with n ...
- I Hate It---hdu1754线段树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1754 和上一题一样是模板题,就是那道题求得是和,这道求得是最大值: #include<iostrea ...
- zabbix 报错汇总
打开zabbix web界面点击profile出现以下报错信息: scandir() has been disabled for security reasons [profile.php:198 → ...
- xss跨站脚本攻击及xss漏洞防范
xss跨站脚本攻击(Cross Site Scripting,因与css样式表相似故缩写为XSS).恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Scrip ...
- Java多线程面试、笔试方向---后续补充
1.ThreadLocal类 线程级别的局部变量,为每个使用该变量的线程提供一个独立的变量副本,每个线程修改副本时不影响其他线程对象的副本. ThreadLocal实例通常作为静态私有字段出 ...
- js-jquery-对象与JSON字符串互相转换
1:jQuery插件支持的转换方式 代码如下: String→Object$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转 ...
- 高性能mysql 4 ,5章
第4章 1:查询优化,多表连接时只取需要的列,要对select * 保持怀疑. 2:如果发现访问的数据行数很大,而生成的结果中数据行很少,那么可以尝试更复杂的修改 a: 使用覆盖索引,b: 更改架构, ...
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- print(array)时array中间是省略号没有输出全部的解决方法
import numpy as np np.set_printoptions(threshold=np.inf) 大量元素情况 可以采用set_printoptions(threshold='nan' ...
- scipy模块