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 ...
随机推荐
- Spring Boot☞ 统一异常处理
效果区: 代码区: package com.wls.integrateplugs.exception.dto; public class ErrorInfo<T> { public st ...
- centos7设置、查看、删除环境变量的方法
centos查看环境变量与设置环境变量在使用过程中很常见,本文整理了一些常用的与环境变量相关的命令,感兴趣的朋友可以参考下希望对你有所帮助 1. 显示环境变量HOME(红色部分代表要输入的命令,不要把 ...
- ConfigureAwait(false)避免上下文延续
之前MVC利用MvcHtmlString封装通用下拉菜单,菜单数据需要从webapi获取,自然用到了 await Http Client.GetAsync(Url)方法,前端 @Html.Select ...
- CSS 实现等高布局以及多行文本垂直居中
将display属性设置为table-cell,具有table的特点. 1.同行等高. 2.宽度自动调节. 相当于表格是td, <style type="text/css"& ...
- Web环境中Spring的启动过程
1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...
- WebCalendar.js
var cal; var isFocus=false; //是否为焦点 var pickMode ={ "second":1, "minu ...
- linux下PHP5.5的安装【oci8,pdo-oci,memcache,Zend OPCache扩展】
最近一段时间学习了一下PHP,用CI做了一个小项目,为了开发方便,本地windows下使用了集成环境XAMPP,不过当把项目部署到linux上时,确实遇到了很多问题,下面把我在linux上安装php的 ...
- Replication--发布属性immediate_sync
在创建发布时,如果选择立即初始化,会将immediate_sync属性设置为true.如果immediate_sync属性为true时,snapshot文件和发布事务及发布命令将一直保留到指定的事务保 ...
- ecahrt 扇形(半扇形)
var data = [{ "name": "1", "value": 54 }, { "name": "2& ...
- django drf 初探serializer
1.定义Model对应的serializer from rest_framework import serializers class GoodsSerializer(serializers.Seri ...