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 ...
 
随机推荐
- Python开发【笔记】:what?进程queue还能生产出线程!
			
进程queue底层用线程传输数据 import threading import multiprocessing def main(): queue = multiprocessing.Queue() ...
 - 怎样在 Ubuntu 上使用 ZFS 文件系统 | Linux 中国
			
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79329762 http://mmbiz ...
 - InnoSQL/MySQL并行复制的实现与配置
			
InnoSQL/MySQL并行复制的实现与配置 http://www.innomysql.net/article/6276.html 并行复制之前的解决方案 InnoSQL在5.5.30-v4版本中支 ...
 - 解决 libev.so.4()(64bit) is needed by percona-xtrabackup-2.3.4-1.el6.x86_64案例
			
在mysql主从同步时经常会用到Xtra, XtraBackup可以说是一个相对完美的免费开源数据备份工具,支持在线无锁表同步复制和可并行高效率的安全备份恢复机制相比mysqldump来说优势较大好处 ...
 - 【剑指offer】 二叉树中和为某一值的路径
			
一.题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度 ...
 - requesMapping注解
			
java类 package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; impor ...
 - vue-router中参数传递 && 编程式导航 && 坑 && beforeRouteEnter
			
第一部分: vue-router参数传递 通过router-link我们可以向路由到的组件传递参数,这在我们实际使用中时非常重要的. 路由: { path:"/DetailPage" ...
 - Hadoop集群安装-CDH5(3台服务器集群)
			
CDH5包下载:http://archive.cloudera.com/cdh5/ 主机规划: IP Host 部署模块 进程 192.168.107.82 Hadoop-NN-01 NameNode ...
 - MVC增加操作日志
			
在后台管理中,有一些操作是需要增加操作日志的,尤其是对一些比较敏感的金额类的操作,比如商城类的修改商品金额.删除商品.赠送金额等人工的操作.日志中记录着相关操作人的操作信息,这样,出了问题也容易排查. ...
 - Genymotion虚拟镜像下载慢或者失败的解决办法
			
Genymotion虚拟镜像下载慢或者失败的解决办法 http://files2.genymotion.com/dists/8.0.0/ova/genymotion_vbox86p_8.0_18061 ...