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
代码:
欧拉筛
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<stdlib.h>
const int maxn=1e6+5;
typedef long long ll;
using namespace std;
int prime[1000005];
bool vis[1000005];
void erla() {
int cnt =0;
memset(vis,false,sizeof(vis));
memset(prime,0,sizeof(prime));
for(int t=2; t<=1000005; t++) {
if(!vis[t]) {
prime[cnt++]=t;
}
for(int j=0; j<cnt&&t*prime[j]<=1000005; j++) {
vis[t*prime[j]]=true;
if(t%prime[j]==0) {
break;
}
}
}
}
int main()
{
int T;
cin>>T;
erla();
int n;
int cnt=1;
while(T--)
{
cin>>n;
ll sum=0;
int x;
int l,r;
for(int t=0;t<n;t++)
{
scanf("%d",&x);
for(int j=x+1;j<=1000003;j++)
{
if(vis[j]==false)
{
sum+=j;
break;
}
}
}
cout<<"Case "<<cnt<<": "<<sum<<" Xukha"<<endl;
cnt++;
}
return 0;
}
二分+欧拉函数
wa了,没找到错
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
const int maxn=1e6+5;
typedef long long ll;
using namespace std;
struct node
{
ll val,id;
}p[maxn];
int Eular(int n)
{
int ans = n;
for (int i = 2 ; i * i <= n ; i++)
{
if (n % i == 0)
{
ans -= ans / i;
while (n % i == 0)
n /= i;
}
}
if (n > 1)
ans -= ans / n;
return ans;
}
bool cmp(node x,node y)
{
if(x.val!=y.val)
{
return x.val<y.val;
}
else
{
return x.id<y.id;
}
}
int main()
{
int cc=0;
for(int t=2;t<=1000003;t++)
{
p[cc].val=Eular(t);
p[cc++].id=t;
}
sort(p,p+cc,cmp);
int T;
cin>>T;
int n;
int cnt=1;
while(T--)
{
cin>>n;
ll sum=0;
int x;
int l,r;
for(int t=0;t<n;t++)
{
scanf("%d",&x);
l=0;r=cc;
while(l<r)
{
int mid=(l+r)>>1;
if(p[mid].val<x)
{
l=mid+1;
}
else
{
r=mid;
}
}
sum+=p[(l+r)>>1].id;
}
cout<<"Case "<<cnt<<": "<<sum<<" Xukha"<<endl;
cnt++;
}
return 0;
}
Bi-shoe and Phi-shoe(欧拉筛)的更多相关文章
- 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛
欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...
- 素数筛&&欧拉筛
折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...
- 欧拉筛,线性筛,洛谷P2158仪仗队
题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- [SDOI2008]仪仗队(欧拉筛裸题)
题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如右图 ...
- POJ3090 Visible Lattice Points 欧拉筛
题目大意:给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 线y=x和坐标轴上的点都被(1,0)(0,1)(1,1)挡住了.除这三个钉子外,如果一个点(x,y)不互质,则 ...
- noip复习——线性筛(欧拉筛)
整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...
- [51NOD1181]质数中的质数(质数筛法)(欧拉筛)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...
- pku-2909 (欧拉筛)
题意:哥德巴赫猜想.问一个大于2的偶数能被几对素数对相加. 思路:欧拉筛,因为在n<215,在3万多,一个欧拉筛得时间差不多4*104, 那么筛出来的素数有4千多个,那么两两组合直接打表,时间复 ...
- hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 高性能python编程之协程(stackless)-乾颐堂
我们都知道并发(不是并行)编程目前有四种方式,多进程,多线程,异步,和协程. 多进程编程在python中有类似C的os.fork,当然还有更高层封装的multiprocessing标准库,在之前写过的 ...
- JDK1.6 1.7 1.8 多版本windows安装 执行命令java -version 版本不变的问题
现在Windows的java安装已经没有解压版本,Oracle官方也不会再提供了,只有安装程序 所以每当安装一次JDK,都会将 java.exe.javaw.exe.javaws.exe三个可执行文件 ...
- redis集群部署及常用的操作命令(下)
搭建好集群之后,为了扩容需要再加入一个节点.那就再复制一个7006,改为相应的redis.conf(复制了改个port就好,如果复制的redis之前属于集群,需要把关联的node.conf之类的去掉) ...
- [GO]匿名字段
package main import ( "fmt" ) type Person struct { name string sex byte age int } type Stu ...
- YII2 全局设定 默认参数
$gridviewSettings = [ 'export' => false, 'responsive' => true, 'floatHeader' => true, 'floa ...
- Linq学习<四> linq to XML
LINQ to XML并不打算替代标准的XML API,例如,XML DOM(Document Object Model).Path.XQuery和XSLT等.如果熟悉这些API或当前需要使用或学习它 ...
- List of HTTP header fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields Content-Type The MIME type of the body of t ...
- eclipse 市场
http://marketplace.eclipse.org/ 如何找工具官网? 举例:UMLet 进入官网以后,下载插件,然后离线安装.
- g++中宏NULL究竟是什么?
NULL是个指针,还是个整数?0?或(void*)0?答案是和g++版本有关.g++ 4.6支持C++11,引入了nullptr,也许会发生变化. 可以写段简单代码求证一下: #include < ...
- 编写高质量代码改善C#程序的157个建议——建议140:使用默认的访问修饰符
建议140:使用默认的访问修饰符(我不太赞成作者的这个观点,这样减少的代码基本可以忽略不计,但是,如果把访问修饰符补充完整,反而会使代码更加易读.我认为自己写代码时应该尽量加上访问修饰符,看别人写的代 ...