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(欧拉筛)的更多相关文章

  1. 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛

    欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...

  2. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

  3. 欧拉筛,线性筛,洛谷P2158仪仗队

    题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...

  4. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

  5. [SDOI2008]仪仗队(欧拉筛裸题)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如右图 ...

  6. POJ3090 Visible Lattice Points 欧拉筛

    题目大意:给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 线y=x和坐标轴上的点都被(1,0)(0,1)(1,1)挡住了.除这三个钉子外,如果一个点(x,y)不互质,则 ...

  7. noip复习——线性筛(欧拉筛)

    整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...

  8. [51NOD1181]质数中的质数(质数筛法)(欧拉筛)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...

  9. pku-2909 (欧拉筛)

    题意:哥德巴赫猜想.问一个大于2的偶数能被几对素数对相加. 思路:欧拉筛,因为在n<215,在3万多,一个欧拉筛得时间差不多4*104, 那么筛出来的素数有4千多个,那么两两组合直接打表,时间复 ...

  10. hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. [C++] Function Template - optional parameter

    Function Template

  2. python实现高效率的排列组合算法-乾颐堂

    组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中. 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数. 然后从左到右扫描数组 ...

  3. Source Insight: has been changed outside of Source Insight

    has been changed outside of Source Insight 2018年04月26日 09:41:01 linux_c_coding_man 阅读数:247 摘自:https: ...

  4. kcp结构体字段含义

    conv 会话IDmtu 最大传输单元mss 最大分片大小state 连接状态(0xFFFFFFFF表示断开连接)snd_una 第一个未确认的包snd_nxt 下一个待分配的包的序号rcv_nxt ...

  5. js总结33 :javascript-DOM节点属性

    1 设置节点属性三个方法: 获取:getAttribute(名称) 设置:setAttribute(名称, 值) 删除:removeAttribute(名称) 举个例子: <!DOCTYPE h ...

  6. HDU 3366 Passage (概率DP)

    题意:T组测试数据,一个人困在了城堡中,有n个通道,m百万money ,每个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i], 遇到士兵得给1百万money,否则会被杀掉,还有 1-p ...

  7. Hibernate查询对象所有字段,单个字段 ,几个字段取值的问题

    HQL 是Hibernate Query Language的简写,即 hibernate 查询语言:HQL采用面向对象的查询方式.HQL查询提供了更加丰富的和灵活的查询特性,因此Hibernate将H ...

  8. 洛谷P2634 [国家集训队]聪聪可可 (点分治)

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...

  9. hdu2993 MAX Average Problem (斜率dp)

    参考:http://www.cnblogs.com/kuangbin/archive/2012/08/27/2657878.html //#pragma warning (disable: 4786) ...

  10. java学习(五)java类继承

    1.制作一个工具类的文档 javadoc -d 目录 -author -version   arrayTool.java 实例: class arrayDemo { public static voi ...