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. mosquitto配置文件/etc/mosquitto/mosquitto.conf配置参数详细说明

    mosquitto配置文件/etc/mosquitto/mosquitto.conf配置参数详细说明 摘自:https://blog.csdn.net/weixin_43025071/article/ ...

  2. mysql-day01

    Microsoft Windows [版本 10.0.17134.648](c) 2018 Microsoft Corporation.保留所有权利. C:\Users\lijun>java用法 ...

  3. scala初学笔记

    tips: 1.函数的定义: def addOne(m: Int): Int = m + 1 m为参数,要指定其类型,Int: 后边跟着返回值的类型,Int= 后边是函数的内部 2.匿名函数: (x: ...

  4. 代码审查清单 Code Review

    代码审查清单 常规项 代码能够工作么?它有没有实现预期的功能,逻辑是否正确等. 所有的代码是否简单易懂? 代码符合你所遵循的编程规范么?这通常包括大括号的位置,变量名和函数名,行的长度,缩进,格式和注 ...

  5. 编写高质量代码改善C#程序的157个建议——建议125:避免用FCL的类型名称命名自己的类型

    建议125:避免用FCL的类型名称命名自己的类型 试想过自己写一个Socket类型吗?如果没有,我们来尝试一下: public class Socket { //省略 } 把以上代码同某些其他工具类封 ...

  6. 编写高质量代码改善C#程序的157个建议——建议67:慎用自定义异常

    建议67:慎用自定义异常 除非有充分的理由,否则不要创建自定义异常.如果要对某类程序出错做特殊处理,那就自定义异常.需要自定义异常的理由如下: 1)方便测试.通过抛出一个自定义的异常类型实例,我们可以 ...

  7. 安卓利用Handlers,AsyncTask和Loaders运行后台程序

    安卓的用户界面线程(user interface thread) 1.1 主线程 安卓修改用户界面并从一个单一用户界面线程中处理输入事件,这个线程也被称作主线程(main thread) Androi ...

  8. tcxgrid控件中drag a column header here to group by that column移除方法

  9. Android-SDCard外部存储文件读写

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses ...

  10. C#里面获得应用程序的当前路径

    在C#里面获得应用程序的当前路径 Environment.CurrentDirectorySystem.IO.Directory.GetCurrentDirectory() ——上面两种获得的是当前路 ...